更改esttab输出的顺序

时间:2019-04-08 15:00:51

标签: output regression stata

以下涉及社区贡献命令esttab的解决方案(基于estout help file的代码)提供了一种显示系数的方法同一行中的不同回归。

但是,订购是意外的。我该如何解决?

首先,我定义一个程序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

然后我运行回归并使用该程序将这些回归的结果合并为一个:

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

然后我将它们移调:

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

结果:

------------------------------------------------------------
                      (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)   
------------------------------------------------------------

显然,顺序已更改:它是b1b2,而不是b3bivarb1bivarb2b3

如何更改顺序为b1b2b3bivar

1 个答案:

答案 0 :(得分:3)

只需使用order()的{​​{1}}选项:

esttab