Use esttab to generate summary statistics by group with columns for mean difference and significance

Richard Herron picture Richard Herron · Mar 21, 2013 · Viewed 45k times · Source

I would like to use esttab (ssc install estout) to generate summary statistics by group with columns for the mean difference and significance. It is easy enough to generate these as two separate tables with estpost, summarize, and ttest, and combine manually, but I would like to automate the whole process.

The following code generates the two components of the desired table.

sysuse auto, clear

* summary statistics by group
eststo clear
by foreign: eststo: quietly estpost summarize ///
    price mpg weight headroom trunk
esttab, cells("mean sd") label nodepvar   

* difference in means
eststo: estpost ttest price mpg weight headroom trunk, ///
    by(foreign) unequal 
esttab ., wide label   

And I can print the two tables and cut-an-paste into one table.

* can generate similar tables and append horizontally
esttab, cells("mean sd") label
esttab, wide label


* manual, cut-and-paste solution
-------------------------------------------------------------------------------------------------------
                              (1)                       (2)                         (3)                

                             mean           sd         mean           sd         
-------------------------------------------------------------------------------------------------------
Price                    6072.423     3097.104     6384.682     2621.915         -312.3         (-0.44)
Mileage (mpg)            19.82692     4.743297     24.77273     6.611187         -4.946**       (-3.18)
Weight (lbs.)            3317.115     695.3637     2315.909     433.0035         1001.2***       (7.50)
Headroom (in.)           3.153846     .9157578     2.613636     .4862837          0.540**        (3.30)
Trunk space (.. ft.)        14.75     4.306288     11.40909     3.216906          3.341***       (3.67)
-------------------------------------------------------------------------------------------------------
Observations                   52                        22                          74                
-------------------------------------------------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

It seems that I should be able to get the desired table with one esttab call and without cutting-and-pasting, but I can't figure it out. Is there a way to generate the desired table without manually cutting-and-pasting?

I would prefer to output a LaTeX table, but anything that eliminates the cutting-and-pasting is a big step, even passing through a delimited text file.

Answer

Mattias &#214;hman picture Mattias Öhman · Jul 8, 2016

If you still want to use esttab, you can play around using cells and pattern. The table in the original post can be replicated with the following code:

sysuse auto, clear

eststo domestic: quietly estpost summarize ///
    price mpg weight headroom trunk if foreign == 0
eststo foreign: quietly estpost summarize ///
    price mpg weight headroom trunk if foreign == 1
eststo diff: quietly estpost ttest ///
    price mpg weight headroom trunk, by(foreign) unequal

esttab domestic foreign diff, ///
cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2)) t(pattern(0 0 1) par fmt(2))") ///
label

which yields

-----------------------------------------------------------------------------------------------------
                              (1)                       (2)                       (3)                

                             mean           sd         mean           sd            b               t
-----------------------------------------------------------------------------------------------------
Price                     6072.42      3097.10      6384.68      2621.92      -312.26         (-0.44)
Mileage (mpg)               19.83         4.74        24.77         6.61        -4.95**       (-3.18)
Weight (lbs.)             3317.12       695.36      2315.91       433.00      1001.21***       (7.50)
Headroom (in.)               3.15         0.92         2.61         0.49         0.54**        (3.30)
Trunk space (.. ft.)        14.75         4.31        11.41         3.22         3.34***       (3.67)
-----------------------------------------------------------------------------------------------------
Observations                   52                        22                        74                
-----------------------------------------------------------------------------------------------------