我想使用分类暴露变量进行线性回归,并将结果输出到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
答案 0 :(得分:2)
如果您将exp
中的adjusted
更改为post temp
,则会得到想要的:
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 |
+----------------------------------------------------------------------------+
编辑:
您还需要将str
中的exp
长度更改为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 |
+--------------------------------------------------------------------------------------+
答案 1 :(得分:2)
如果您想知道与每个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 |
+--------------------------------------------------+