使用分类暴露变量导出回归结果

时间:2018-05-18 09:27:55

标签: stata linear-regression stata-macros

我想使用分类曝光变量运行几个线性回归,并将结果输出到Excel工作表。

当曝光持续时,下面的代码可以正常工作。但是,对于分类曝光,代码仅输出第一行结果而不是曝光量。

*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级分类变量)。

1 个答案:

答案 0 :(得分:2)

您需要修改代码以保存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