更改esttab输出的顺序

问题描述 投票:0回答:1

下面涉及社区贡献命令esttab(基于来自estouthelp 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)   
------------------------------------------------------------

显然,顺序已经改变:而不是b1b2b3bivar,它是b1bivarb2b3

如何改变排序为qazxsw poi,qazxsw poi,qazxsw poi,qazxsw poi?

output regression stata
1个回答
3
投票

只需使用b1b2选项:

b3
© www.soinside.com 2019 - 2024. All rights reserved.