什么是二維表(twoway tables)
用來統(tǒng)計(jì)分類變量(categories)的頻數(shù)的
tab命令
二維表基礎(chǔ)命令,統(tǒng)計(jì)一個變量叫一維表,兩個變量叫二維表。
tab后跟的第一個變量可以認(rèn)為是“垂直變量”,第二個變量可以認(rèn)為是“水平變量”。
sysuse nlsw88, clear
tab union //一維表
tab union sex //二維表
二維表輸出命令tabout
tabout race south collgrad using table39.docx, ///
replace style(docx) font(bold) c(freq row col) ///
f(0c 1 1) layout(cb) landscape title(Table 39: ///
Example of rotation with docx output style) ///
fn(nlsw88.dta) open
**c(freq col) ** is the contents option, which determines the cell
contents: frequencies and column percentages
f(0c 1) is the format option and sets the cell numbers as zero decimal points with commas for the frequencies and one decimal point for the column percentages
font(bold) turns on bold formatting for the variable labels and the
second row of the heading;
twidth(11) sets the width of the table to 11 centimetres
title(Table 1 etc) is placed above the table
fn(Source etc) is a footnote placed below the table
準(zhǔn)備工作要設(shè)定var lab,每個值lab
*準(zhǔn)備工作
sysuse nlsw88, clear
la var union "Member of a union"
la def union 0 "Not a union member" ///
1 "Union member", modify
la val union union
la var south "Location"
la def south 0 "Does not live in the South" ///
1 "Lives in the South", modify
la val south south
la var race "Race"
la def race 1 "White" 2 "Black" ///
3 "Other", modify
la val race race
la var collgrad "Education"
la def collgrad 0 "Not college graduate" ///
1 "College graduate", modify
la val collgrad collgrad
la var married "Marital status"
la def married 0 "Single" 1 "Married", modify
la val married married
gen wt = 10 * runiform()
gen int fwt = ceil(wt)
gen inc = 1000 * runiform()
gen income = cond(inc < 300, inc + 360, inc +200)
la var income "Income"
gen sex = cond(wt<5, 1, 2)
la var sex "Sex"
la def sex 1 "Male" 2 "Female", modify
replace sex = cond(wt<0.5, ., sex)
la val sex sex
gen pregnant = cond(wt>8.5, 1, 2)
la var pregnant "Currently pregnant"
la def pregnant 1 "Pregnant" 2 "Not pregnant" ///
, modify
replace pregnant = cond(sex==1, ., pregnant)
la val pregnant pregnant
la var industry "Industry"
la var occupation "Occupation"

效果圖