导出带有分类暴露变量的回归结果

Export regression results with categorical exposure variables

我想 运行 使用分类曝光变量进行几个线性回归,并将结果输出到 excel sheet。

下面的代码在连续曝光时工作正常。但是,对于分类曝光,代码仅输出第一行结果,而不是所有曝光级别。

*Code which works for continuous exposures

sysuse auto.dta
describe 
summ

postfile temp str40 exp str40 outcome adjusted N beta se lci uci pval using ///
"\test.dta", replace

foreach out in price weight {
    foreach exp in i.rep78 {
        foreach adjust in 1 {

            if `adjust'==1 local adjusted "mpg" 

            xi: reg `out' `exp' `adjusted'  
            local N = e(N) 
            matrix table=r(table)
            local beta = table[1,1]
            local se = table[2,1]
            local lci = table[5,1]
            local uci = table[6,1]
            local pval=table[4,1]
            post temp ("`out'") ("`exp'") (`adjusted') (`N') (`beta') (`se') /// 
                      (`lci') (`uci') (`pval')
        }
    }
}

postclose temp 

use "\test.dta", clear
export excel using "\test.xlsx", firstrow(variables)

上面的代码只生成一行,其中包含 rep78 第一级的估计值,而它应该生成 4 行(rep78 是一个 5 级分类变量)。

您需要修改代码以保存 r(table) 所有相关列的结果:

. reg price i.rep78

. matrix list r(table)

r(table)[9,6]
                1b.          2.          3.          4.          5.            
             rep78       rep78       rep78       rep78       rep78       _cons
     b           0    1403.125   1864.7333        1507      1348.5      4564.5
    se           .   2356.0851   2176.4582   2221.3383   2290.9272   2107.3466
     t           .    .5955324   .85677426   .67841985   .58862629    2.165994
pvalue           .   .55358783   .39476643   .49995129   .55818378   .03404352
    ll           .   -3303.696  -2483.2417  -2930.6334  -3228.1533    354.5913
    ul           .    6109.946   6212.7083   5944.6334   5925.1533   8774.4087
    df          64          64          64          64          64          64
  crit   1.9977297   1.9977297   1.9977297   1.9977297   1.9977297   1.9977297
 eform           0           0           0           0           0           0

所以,在你的代码中,在 matrix table=r(table) 之后你需要有类似的东西:

forvalues i = 1 / `= colsof(r(table)) - 1' {    

    local beta = table[1,`i']
    local se = table[2,`i']
    local lci = table[5,`i']
    local uci = table[6,`i']
    local pval=table[4,`i']

    post temp ("`out'") ("`exp'") (`adjusted') (`N') (`beta') (`se') ///
              (`lci') (`uci') (`pval')

}

以下对我有用:

sysuse auto.dta, clear
describe 
summ

postfile temp str40 exp str40 outcome adjusted N beta se lci uci pval using ///
"test.dta", replace

foreach out in price weight {
    foreach exp in i.rep78 {
        foreach adjust in 1 {

            if `adjust'==1 local adjusted "mpg" 

            reg `out' `exp' `adjusted' 

            local N = e(N) 
            matrix table=r(table)

            forvalues i = 1 / `= colsof(r(table))-1' {

                local beta = table[1,`i']
                local se = table[2,`i']
                local lci = table[5,`i']
                local uci = table[6,`i']
                local pval=table[4,`i']                         

                post temp ("`out'") ("`exp'") (`adjusted') (`N') (`beta') ///
                          (`se') (`lci') (`uci') (`pval')

            }                           
        }
    }
}

postclose temp 

use "test.dta", clear