首页 > 解决方案 > 更改 esttab 输出的顺序

问题描述

下面的解决方案涉及社区贡献的命令esttab(基于帮助文件estout中的代码),提供了一种在同一行中显示来自不同回归的系数的方法。

但是,订单出乎意料。我怎样才能解决这个问题?

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

显然,顺序发生了变化:不是b1, b2, b3, bivar, 而是b1, bivar, b2, b3

如何将排序更改为b1, b2, b3, bivar

标签: outputregressionstata

解决方案


只需使用以下order()选项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

推荐阅读