我正在尝试使用社区贡献的命令estout将双向汇总表导出到LaTeX。这是一张表格,总结了两个类别变量weightforeign的数值pricehigh的平均值:

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告诉我,使用summarize()tabulate时,不允许tabulateestpost选项:

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


我一直在搜索estout文档(尤其是here)和Statalist,但是找不到如何使用estout重新创建该表的方法。

最佳答案

社区提供的命令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
---------------------------------------------------


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

关于export - 将带有两个分组依据变量的摘要表导出到LaTeX,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42653942/

10-11 02:14