0
votes

I am using the Stata command esttab to convert some tabulated output to Latex form. I want to include total number of observations in each column in the last row of the table. I have run across an issue using the keep suboption for blist. Sometimes keep performs as expected. Sometimes it does nothing.

I found the following on statalist, which suggests to me it may have something to do with the difference between coeflist and varlist, but I don't think that is the issue here. http://www.statalist.org/forums/forum/general-stata-discussion/general/131816-using-factor-variables-in-estimates-table-keep

Here is a reproducible example.

clear all
sysuse auto

gen big=(weight>3000)
label define Big 0 "Not in Big Set" 1 "In Big Set"
label values big Big
label variable big "Is Item Found in Big Set?"
compress big  //want it to be byte to match my real data

estpost tabulate big foreign

esttab using myfilename.tex, replace f ///
cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
collabels(none) unstack noobs nonumber nomtitle    ///
eqlabels(, lhs("Size Category"))                     ///
varlabels(, blist(Total "\hline "))

estpost tabulate rep78 foreign

esttab using myfilename2.tex, replace f ///
cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
collabels(none) unstack noobs nonumber nomtitle    ///
eqlabels(, lhs("Repair Rec."))                     ///
varlabels(, blist(Total "\hline "))

The latex output looks like this in the first case:

Size Category&    Domestic&     Foreign&       Total\\
\hline
Not in Big Set&       28.85&       90.91&       47.30\\
            &        (15)&        (20)&        (35)\\
In Big Set  &       71.15&        9.09&       52.70\\
            &        (37)&         (2)&        (39)\\
\hline Total       &      100.00&      100.00&      100.00\\
            &        (52)&        (22)&        (74)\\

It looks like this (correct) in the second case:

Repair Rec. &    Domestic&     Foreign&       Total\\
\hline
1           &        4.17&        0.00&        2.90\\
2           &       16.67&        0.00&       11.59\\
3           &       56.25&       14.29&       43.48\\
4           &       18.75&       42.86&       26.09\\
5           &        4.17&       42.86&       15.94\\
\hline Total       &      100.00&      100.00&      100.00\\
        &        (48)&        (21)&        (69)\\

I also thought it may have something to do with my storage type, but adding recast int big before the first estpost didn't fix it.

Why is keep only working in one if these cases? How can it be fixed?

Or is there another workaround to achieve my goal of number of observations in each column appearing in the last row (labeling them "Number of observations" would be a bonus)?

2
Since I don't really know estout, I'll make this a comment: perhaps someone will see that there is no answer and will provide the definitive answer. Thanks to your excellent reproducible example, I was able to discover that, in the spirit of your attempt by recasting big as an int, removing the value label on big led to the first case now matching the second case. Good luck taking it from here! - user4690969
Thanks William! That is interesting! I can confirm that this does create my desired effect with keep. However, it leads to the issue that I now don't have reasonable row labels. I feel like this should be solvable with the eqlabels option, but I haven't figured it out yet. - Danielle
And of course I means varlabels - Danielle

2 Answers

1
votes

I pointed out this discussion to the developer of the esttab set of programs, since he's not active on Stack Overflow. He responded with the following, suggesting that I post it here on his behalf, which I'm glad to do to provide a full resolution to this problem.


This seems to be a bug. The element-specific keep() option - i.e. keep() within cells() - does not appear to work with coefficient names that contain spaces. A workaround is to use the elabels option with estpost tabulate. This prevents estpost tabulate from trying to construct coefficient names from the labels. Instead it uses the values and stores the labels in e(labels) and, in case of twoway tables, in e(eqlabels). These macros can then be used in estout to print the labels. Here is an example:

. clear all

. sysuse auto
(1978 Automobile Data)

. gen big=(weight>3000)

. label define Big 0 "Not in Big Set" 1 "In Big Set"

. label values big Big

. label variable big "Is Item Found in Big Set?"

. estpost tabulate big foreign, elabels

foreign      |                                            
         big |      e(b)     e(pct)  e(colpct)  e(rowpct)
-------------+--------------------------------------------
0            |                                            
           0 |        15   20.27027   28.84615   42.85714
           1 |        37         50   71.15385   94.87179
       Total |        52   70.27027        100   70.27027
-------------+--------------------------------------------
1            |                                            
           0 |        20   27.02703   90.90909   57.14286
           1 |         2   2.702703   9.090909   5.128205
       Total |        22   29.72973        100   29.72973
-------------+--------------------------------------------
Total        |                                            
           0 |        35    47.2973    47.2973        100
           1 |        39    52.7027    52.7027        100
       Total |        74        100        100        100

row labels saved in macro e(labels)
column labels saved in macro e(eqlabels)

. esttab, tex fragment ///
>     cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
>     collabels(none) unstack noobs nonumber nomtitle ///
>     eqlabels(`e(eqlabels)', lhs("Size Category")) ///
>     varlabels(`e(labels)', blist(Total "\hline "))

Size Category&    Domestic&     Foreign&       Total\\
\hline
Not in Big Set&       28.85&       90.91&       47.30\\
In Big Set  &       71.15&        9.09&       52.70\\
\hline Total       &      100.00&      100.00&      100.00\\
            &        (52)&        (22)&        (74)\\
0
votes

Well, thanks to William's good hint, I figured out this solution. I am still in the dark about WHY the original code doesn't work, but here is a way to get the desired output from keep and a workaround for the loss of the variable labels using varlabels...

clear all
sysuse auto

gen big=(weight>3000)
label variable big "Is Item Found in Big Set?"
compress big  //want it to be byte to match my real data

estpost tabulate big foreign

esttab using myfilename.tex, replace f ///
cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
collabels(none) unstack noobs nonumber nomtitle    ///
eqlabels(, lhs("Size Category"))                     ///
varlabels(0 "Not in Big Set" 1 "In Big Set", blist(Total "\hline "))

This produces:

Size Category&    Domestic&     Foreign&       Total\\
\hline
Not in Big Set&       28.85&       90.91&       47.30\\
In Big Set  &       71.15&        9.09&       52.70\\
\hline Total       &      100.00&      100.00&      100.00\\
            &        (52)&        (22)&        (74)\\