stata--二维表
2019-10-13 本文已影响0人
古城路揸fit人
什么是二维表(twoway tables)
用来统计分类变量(categories)的频数的
tab命令
二维表基础命令,统计一个变量叫一维表,两个变量叫二维表。
tab后跟的第一个变量可以认为是“垂直变量”,第二个变量可以认为是“水平变量”。
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
准备工作要设定var lab,每个值lab
*准备工作
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"
效果图