Search code examples
outputregressionstata

Change ordering of esttab output


The solution below involving the community-contributed command esttab (based on code from estout's help file), provides a way to show coefficients from different regressions in the same row.

However, the ordering is unexpected. How can I fix this?

First, I define a program appendmodels:

capt prog drop appendmodels
program appendmodels, eclass
// using first equation of model
syntax namelist
     tempname b V tmp
     foreach name of local namelist {
         qui est restore `name'
         mat `tmp' = e(b)
         local eq1: coleq `tmp'
         gettoken eq1 : eq1
         mat `tmp' = `tmp'[1,"`eq1':"]
         local cons = colnumb(`tmp',"_cons")
         if `cons'<. & `cons'>1 {
             mat `tmp' = `tmp'[1,1..`cons'-1]
         }
         mat `b' = nullmat(`b') , `tmp'
         mat `tmp' = e(V)
         mat `tmp' = `tmp'["`eq1':","`eq1':"]
         if `cons'<. & `cons'>1 {
             mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
         }
         capt confirm matrix `V'
         if _rc {
             mat `V' = `tmp'
        }
        else {
             mat `V' = ///
            ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
           ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
         }
     }
     local names: colfullnames `b'
     mat coln `V' = `names'
     mat rown `V' = `names'
     eret post `b' `V'
     eret local cmd "whatever"
end

Then I run regressions and use the program to combine the results of these regressions into one:

eststo clear
sysuse auto, clear

eststo b1: regress price weight
eststo b2: regress price mpg
eststo b3: regress price foreign
eststo bivar: appendmodels b1 b2 b3

Then I transpose them:

esttab b1 b2 b3 bivar, se nostar noconstant

matrix C = r(coefs)

eststo clear

local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
     local ++i
     local j 0
     capture matrix drop b
     capture matrix drop se
     foreach model of local models {
         local ++j
         matrix tmp = C[`i', 2*`j'-1]
         if tmp[1,1]<. {
             matrix colnames tmp = `model'
             matrix b = nullmat(b), tmp
             matrix tmp[1,1] = C[`i', 2*`j']
             matrix se = nullmat(se), tmp
         }
     }
     ereturn post b
     quietly estadd matrix se
     eststo `name'
 }

esttab, se mtitle noobs

Result:

------------------------------------------------------------
                      (1)             (2)             (3)   
                   weight             mpg         foreign   
------------------------------------------------------------
b1                  2.044***                                
                  (0.377)                                   

bivar               2.044***       -238.9***        312.3   
                  (0.377)         (53.08)         (754.4)   

b2                                 -238.9***                
                                  (53.08)                   

b3                                                  312.3   
                                                  (754.4)   
------------------------------------------------------------

Clearly, the ordering has changed: instead of b1, b2, b3, bivar, it is b1, bivar, b2, b3.

How can I change the ordering to be b1, b2, b3, bivar?


Solution

  • Just use the order() option of esttab:

    esttab, se mtitle noobs order(b?)
    
    
    ------------------------------------------------------------
                          (1)             (2)             (3)   
                       weight             mpg         foreign   
    ------------------------------------------------------------
    b1                  2.044***                                
                      (0.377)                                   
    
    b2                                 -238.9***                
                                      (53.08)                   
    
    b3                                                  312.3   
                                                      (754.4)   
    
    bivar               2.044***       -238.9***        312.3   
                      (0.377)         (53.08)         (754.4)   
    ------------------------------------------------------------
    Standard errors in parentheses
    * p<0.05, ** p<0.01, *** p<0.001