Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Margins of specified expression with multiple outcomes?

    Please consider the following contrived example (from the Manual [https://www.stata.com/manuals/rmargins.pdf#page=23]).
    Code:
    use https://www.stata-press.com/data/r17/margex, clear
    mlogit group i.sex age
    One can obtain margins for multiple outcomes simultaneously
    Code:
    margins sex, predict(outcome(1)) predict(outcome(3))
    And one can obtain margins for a specific expression involving one outcome
    Code:
    margins sex, expression(invlogit(predict(outcome(1))))
    But if you fold multiple predict() options inside of the expression() option Stata seems to throw a code. This next line returns a r(111) error that reads "__marg_pvar_1__marg_pvar_2 not found"
    Code:
    margins sex, exp(invlogit(predict(outcome(1)) predict(outcome(3))))
    How does one obtain margins of a specified expression with multiple outcomes? Is it possible to specify the expression() option so that you can obtain margins for multiple outcomes simultaneously?

    Thank you for your time, consideration, and help.

  • #2
    Hi Konrad,
    Yes its possible, but you need to "hack" margins a bit. Basically you could write your own "predict" program that does what you want. And then call it all with predict.
    For example:
    Code:
     program mymlogit_p
        syntax newvarname [if] [in], [p_invout1 p_outcome3 *]
        marksample touse, novarlist
        if "`p_invout1'"!="" {
             tempvar v2 v3
            _predict double `v2' if `touse' , eq(2)
             _predict double `v3' if `touse' , eq(3)
            gen `typlist' `varlist' = invlogit(1/(1+exp(`v2')+exp(`v3'))) if `touse'
         }
         else if "`p_outcome3'"!="" {
             tempvar v2 v3
            _predict double `v2' if `touse' , eq(2)
             _predict double `v3' if `touse' , eq(3)
            gen `typlist' `varlist' = exp(`v3')/(1+exp(`v2')+exp(`v3')) if `touse'
         }
         else if "`options'"!="" {
                mlogit_p `0'
                exit
         }
    
    end
    program adde, eclass
        ereturn `0'
    end
    
    mlogit group i.sex age
    adde local predict mymlogit_p
    
    ** To show that it produces the same
    margins sex,  exp(predict(outcome(3)))
    margins sex,  predict(p_outcome3)
    margins sex, exp(invlogit(predict(outcome(1)))) 
    margins sex,  predict(p_invout1)
    
    ** At the same time
    margins sex,  predict(p_invout1) predict(p_outcome3)
    HTH

    Comment

    Working...
    X