将带有两个分组依据变量的摘要 table 导出到 LaTeX

Export summary table with two group-by variables to LaTeX

我正在尝试使用 社区贡献 命令 estout 将双向摘要 table 导出到 LaTeX。这是一个 table 总结了两个分类变量 foreignpricehigh:

的数值 weight 的平均值
sysuse auto, clear
gen pricehigh = 0
replace pricehigh = 1 if price > 6165

tabulate foreign pricehigh, summarize(weight) means label

                          Means of Weight (lbs.)

           |      pricehigh
  Car type |         0          1 |     Total
-----------+----------------------+----------
  Domestic | 3,080.513  4,026.923 | 3,317.115
   Foreign | 2,118.462  2,601.111 | 2,315.909
-----------+----------------------+----------
     Total |     2,840  3,443.636 | 3,019.459

但是,Stata 告诉我在使用 tabulateestpost 时不允许 tabulatesummarize() 选项:

estpost tabulate foreign pricehigh, summarize(weight) means label
option summarize() not allowed
r(198);

我一直在搜索 estout 文档(特别是 here)和 Statalist,但找不到如何使用 estout 重新创建此 table。

社区贡献命令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 执行相同操作需要您自己创建 table:

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
---------------------------------------------------

此处使用命令 esttabestout 的包装器)进行说明。