Announcement

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

  • Margins and marginsplot after gsem

    This is my first Statalist post. I’m trying to use marginsplot to graph predicted probabilities after using gsem to estimate a multinomial, two-level logistic regression with random intercepts only. Specifically, I’m trying to visually compare the probabilities for two outcomes (other than the base outcome) in response to the same continuous predictor.

    First, can someone point me to a good example for how to do something like this, showing the steps from gsem to margins to marginsplot?

    Second, I’m mainly getting stalled in margins, which just hangs until I break it. Is it normal for margins to take much more time to run than the gsem estimation?

    Also, does margins (and marginsplot) need the data to be in active memory, or can I use the commands after restoring the estimates from a previously saved gsem command? When I try to use them on restored estimates, I get this error message: “e(sample) does not identify the estimation sample.”
    • I’ve tried setting margins to noesample, but this seems to return all the predicted values as zeros.
    • I’ve tried setting e(sample) to include the variables in the model, but of course, there are “no variables defined” with restored estimates.
    Thank you in advance!

  • #2
    Hello Jiannbin Shiao, and welcome to the forum. I don't have time to delve into all of your questions right now, but I do have a question of my own: Why are you using -gsem- to estimate a two-level logistic regression model? It seems to me it would be easier to use -melogit-.

    Code:
    help melogit
    Good luck with your work.
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 18.5 (Windows)

    Comment


    • #3
      Hi Bruce, thanks for the reply! Isn't melogit for binomial outcomes? I thought gsem was my only option for estimating a multilevel, multinomial logistic regression model.

      Best,
      J

      Comment


      • #4
        Ah, sorry. I missed the word multinomial when I first read your post.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment


        • #5
          margins after gsem should work like most other estimation
          commands.

          margins does require data since it uses predict to compute
          predictive margins and marginal effects.

          margins default behavior for gsem with a multinomial
          outcome is to compute predictive margins for each level of the outcome
          variable. It also computes standard errors via the delta method, which
          means it must compute derivatives of the predictions with respect to
          each coefficient in the model. At this time, these derivatives are
          computed numerically for gsem results, thus they are slow for
          margins after gsem compared to after mlogit where
          analytical derivatives have been coded to speed things up.

          Use option predict() to focus on a single prediction, this will speed
          things up for you and make results easier to interpret.

          margins option predict() is repeatable, so you can compare
          the specific predictive margins without having to wait for results from the
          whole set of outcome levels.

          Use option nose while working your way through which margins results
          you want to graph. Option nose prevents margins from
          computing standard errors; this makes the margins estimation much
          faster (no need to compute derivatives).

          Once you have decided on the predictive margins of interest, remove option nose
          if you want marginsplot to plot the confidence bands.

          The following examples use the data from Example 41g in [SEM].
          Code:
          webuse gsem_lineup
          Here is a simple cross-seciontal example (think mlogit).
          Code:
          gsem i.chosen <- i.location
          * default is predicted probabilities for each outcome
          margins, nose
          marginsplot, name(fit1_default)
          * focus on a single prediction
          margins, nose predict(pr outcome(2.chosen))
          marginsplot, name(fit1_chosen2)
          * predictive margins for levels of location
          margins location, nose predict(pr outcome(2.chosen))
          marginsplot, name(fit1_loc_chosen2)
          * predictive margins for levels of location, for each outcome
          margins location, nose
          marginsplot, name(fit1_loc_default)
          Here is the same thing, except now we are working with a 2-level model.
          Code:
          gsem i.chosen <- i.location L[suspect]@1
          * default is predicted probabilities for each outcome
          margins, nose
          marginsplot, name(fit2_default)
          * focus on a single prediction
          margins, nose predict(pr outcome(2.chosen))
          marginsplot, name(fit2_chosen2)
          * predictive margins for levels of location
          margins location, nose predict(pr outcome(2.chosen))
          marginsplot, name(fit2_loc_chosen2)
          * predictive margins for levels of location, for each outcome
          margins location, nose
          marginsplot, name(fit2_loc_default)

          Comment


          • #6
            Jeff, this is fantastic! Thank you!

            Comment

            Working...
            X