Search code examples
statalogistic-regressionmultinomialstata-macros

Saving category names of multinomial outcome in model results


I run multinomial logistic regression models in Stata and export the results using the code below:

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

However, the results file does not show which estimate relates to which category of the outcome.

How can i add this information to the results file?


Solution

  • Simply use:

    local matnames: colfullnames table
    

    instead of:

    local matnames: colnames table
    

    Results:

    . 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 |
         +---------------------------------+