user6366660
user6366660

Reputation: 21

Creating side-by-side summary statistics table using esttab

I am trying to create a summary stats table with different categorical variables.

This is what I have managed to achieve with the community-contributed command esttab:

enter image description here

The code I have used is the following:

estpost tabstat diff_32 diff_43 diff_54 diff_42 diff_52 diff_53, ///
by(es) stat(mean sd) nototal columns(stat)

esttab . using "$tables/25_trial", replace cells(mean sd) tex label nogaps

However, what I need is for the columns to be side-by-side such that my mean is the main cell, with standard deviation as auxiliary. In other words, each column would contain one categorical variable, with mean and standard deviation in the same cell.

How can I produce the desired output?

Upvotes: 2

Views: 2934

Answers (1)

user8682794
user8682794

Reputation:

Consider the following toy example:

sysuse auto, clear          
estpost tabstat price weight mpg, by(foreign) stat(mean sd) nototal columns(stat)

esttab ., cells(mean sd) label nogaps tex

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
                    &\multicolumn{1}{c}{(1)}\\
                    &\multicolumn{1}{c}{}\\
                    &     mean/sd\\
\hline
Domestic            &            \\
Price               &    6072.423\\
                    &    3097.104\\
Weight (lbs.)       &    3317.115\\
                    &    695.3637\\
Mileage (mpg)       &    19.82692\\
                    &    4.743297\\
\hline
Foreign             &            \\
Price               &    6384.682\\
                    &    2621.915\\
Weight (lbs.)       &    2315.909\\
                    &    433.0035\\
Mileage (mpg)       &    24.77273\\
                    &    6.611187\\
\hline
Observations        &          74\\
\hline\hline
\end{tabular}
}

One has to merely use the unstack option in esttab to get the desired output:

esttab ., cells(mean sd) label nogaps tex unstack

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{2}{c}}
\hline\hline
                    &\multicolumn{2}{c}{(1)}  \\
                    &\multicolumn{2}{c}{}     \\
                    &    Domestic&     Foreign\\
                    &     mean/sd&     mean/sd\\
\hline
Price               &    6072.423&    6384.682\\
                    &    3097.104&    2621.915\\
Weight (lbs.)       &    3317.115&    2315.909\\
                    &    695.3637&    433.0035\\
Mileage (mpg)       &    19.82692&    24.77273\\
                    &    4.743297&    6.611187\\
\hline
Observations        &          74&            \\
\hline\hline
\end{tabular}
}

Upvotes: 2

Related Questions