FooBar
FooBar

Reputation: 16528

Twoway estpost tabstat-esttab: retain variable labels

I want to describe characteristics of cars by origin, and retrieve the result as latex table:

sysuse auto 
estpost tabstat price trunk, by(foreign) statistics(mean sd) columns(statistics) listwise nototal 
esttab using test.txt, main(mean) aux(sd)

Already after the estpost I can feel that the labels are going missing: it correctly displays the value labels "Domestic" and "Foreign", but simply lists the variables as "price" and "trunk" instead of "Price" and "Trunk space".

I have seen this problem on the internet, but no solution is satisfactory. Some suggest fsum, but that doesn't really allow latex, and also no cross-tabulation (means of x by category y). How can I fix this?

I automatized the accepted's answer as follows:

local varlabels
foreach var in price trunk {
        local varlabels `"`varlabels' `var' "`:variable label `var''""'       
}

Upvotes: 3

Views: 1604

Answers (1)

Wouter
Wouter

Reputation: 3271

The varlabels option allows you to add custom labels. After the estpost command the names of the estimates look like this:

. mat l e(mean)

e(mean)[1,4]
       Domestic:  Domestic:   Foreign:   Foreign:
          price      trunk      price      trunk
mean  6072.4231      14.75  6384.6818  11.409091

You can replace these names with variable labels with the addition of some code:

sysuse auto 
estpost tabstat price trunk, by(foreign) statistics(mean sd) columns(statistics) listwise nototal 

foreach name in `:colfullnames e(mean)' {
    foreach var in price trunk {
        if strpos("`name'", "`var'") > 0 {
            local varlabels `"`varlabels' `name' "`:variable label `var''""'
        }
    }
}

di `"`varlabels'"'
esttab, main(mean) aux(sd) varlabels(`varlabels')

Result:

. di `"`varlabels'"'
 Domestic:price "Price" Domestic:trunk "Trunk space (cu. ft.)" Foreign:price "Price" Foreign:trunk "Trunk space (cu. ft.)"

. esttab, main(mean) aux(sd) varlabels(`varlabels')

----------------------------
                      (1)   
                            
----------------------------
Domestic                    
Price              6072.4   
                 (3097.1)   
Trunk.. ft.)        14.75   
                  (4.306)   
----------------------------
Foreign                     
Price              6384.7   
                 (2621.9)   
Trunk.. ft.)        11.41   
                  (3.217)   
----------------------------
N                      74   
----------------------------
mean coefficients; sd in parentheses
* p<0.05, ** p<0.01, *** p<0.001

When adding option unstack to esttab, Domestic and Foreign are used as column names and the names of the coefficients are only price and trunk, so you could do something like this:

esttab, main(mean) aux(sd) unstack varlabels(price "`:variable label price'" trunk "`:variable label trunk'")

Upvotes: 2

Related Questions