在模型结果中保存多项结果的类别名称
Saving category names of multinomial outcome in model results
我在 Stata 中 运行 多项逻辑回归模型并使用以下代码导出结果:
sysuse auto.dta, clear
describe
summ
postfile temp str100 exp str40 outcome adjust N beta se lci uci pval using ///
"\test_multinom_models.dta", replace
foreach out in rep78 {
foreach exp in price {
foreach adjust in 1 2{
if `adjust'==1 local adjusted ""
if `adjust'==2 local adjusted "trunk"
xi: mlogit `out' `exp' `adjusted', rrr
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_multinom_models.dta", clear
export excel using "\test_multinom_models.xls", firstrow(variables) replace
但是,结果文件没有显示哪个估计与哪个类别的结果相关。
如何将此信息添加到结果文件中?
只需使用:
local matnames: colfullnames table
而不是:
local matnames: colnames table
结果:
. list exp beta se
+---------------------------------+
| exp beta se |
|---------------------------------|
1. | 1:_cons 1.124016 3.845891 |
2. | 1:_cons 61.51294 252.5805 |
3. | 1:price .9997586 .0006926 |
4. | 1:price .9994512 .000728 |
5. | 1:trunk .6101818 .1825584 |
|---------------------------------|
6. | 2:_cons .5509481 .867367 |
7. | 2:_cons .3740141 .3564033 |
8. | 2:price .9999453 .0001431 |
9. | 2:price .9999547 .0001537 |
10. | 2:trunk .9706588 .099808 |
|---------------------------------|
11. | 3:o._cons 1 . |
12. | 3:o._cons 1 . |
13. | 3:o.price 1 . |
14. | 3:o.price 1 . |
15. | 3:o.trunk 1 . |
|---------------------------------|
16. | 4:_cons 2.724945 3.140763 |
17. | 4:_cons .775281 .54465 |
18. | 4:price .9999589 .0001027 |
19. | 4:price 1.000004 .0001082 |
20. | 4:trunk .8986906 .0705406 |
|---------------------------------|
21. | 5:price .9999378 .0001279 |
22. | 5:price 1.000051 .0001324 |
23. | 5:trunk .7821434 .0803135 |
+---------------------------------+
我在 Stata 中 运行 多项逻辑回归模型并使用以下代码导出结果:
sysuse auto.dta, clear
describe
summ
postfile temp str100 exp str40 outcome adjust N beta se lci uci pval using ///
"\test_multinom_models.dta", replace
foreach out in rep78 {
foreach exp in price {
foreach adjust in 1 2{
if `adjust'==1 local adjusted ""
if `adjust'==2 local adjusted "trunk"
xi: mlogit `out' `exp' `adjusted', rrr
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_multinom_models.dta", clear
export excel using "\test_multinom_models.xls", firstrow(variables) replace
但是,结果文件没有显示哪个估计与哪个类别的结果相关。
如何将此信息添加到结果文件中?
只需使用:
local matnames: colfullnames table
而不是:
local matnames: colnames table
结果:
. list exp beta se
+---------------------------------+
| exp beta se |
|---------------------------------|
1. | 1:_cons 1.124016 3.845891 |
2. | 1:_cons 61.51294 252.5805 |
3. | 1:price .9997586 .0006926 |
4. | 1:price .9994512 .000728 |
5. | 1:trunk .6101818 .1825584 |
|---------------------------------|
6. | 2:_cons .5509481 .867367 |
7. | 2:_cons .3740141 .3564033 |
8. | 2:price .9999453 .0001431 |
9. | 2:price .9999547 .0001537 |
10. | 2:trunk .9706588 .099808 |
|---------------------------------|
11. | 3:o._cons 1 . |
12. | 3:o._cons 1 . |
13. | 3:o.price 1 . |
14. | 3:o.price 1 . |
15. | 3:o.trunk 1 . |
|---------------------------------|
16. | 4:_cons 2.724945 3.140763 |
17. | 4:_cons .775281 .54465 |
18. | 4:price .9999589 .0001027 |
19. | 4:price 1.000004 .0001082 |
20. | 4:trunk .8986906 .0705406 |
|---------------------------------|
21. | 5:price .9999378 .0001279 |
22. | 5:price 1.000051 .0001324 |
23. | 5:trunk .7821434 .0803135 |
+---------------------------------+