【问题标题】:Extract regression results with names of covariates提取带有协变量名称的回归结果
【发布时间】:2018-12-05 11:10:40
【问题描述】:

我想使用分类曝光变量运行线性回归,并将结果输出到 Excel 表中,每个协变量的名称都包含在结果旁边。

下面的Stata代码可以很好地导出结果:

sysuse auto.dta, clear
describe
summ

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

foreach out in price {
    foreach exp in i.foreign {
        foreach adjust in 1 2 {

            if `adjust'==1 local adjusted ""
            if `adjust'==2 local adjusted "mpg weight length displacement i.trunk"

            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 ("`exp'") ("`out'") (`adjust') (`N') (`beta') ///
                (`se') (`lci') (`uci') (`pval')

            }
        }
    }
}

postclose temp

use "test.dta", clear

但是,所有行都标记为i.foreign,因此很难知道哪些结果对应于其他协变量。

理想情况下,我希望有一列显示与结果对应的名称,即mpg, weight, length, displacement, i.trunk

【问题讨论】:

  • 谢谢,但这仍然没有显示每个协变量对应的结果,例如第 1-4 行应该说是外国的。第 5 行应该说 mpg,第 6 行应该说重量等?
  • 如果您想包含曝光,您只需在后期调整之前添加 exp。也就是说,目前还不清楚你想要什么。调整第 3 行和第 4 行。怎么可能只说foreign
  • 您还需要将exp 中的str 长度从40 更改为100 或更长。
  • 查看我修改后的答案。
  • 我需要能够知道与 beta 相关的变量。例如,第 5 行中的 beta 用于 mpg。您的解决方案目前未显示 beta 与哪个变量相关

标签: stata linear-regression stata-macros


【解决方案1】:

如果你想知道每个beta对应的矩阵列名:

sysuse auto.dta, clear
describe 
summ

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

foreach out in price {
foreach exp in i.foreign {
    foreach adjust in 1 2 {

        if `adjust'==1 local adjusted "" 
        if `adjust'==2 local adjusted "mpg weight length displacement i.trunk" 

        reg `out' `exp' `adjusted' 

        local N = e(N) 
        matrix table=r(table)
        local matnames: colnames table
        tokenize `matnames'

        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 ("``i''") ("`out'") (`adjust') (`N') (`beta') ///
                      (`se') (`lci') (`uci') (`pval')
        }                           
      }
   } 
}
postclose temp 

use "test.dta", clear

list exp outcome adjust N beta, separator(0)

     +--------------------------------------------------+
     |          exp   outcome   adjust    N        beta |
     |--------------------------------------------------|
  1. |   0b.foreign     price        1   74           0 |
  2. |    1.foreign     price        1   74    312.2587 |
  3. |   0b.foreign     price        2   74           0 |
  4. |    1.foreign     price        2   74    3152.553 |
  5. |          mpg     price        2   74   -9.723515 |
  6. |       weight     price        2   74    4.613294 |
  7. |       length     price        2   74   -92.95226 |
  8. | displacement     price        2   74    10.30914 |
  9. |     5b.trunk     price        2   74           0 |
 10. |      6.trunk     price        2   74    530.6144 |
 11. |      7.trunk     price        2   74   -245.4009 |
 12. |      8.trunk     price        2   74    1722.497 |
 13. |      9.trunk     price        2   74    368.6347 |
 14. |     10.trunk     price        2   74     355.778 |
 15. |     11.trunk     price        2   74   -229.7306 |
 16. |     12.trunk     price        2   74    2002.943 |
 17. |     13.trunk     price        2   74    47.29906 |
 18. |     14.trunk     price        2   74    1746.247 |
 19. |     15.trunk     price        2   74    1473.953 |
 20. |     16.trunk     price        2   74    115.0414 |
 21. |     17.trunk     price        2   74    319.3028 |
 22. |     18.trunk     price        2   74    2780.235 |
 23. |     20.trunk     price        2   74    142.0096 |
 24. |     21.trunk     price        2   74    737.9046 |
 25. |     22.trunk     price        2   74    408.4962 |
 26. |     23.trunk     price        2   74   -669.1454 |
     +--------------------------------------------------+

【讨论】:

  • 完美!谢谢
【解决方案2】:

如果你将exp 中的adjusted 更改为adjusted,你会得到你想要的:

sysuse auto.dta, clear
describe 
summ

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

foreach out in price {
foreach exp in i.foreign {
    foreach adjust in 1 2 {

        if `adjust'==1 local adjusted "" 
        if `adjust'==2 local adjusted "mpg weight length displacement i.trunk" 

        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 ("`adjusted'") ("`out'") (`adjust') (`N') (`beta') ///
                      (`se') (`lci') (`uci') (`pval')
        }                           
      }
   } 
} 

postclose temp 

use "test.dta", clear

list exp outcome adjust N beta, separator(0)

     +----------------------------------------------------------------------------+
     |                                    exp   outcome   adjust    N        beta |
     |----------------------------------------------------------------------------|
  1. |                                            price        1   74           0 |
  2. |                                            price        1   74    312.2587 |
  3. | mpg weight length displacement i.trunk     price        2   74           0 |
  4. | mpg weight length displacement i.trunk     price        2   74    3152.553 |
  5. | mpg weight length displacement i.trunk     price        2   74   -9.723515 |
  6. | mpg weight length displacement i.trunk     price        2   74    4.613294 |
  7. | mpg weight length displacement i.trunk     price        2   74   -92.95226 |
  8. | mpg weight length displacement i.trunk     price        2   74    10.30914 |
  9. | mpg weight length displacement i.trunk     price        2   74           0 |
 10. | mpg weight length displacement i.trunk     price        2   74    530.6144 |
 11. | mpg weight length displacement i.trunk     price        2   74   -245.4009 |
 12. | mpg weight length displacement i.trunk     price        2   74    1722.497 |
 13. | mpg weight length displacement i.trunk     price        2   74    368.6347 |
 14. | mpg weight length displacement i.trunk     price        2   74     355.778 |
 15. | mpg weight length displacement i.trunk     price        2   74   -229.7306 |
 16. | mpg weight length displacement i.trunk     price        2   74    2002.943 |
 17. | mpg weight length displacement i.trunk     price        2   74    47.29906 |
 18. | mpg weight length displacement i.trunk     price        2   74    1746.247 |
 19. | mpg weight length displacement i.trunk     price        2   74    1473.953 |
 20. | mpg weight length displacement i.trunk     price        2   74    115.0414 |
 21. | mpg weight length displacement i.trunk     price        2   74    319.3028 |
 22. | mpg weight length displacement i.trunk     price        2   74    2780.235 |
 23. | mpg weight length displacement i.trunk     price        2   74    142.0096 |
 24. | mpg weight length displacement i.trunk     price        2   74    737.9046 |
 25. | mpg weight length displacement i.trunk     price        2   74    408.4962 |
 26. | mpg weight length displacement i.trunk     price        2   74   -669.1454 |
     +----------------------------------------------------------------------------+

编辑:

您还需要将exp 中的str 长度更改为100 或更长,以保存字符串中的所有协变量:

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

那么,如果您也将post temp 更改为也包括曝光:

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

你会得到想要的输出:

list exp outcome adjust N beta, separator(0)

     +--------------------------------------------------------------------------------------+
     |                                              exp   outcome   adjust    N        beta |
     |--------------------------------------------------------------------------------------|
  1. |                                        i.foreign     price        1   74           0 |
  2. |                                        i.foreign     price        1   74    312.2587 |
  3. | i.foreign mpg weight length displacement i.trunk     price        2   74           0 |
  4. | i.foreign mpg weight length displacement i.trunk     price        2   74    3152.553 |
  5. | i.foreign mpg weight length displacement i.trunk     price        2   74   -9.723515 |
  6. | i.foreign mpg weight length displacement i.trunk     price        2   74    4.613294 |
  7. | i.foreign mpg weight length displacement i.trunk     price        2   74   -92.95226 |
  8. | i.foreign mpg weight length displacement i.trunk     price        2   74    10.30914 |
  9. | i.foreign mpg weight length displacement i.trunk     price        2   74           0 |
 10. | i.foreign mpg weight length displacement i.trunk     price        2   74    530.6144 |
 11. | i.foreign mpg weight length displacement i.trunk     price        2   74   -245.4009 |
 12. | i.foreign mpg weight length displacement i.trunk     price        2   74    1722.497 |
 13. | i.foreign mpg weight length displacement i.trunk     price        2   74    368.6347 |
 14. | i.foreign mpg weight length displacement i.trunk     price        2   74     355.778 |
 15. | i.foreign mpg weight length displacement i.trunk     price        2   74   -229.7306 |
 16. | i.foreign mpg weight length displacement i.trunk     price        2   74    2002.943 |
 17. | i.foreign mpg weight length displacement i.trunk     price        2   74    47.29906 |
 18. | i.foreign mpg weight length displacement i.trunk     price        2   74    1746.247 |
 19. | i.foreign mpg weight length displacement i.trunk     price        2   74    1473.953 |
 20. | i.foreign mpg weight length displacement i.trunk     price        2   74    115.0414 |
 21. | i.foreign mpg weight length displacement i.trunk     price        2   74    319.3028 |
 22. | i.foreign mpg weight length displacement i.trunk     price        2   74    2780.235 |
 23. | i.foreign mpg weight length displacement i.trunk     price        2   74    142.0096 |
 24. | i.foreign mpg weight length displacement i.trunk     price        2   74    737.9046 |
 25. | i.foreign mpg weight length displacement i.trunk     price        2   74    408.4962 |
 26. | i.foreign mpg weight length displacement i.trunk     price        2   74   -669.1454 |
     +--------------------------------------------------------------------------------------+

【讨论】:

  • 这非常接近,但仍然不太正确。第 3 行和第 4 行中的 exp 应该是 i.foreign。第 5 行应该是 mpg,第 6 行应该是重量。第 7 行应该是长度。第 8 行应该是位移
  • 查看我的其他答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 2021-08-07
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多