2
votes

I am trying to export a two-way summary table to LaTeX using the community-contributed command estout. This is a table summarizing the mean values of numerical weight across two categorical variables foreign and 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

However, Stata tells me that the summarize() option for tabulate is not allowed when using tabulate and estpost:

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

I have been searching the estout documentation (particularly here) and Statalist, but cannot find how to re-create this table using estout.

1

1 Answers

0
votes

The community-contributed command tabout can easily produce the desired output as follows:

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}

In contrast, doing the same with estout requires you to create the table yourself:

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

Result:

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

The command esttab (a wrapper for estout) is used here for illustration.