benjann / estout

Stata module to make regression tables
http://repec.sowi.unibe.ch/stata/estout/index.html
MIT License
71 stars 18 forks source link

unstack problem when output single-equation & multi-equation together #42

Closed dreistein543 closed 2 years ago

dreistein543 commented 2 years ago

sometimes we need to combine summarize table and correlation table. so I tried code as below

sysuse auto, clear
eststo tabsum: estpost sum price mpg weight
eststo tabcor: estpost corr price mpg weight, matrix

esttab tabsum tabcor, not nomtitle noobs nonumber legend eqlabel("",none) ///
      cells((mean(pattern(1 0) fmt(3) ) ///
               sd(pattern(1 0) fmt(3) ) ///
                b(pattern(0 1) fmt(4) star) ///
))

// which gives me something like
------------------------------------------------------
                     mean           sd            b   
------------------------------------------------------
price            6165.257     2949.496       1.0000   
mpg                21.297        5.786      -0.4686***
weight           3019.459      777.194       0.5386***
mpg                                          1.0000   
weight                                      -0.8072***
weight                                       1.0000   
------------------------------------------------------

// then I add unstack to separate e(b) content

esttab tabsum tabcor, not nomtitle noobs nonumber legend eqlabel("",none) ///
      cells((mean(pattern(1 0) fmt(3) ) ///
               sd(pattern(1 0) fmt(3) ) ///
                b(pattern(0 1) fmt(4) star) ///
)) unstack

// then it gives me something as below. the correlation matrix goes missing
--------------------------------------
                     mean           sd
--------------------------------------
price            6165.257     2949.496
mpg                21.297        5.786
weight           3019.459      777.194
--------------------------------------
* p<0.05, ** p<0.01, *** p<0.001
dreistein543 commented 2 years ago

seems @benjann has answered a similar question in Statalist. But this seems not an elegant solution.

I wander if there is a more simple way to combine these 2 table (i.e. something like code I list above) since estout has put out many versions during these years. Here is my reason:

if I use these 2 method, the correlation matrix won't mark with stars since they just post e(b) matrix

benjann commented 2 years ago

I fixed this in the 25mar2022 update. If you update estout on your system, the table should come out fine:

sysuse auto, clear
eststo tabsum: estpost sum price mpg weight
eststo tabcor: estpost corr price mpg weight, matrix

esttab tabsum tabcor, not nomtitle noobs nonumber legend ///
      cells((mean(pattern(1 0) fmt(3) ) ///
               sd(pattern(1 0) fmt(3) ) ///
                b(pattern(0 1) fmt(4) star) ///
)) unstack

--------------------------------------------------------------------------------------
                        _                     price             mpg          weight   
                     mean           sd            b               b               b   
--------------------------------------------------------------------------------------
price            6165.257     2949.496       1.0000                                   
mpg                21.297        5.786      -0.4686***       1.0000                   
weight           3019.459      777.194       0.5386***      -0.8072***       1.0000   
--------------------------------------------------------------------------------------
* p<0.05, ** p<0.01, *** p<0.001

Getting good labels for the columns will be tricky, however. My suggestion would be to store the mean and sd differently so that they are both available as e(b). Things are probably easier to handle if mean and sd are separate estimation sets. Maybe about as follows:

sysuse auto, clear
estpost tabstat price mpg weight, s(mean) columns(statistics)
mat mean = e(mean)
mat coleq mean = "mean"
estadd matrix mean, replace
eststo mean
estpost tabstat price mpg weight, s(sd) columns(statistics)
mat sd = e(sd)
mat coleq sd = "sd"
estadd matrix sd, replace
eststo sd
estpost corr price mpg weight, matrix
eststo cor
esttab mean sd cor, not noobs nonumber legend ///
    cells((mean(pattern(1 0 0) fmt(3) ) ///
             sd(pattern(0 1 0) fmt(3) ) ///
              b(pattern(0 0 1) fmt(4) star) ///
)) unstack collab(none) mti(Sum "" Corr)
--------------------------------------------------------------------------------------
                      Sum                      Corr                                   
                     mean           sd        price             mpg          weight   
--------------------------------------------------------------------------------------
price            6165.257     2949.496       1.0000                                   
mpg                21.297        5.786      -0.4686***       1.0000                   
weight           3019.459      777.194       0.5386***      -0.8072***       1.0000   
--------------------------------------------------------------------------------------
* p<0.05, ** p<0.01, *** p<0.001

end

dreistein543 commented 2 years ago

such an elegant solution! thank you Ben