community-contributed 命令tabout 可以轻松生成所需的输出,如下所示:
sysuse auto, clear
generate pricehigh = 0
replace pricehigh = 1 if price > 6165
tabout foreign pricehigh using table.tex, style(tex) content(mean weight) sum replace
type table.tex
\begin{center}
\footnotesize
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\begin{tabularx} {14} {@{} l Y Y Y @{}}
\toprule
& \multicolumn{3}{c}{pricehigh} \\
\cmidrule(l{1em}){2-4}
& 0 & 1 & Total \\
\cmidrule(l{1em}){2-4}
& Mean weight & Mean weight & Mean weight \\
\midrule
Car type \\
Domestic & 3,080.5 & 4,026.9 & 3,317.1 \\
Foreign & 2,118.5 & 2,601.1 & 2,315.9 \\
Total & 2,840.0 & 3,443.6 & 3,019.5 \\
\bottomrule
\end{tabularx}
\normalsize
\end{center}
相比之下,对estout 执行相同操作需要您自己创建表:
sysuse auto, clear
generate pricehigh = 0
replace pricehigh = 1 if price > 6165
matrix A = J(3, 3, 0)
summarize weight if !foreign & !pricehigh, meanonly
matrix A[1,1] = r(mean)
summarize weight if !foreign & pricehigh, meanonly
matrix A[1,2] = r(mean)
summarize weight if !foreign, meanonly
matrix A[1,3] = r(mean)
summarize weight if foreign & !pricehigh, meanonly
matrix A[2,1] = r(mean)
summarize weight if foreign & pricehigh, meanonly
matrix A[2,2] = r(mean)
summarize weight if foreign, meanonly
matrix A[2,3] = r(mean)
summarize weight if !pricehigh, meanonly
matrix A[3,1] = r(mean)
summarize weight if pricehigh, meanonly
matrix A[3,2] = r(mean)
summarize weight, meanonly
matrix A[3,3] = r(mean)
matrix colnames A = 0 1 Total
matrix rownames A = Domestic Foreign Total
结果:
esttab matrix(A), title(Means of Weight (lbs.)) mtitles(pricehigh) gaps
Means of Weight (lbs.)
---------------------------------------------------
pricehigh
0 1 Total
---------------------------------------------------
Domestic 3080.513 4026.923 3317.115
Foreign 2118.462 2601.111 2315.909
Total 2840 3443.636 3019.459
---------------------------------------------------
这里使用命令esttab(estout 的包装器)进行说明。