Announcement

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

  • margins command in gsem

    Dear Statalisters:

    I ran a gsem command and tried to get average marginal effects (AME). The issue is how combine two options in the margins command. The gsem command is as follows:

    . gsem (y1 <- i.v1 i.v2 i.v3 1.v4 agey, probit ///
    (y2 <- i.v1 i.v2 i.v3 agey, probit)

    To calculate AME, the “predict(fixed)” has to be in the command (or an error message will appear). Thus,

    . margins, dydx(*) predict(fixed)

    However, the result is for equation (y1). To get the result for equation (y2), Stata manual indicates that an option “predict(equation(y2))” is needed but I cannot to get “predict(fixed)” to work at the same time. The question is how to make the two option work together in margins command.

    I would greatly appreciate any advice.

    Thanks

    T.Y.

  • #2
    Try
    Code:
    margins, dydx(*) predict(outcome(y2) fixed)

    Comment


    • #3
      Jeff:

      Thank you for the advice. I tried the code you suggested but the Stata gave the error message as if I did not use "fixed". See below.

      . margins, dydx(*) predict(outcome(y2) fixed)
      prediction is a function of possibly stochastic quantities other than e(b)

      Please note that a latent variable was included in the gsem. The codes takes the form like

      . gsem (y1 <- i.v1 i.v2 i.v3 1.v4 agey L@1, probit ///
      (y2 <- i.v1 i.v2 i.v3 agey L, probit), var(L@1)

      Thanks

      T.Y.

      Comment


      • #4
        I cannot reproduce the error.

        Here is an example using data from example 45g in the [SEM] manual.
        Scroll down to the end to see the results from margins.
        Code:
        . webuse gsem_womenwk
        (Fictional data on women and work)
        
        . gen selected = !missing(wage)
        
        . gsem (selected <- married children educ age L@1, probit) (wage  <- educ age L)
        > , var(L@1)
        
        Fitting fixed-effects model:
        
        Iteration 0:   log likelihood =  -5218.266  
        Iteration 1:   log likelihood = -5208.9056  
        Iteration 2:   log likelihood = -5208.9038  
        Iteration 3:   log likelihood = -5208.9038  
        
        Refining starting values:
        
        Grid node 0:   log likelihood = -5229.3385
        
        Fitting full model:
        
        Iteration 0:   log likelihood = -5229.3385  
        Iteration 1:   log likelihood = -5213.9863  
        Iteration 2:   log likelihood = -5182.0484  
        Iteration 3:   log likelihood = -5178.4728  
        Iteration 4:   log likelihood = -5178.3076  
        Iteration 5:   log likelihood = -5178.3073  
        Iteration 6:   log likelihood = -5178.3069  
        Iteration 7:   log likelihood = -5178.3064  
        Iteration 8:   log likelihood =  -5178.306  
        Iteration 9:   log likelihood = -5178.3053  
        Iteration 10:  log likelihood =  -5178.305  
        Iteration 11:  log likelihood = -5178.3047  
        Iteration 12:  log likelihood = -5178.3046  
        
        Generalized structural equation model             Number of obs   =       2000
        Log likelihood = -5178.3046
        
         ( 1)  [selected]L = 1
         ( 2)  [var(L)]_cons = 1
        ------------------------------------------------------------------------------
                     |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
        selected <-  |
             married |    .629573   .0953115     6.61   0.000      .442766    .8163801
            children |    .620429   .0392907    15.79   0.000     .5434206    .6974374
                educ |   .0788174   .0151815     5.19   0.000     .0490622    .1085726
                 age |    .051633   .0058737     8.79   0.000     .0401208    .0631453
                   L |          1  (constrained)
               _cons |  -3.522863   .2677655   -13.16   0.000    -4.047674   -2.998052
        -------------+----------------------------------------------------------------
        wage <-      |
                educ |   .9899541   .0532565    18.59   0.000     .8855733    1.094335
                 age |     .21313   .0206031    10.34   0.000     .1727486    .2535113
                   L |   5.974205   .5645889    10.58   0.000     4.867631    7.080779
               _cons |   .4857517   1.077039     0.45   0.652    -1.625205    2.596709
        -------------+----------------------------------------------------------------
               var(L)|          1  (constrained)
        -------------+----------------------------------------------------------------
          var(e.wage)|   .3664683   5.183351                      3.35e-13    4.01e+11
        ------------------------------------------------------------------------------
        
        . margins, dydx(*) predict(outcome(wage) fixed)
        
        Average marginal effects                          Number of obs   =       2000
        Model VCE    : OIM
        
        Expression   : Predicted mean (wage (hourly)), fixed portion only,
                       predict(outcome(wage) fixed)
        dy/dx w.r.t. : married children educ age
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
             married |          0  (omitted)
            children |          0  (omitted)
                educ |   .9899541   .0532565    18.59   0.000     .8855733    1.094335
                 age |     .21313   .0206031    10.34   0.000     .1727486    .2535113
        ------------------------------------------------------------------------------
        If you can reproduce this with a dataset you can send to technical support that would
        be helpful.

        Comment


        • #5
          Dear Jeff,
          In the same example, I have two questions:

          how I can find two types of marginal probabilities? margins, at(married=1 children=0) in the first equation?

          Can we add covariation term between exogenous variables in gsem ?

          Thanks
          Last edited by Annika Magnusson; 05 Jun 2015, 09:34.

          Comment


          • #6
            Annika has 2 questions. The first is
            how I can find two types of marginal probabilities? margins, at(married=1 children=0) in the first equation?
            I'm not sure what the question is here. If Annika wants a marginal propbability for married=1 and a
            separate marginal probability for children=0 leaving all other variables as observed, then the syntax
            is
            Code:
            margins, at(married=1) at(children=0)
            For the second question
            Can we add covariation term between exogenous variables in gsem ?
            gsem treats all exogenous variables as fixed, meaning gsem does not attribute any distributional
            assumptions to them.

            Comment


            • #7
              Many thanks Jeff

              I have one more questions. Much appreciated if you can answer it.

              I would like to do marginal plot for interaction between race and low below, yet get the same error:
              default prediction is a function of possibly stochastic quantities other than e(b)

              Thanks
              Annika


              webuse lbw, clear

              option-1
              gsem (smoke<-race low ptl ftv bwt,logit) ///
              (ptl<-race low ftv,ologit)
              margins race, at (low=1) predict(outcome(smoke))
              marginsplot

              option-2
              gsem (smoke<-i.race##i.low ptl ftv bwt,logit) ///
              (ptl<-race low ftv,ologit)

              margins i.race##i.low





              Comment


              • #8
                The gsem specifications do not look correct for the calls to margins.
                I believe Annika meant something like

                Code:
                webuse lbw
                gsem    (smoke<-i.race i.low ptl ftv bwt,logit) ///
                        (ptl<-i.race i.low ftv,ologit)
                margins race, at(low=1) predict(outcome(smoke))
                marginsplot, name(one)
                Even this specification will result in an error from margins because the predicted mean for the
                smoke outcome is a function of the other outcome ptl. This is the stochastic quantity
                that margins is referring to.

                Annika can use the force option to get margins to ignore this dependence
                and treat ptl as a fixed predictor of smoke.

                Code:
                webuse lbw
                gsem    (smoke<-i.race i.low ptl ftv bwt,logit) ///
                        (ptl<-i.race i.low ftv,ologit)
                margins race, at(low=1) predict(outcome(smoke)) force
                marginsplot, name(one)
                Also, given that the model for smoke does not include an interaction between
                race and low, the marginal predictions for each level of race will be
                parallel across the levels of low.

                Here is the second example, reworked so that factor variables are specified consistently
                between the two outcome model specifications and consistent with the prior call to
                margins.

                Code:
                gsem    (smoke<-i.race##i.low ptl ftv bwt,logit) ///
                        (ptl<-i.race i.low ftv,ologit)
                margins i.race#i.low, predict(outcome(smoke)) force
                marginsplot, name(two)

                Comment


                • #9
                  Many thanks Jeff!

                  Comment


                  • #10
                    Hi, I would like to ask a related question.

                    I noticed that Jeff had used two different link functions, logit and ologit, in his example.
                    My question is, does the margins command take care of the differences in the link functions and produce the right marginal effect?

                    Thanks.

                    Pui Hang

                    Comment


                    • #11
                      The predict() option of margins is where you specify which prediction you want margins
                      to work with. The supported predict syntax for gsem is documented in [SEM] predict after gsem.

                      In my previous post, the option predict(outcome(smoke)) specifies to margins that we want to
                      compute the marginal predictions of the smoke outcome variable. Since smoke is a logit
                      outcome variable, this means we want predict probabilities for smoke==1.

                      For ordinal outcomes, margins requires that you specify which level of the outcome variable for the predicted
                      probabilities. So if ptl has 3 levels, say 1, 2, and 3, then the predict() options would be
                      predict(outcome(ptl 1)), predict(outcome(ptl 2)), and predict(outcome(ptl 3)).

                      Comment


                      • #12
                        Jeff, thank you for your prompt reply!

                        Comment


                        • #13
                          Dear Jeff,

                          I wanted to ask your help with resolving problems with the margins and marginplot commands after running a model concerned with determinants of childhood stunting. The syntax is below.

                          gsem (Crypto age Crypt_Age Giardia toilet1 edumother dog goat refrigerator floor casecontrol -> stunt, family(binomial) link(logit)) /// Equation for stunting
                          (f_dung HWeat f_dHWeat toilet1 HW1 toi1HW edumother childfeces edu_chld_feces Giardia Rota casecontrol -> Crypto, family(binomial) link(logit)), nocapslatent
                          margins f_dung, at(HWeat=1) predict(outcome(stunt)) force
                          marginsplot, name(one)

                          When I run the this model with the margins command I get the following error message:

                          margins f_dung, at(HWeat=1) predict(outcome(stunt)) force
                          (note: prediction is a function of possibly stochastic quantities other than e(b))
                          factor 'f_dung' not found in list of covariates
                          r(322);

                          Thank you

                          Kurt Long

                          Comment


                          • #14
                            Dear STATALIST Users


                            I am estimating a probit model with an endogenous regressor (x1) and its interaction with three other exogenous regressors (x2 x3 and x4) using two instruments z1 and z2. However, because the interactions are also now contaminated, I used the following gsem command:

                            gsem (y1 <- x1 x2 x3 x4 x1#x2 x1#x3 x1#x4 L@1, probit) (x1 <- z1 z2 x2 x3 x4) (x1#x2 <- z1#x2 z2#x2) (x1#x3 <- z1#x3 z2#x3) (x1#x4 <- z1#x4 z2#x4), var(L@1)

                            However, I got errors such as these:

                            found where an endogenous variable expected
                            r(198);

                            factor variables may not contain noninteger values
                            r(452);

                            I tried to bypass this problem by manually deriving the interaction terms and I could then estimate the model. But, getting the marginal effect has been so slow using

                            margins, dydx(*) predict(outcome(y) fixed).

                            a) Is there a way to use the interaction notations in gsem (i.e. x1#x2 as dependent variables)?
                            b) Is there a faster way to get the marginal effects?

                            Thank you
                            Moraks

                            Comment

                            Working...
                            X