Announcement

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

  • Feature added to cmp: computing margins of probabilities of certain outcomes

    Several times I've seen or received queries about how, after -cmp-, to run -margins- on the probability of certain outcomes, such as (1,1) from a bivariate probit model. It turns out that this already was possible for some models, using a sort of trick. But the trick was undocumented and it would crash for ordered-probit and non-alternative-specific multinomial probit models. The latest version of the cmp package documents the trick and makes it work for pretty much all models. Install with "ssc install cmp, replace".

    Somewhat unusually I think, you can predict the observation-level log likelihood after fitting with cmp, using "predict XXX, lnl". So to get marginal effects on the probability of given outcomes, you run the estimate, preserve the data set, modify the outcome variables to represent the outcome of interest, run -margins- to get marginal effects on the exponential of the log likelihood, then restore the original data. The process is fairly intuitive, if inelegant. It is slow because margins calls cmp's predict command many times, and each time cmp sets up the entire estimation problem again. But it works. Here are some examples that illustrate by replicating output from -biprobit- and -mprobit-:

    Code:
    cmp setup
    
    webuse laborsup
    gen byte anykids = kids > 0
    biprobit (anykids = fem_inc male_educ) (fem_work = male_educ)
    margins, dydx(fem_inc) predict(p11) // average marginal impact of fem_inc on probability of (1,1)
    cmp (anykids = fem_inc male_educ) (fem_work = male_educ), ind($cmp_probit $cmp_probit)
    preserve
    replace anykids=1
    replace fem_work=1
    margins, dydx(fem_inc) expression(exp(predict(lnl))) force
    restore
    
    webuse sysdsn3
    mprobit insure age male nonwhite site2 site3
    margins, dydx(nonwhite) predict(outcome(2)) // average marginal impact of nonwhite on probability of outcome 2
    cmp (insure = age male nonwhite site2 site3, iia), nolr ind($cmp_mprobit) qui
    preserve
    replace insure = 2
    margins, dydx(nonwhite) expression(exp(predict(lnl))) force
    restore

  • #2
    Hi David
    Thanks for putting up the update.

    I am wanting to use this for a mprobit with random effects estimation which I am using cmp for. I received the following error when I attempted to run it though:
    "Observation-level likelihoods not defined for random effects/coefficient models. e(ll) holds the overall log-likelihood"
    after using the replace script described in the help file and using the script
    "margins, dydx(*) expression(exp(predict(lnl))) force"

    In your description you mention that the trick works for "pretty much all models".
    Is the random effects an instance? If it is, is there anything that can be done?
    Thanks
    Paul

    Comment


    • #3
      Oh that is a very good point. Yes, at the moment, this will not work for hierarchical models. Sorry about that.

      Comment


      • #4
        David Roodman

        Dear David,

        If your example, there is
        Code:
        margins, dydx(nonwhite) predict(outcome(2))
        But this just reports marginal effect of nonwhite on 2nd outcome. But how can I make it report marginal effect of nonwhite on every outcome in one shot? I tried
        Code:
        margins, dydx(nonwhite) predict(outcome(1) outcome(2) outcome(3))
        but it didn't work.

        Comment


        • #5
          The code you quote from my example is executed after the mprobit command, which I did not write (it comes with Stata). So I suggest you check the Stata documentation for mprobit and margins.

          Comment


          • #6
            David Roodman

            Thank you. For mprobit, I learned that, from Stata version 15, I can just run
            Code:
             
             margins, dydx(nonwhite)
            and marginal effects for all outcomes will be reported.

            If I want to get marginal effects in one shock for cmp, what should I run?

            Comment


            • #7
              The technique documented here could only work for one outcome at a time because it temporarily modifies the outcome variable to be constant, equal to the chosen outcome of interest. cmp is a general-purpose command, so the cost sometimes is that doesn't offer as many features specific to a given model.

              Comment


              • #8
                David Roodman

                Thank you! cmp is awesome anyways.

                Comment


                • #9
                  Dear Statalists,

                  I am running a Multinomial probit model with heckman selection using "cmp" command created by David Roodman.
                  I tried to figure out margins
                  Code:
                  Code:
                  webuse womenwk, clear
                  recode wage 0/20 = 0 20/30 = 1 30/max = 2, generate(wage3)
                  gen selectvar = wage<.
                  cmp (wage3 = education age) (selectvar = married children education age), indicators(selectvar*$cmp_mprobit $cmp_probit) structural difficult nonrtolerance tech(dfp) ghkdraws(20)
                  preserve
                  replace wage3 = 1
                  margins, dydx(*) predict(pr)  force
                  restore
                  ranging wage3 from 1 to 3
                  but I am getting the very same probabilities
                  I wonder what is wrong
                  Thanks for your help

                  Comment


                  • #10
                    Yeah the technique I explained in this post won't work for multinomial probit because when you set wage3=1, cmp thinks this is a multinomial probit model with only one outcome.

                    But good news I think: In June 2018, version 8.2.0, I added the ability to predict multinomial probabilities, and marginal effects on them, more directly. See the first mprobit examples in the help file.

                    Comment


                    • #11
                      Thank you very much David!
                      I really appreciate the work you are doing to always improve this command.

                      Comment


                      • #12
                        Quick Q:
                        How do you compute and report "p11 minus p10" (estimate, standard errors, and p-value) in your bivariate probit scenario?

                        Code:
                        cmp (anykids = fem_work male_educ) (fem_work = male_educ instrument), ind($cmp_probit  $cmp_probit)
                        
                        //marginal effect on probability of (1,1)
                        preserve
                        replace anykids=1
                        replace fem_work=1
                        margins, dydx(fem_work) expression(exp(predict(lnl))) force restore
                        matrix a= r(b)
                        restore
                        
                        //marginal effect on probability of (1,0)
                        preserve
                        replace anykids=1
                        replace fem_work=0
                        margins, dydx(fem_work) expression(exp(predict(lnl))) force restore
                        matrix b = r(b)
                        restore
                        
                        //partial marginal effect is the effect of a change in fem_work from 0 to 1. That is, P11 - P10.
                        matrix c = a-b
                        matrix list c
                        Thanks!

                        Comment

                        Working...
                        X