Search code examples
formattingformatlatexoutputstata

Subgroups in esttab with means


Here is the Stata code that I have tried:

eststo clear
sysuse auto, clear
eststo Dom: estpost sum rep78 mpg turn trunk weight length if foreign==0
eststo For: estpost sum rep78 mpg turn trunk weight length if foreign==1
esttab Dom For, cells("mean(fmt(2))" "sd") ///
    nonumber nodepvars noobs se collabels(none) mlabels(, lhs("Var") title)

Below is also the output:

--------------------------------------
Var                   Dom          For
--------------------------------------
rep78                3.02         4.29
                     0.84         0.72
mpg                 19.83        24.77
                     4.74         6.61
turn                41.44        35.41
                     3.97         1.50
trunk               14.75        11.41
                     4.31         3.22
weight            3317.12      2315.91
                   695.36       433.00
length             196.13       168.55
                    20.05        13.68
--------------------------------------

What this does is to compute the mean and standard deviation for several variables using summarize. This is done separately based on a condition (once for foreign observations and once for non-foreign observations).

The results, mean and standard deviation, are then displayed via esttab. I will ultimately want to get this in LaTeX, but this example shows what the result is in Stata for the sake of simplicity.

I have two questions:

  1. How can I get the standard deviations to be shown in parentheses?

  2. Is it possible to include any lines between the variables to separate the two different groups?

I have something like this in mind:

--------------------------------------
Var                   Dom          For
--------------------------------------
Variable Group 1:
--------------------------------------
rep78                3.02         4.29
                    (0.84)       (0.72)
mpg                 19.83        24.77
                    (4.74)       (6.61)
turn                41.44        35.41
                    (3.97)       (1.50)
--------------------------------------
Variable Group 2:
--------------------------------------
trunk               14.75        11.41
                   (4.31)       (3.22)
weight            3317.12      2315.91
                 (695.36)      (433.00)
length             196.13       168.55
                  (20.05)       (13.68)
--------------------------------------

I would like to use eststo, etc. if possible. I would prefer that it be as automated as possible, but I am open to exporting matrices from Stata into LaTeX or using fragments if that is what it takes. If this is not possible, I am also open to other solutions.


Solution

  • Regarding the first question you need to specify option par in sd within cells():

    sysuse auto, clear
    
    eststo clear
    
    eststo Dom: estpost sum rep78 mpg turn trunk weight length if foreign==0
    eststo For: estpost sum rep78 mpg turn trunk weight length if foreign==1
    esttab Dom For, cells("mean(fmt(2))" "sd(par)") ///
        nonumber nodepvars noobs se collabels(none) mlabels(, lhs("Var") title)
    

    With regards to the second question, you could do the following:

    eststo clear
    
    eststo Dom: estpost sum rep78 mpg turn if foreign==0
    eststo For: estpost sum rep78 mpg turn if foreign==1
    esttab Dom For using output.txt, cells("mean(fmt(2))" "sd(par)") ///
        nonumber nodepvars noobs collabels(none) mlabels(, lhs("Vars") title) ///
        posthead("@hline" "Variable Group 1:" "@hline" ) postfoot(" ") replace
    
    eststo clear
    
    eststo Dom: estpost sum trunk weight length if foreign==0
    eststo For: estpost sum trunk weight length if foreign==1
    esttab Dom For using output.txt, cells("mean(fmt(2))" "sd(par)") ///
        nonumber nodepvars noobs collabels(none) mlabels(none)  ///
        prehead("@hline" "Variable Group 2:") append
    

    This will produce the desired output:

    type output.txt
    
    --------------------------------------
    Vars                  Dom          For
    --------------------------------------
    Variable Group 1:
    --------------------------------------
    rep78                3.02         4.29
                       (0.84)       (0.72)
    mpg                 19.83        24.77
                       (4.74)       (6.61)
    turn                41.44        35.41
                       (3.97)       (1.50)
    
    --------------------------------------
    Variable Group 2:
    --------------------------------------
    trunk               14.75        11.41
                       (4.31)       (3.22)
    weight            3317.12      2315.91
                     (695.36)     (433.00)
    length             196.13       168.55
                      (20.05)      (13.68)
    --------------------------------------