Announcement

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

  • Difference between margins and predict

    Hello all, I am running a logit model and when it comes to plot the predictive probability, I found difference between margins & marginsplot and predict. EVENT is the binary outcome var, and PERIOD refers to the age, pr2 is the quadratic version of PERIOD. ELL is a binary var as well. Basically what I want to plot is subject's probability of EVENT at different ages. To give you an example, below is my model.

    Code:
    logit EVENT PERIOD pr2 i.ELL if AUT==1
    Code:
    . logit EVENT PERIOD pr2 i.ELL if AUT==1
    
    Iteration 0:   log likelihood = -2000.4498  
    Iteration 1:   log likelihood = -1890.2252  
    Iteration 2:   log likelihood = -1887.0858  
    Iteration 3:   log likelihood = -1887.0743  
    Iteration 4:   log likelihood = -1887.0743  
    
    Logistic regression                               Number of obs   =       3222
                                                      LR chi2(3)      =     226.75
                                                      Prob > chi2     =     0.0000
    Log likelihood = -1887.0743                       Pseudo R2       =     0.0567
    
    ------------------------------------------------------------------------------
           EVENT |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          PERIOD |   .1542712    .088534     1.74   0.081    -.0192522    .3277947
             pr2 |  -.0375349   .0080552    -4.66   0.000    -.0533227    -.021747
           1.ELL |  -.1361254    .128198    -1.06   0.288    -.3873889    .1151381
           _cons |  -.2112793    .227682    -0.93   0.353    -.6575278    .2349692
    ------------------------------------------------------------------------------
    After getting the model result, I used margins followed by marginsplot.

    Code:
    . margins ELL, at(PERIOD=(1(1)10))
    
    Predictive margins                                Number of obs   =       3222
    Model VCE    : OIM
    
    Expression   : Pr(EVENT), predict()
    
    1._at        : PERIOD          =           1
    
    2._at        : PERIOD          =           2
    
    3._at        : PERIOD          =           3
    
    4._at        : PERIOD          =           4
    
    5._at        : PERIOD          =           5
    
    6._at        : PERIOD          =           6
    
    7._at        : PERIOD          =           7
    
    8._at        : PERIOD          =           8
    
    9._at        : PERIOD          =           9
    
    10._at       : PERIOD          =          10
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
         _at#ELL |
            1 0  |   .1991791   .0518272     3.84   0.000     .0975996    .3007586
            1 1  |    .180405   .0495094     3.64   0.000     .0833683    .2774416
            2 0  |   .2219689   .0431328     5.15   0.000     .1374303    .3065076
            2 1  |   .2017769   .0423774     4.76   0.000     .1187187    .2848352
            3 0  |   .2463177   .0328558     7.50   0.000     .1819215    .3107139
            3 1  |   .2247533   .0342906     6.55   0.000      .157545    .2919616
            4 0  |   .2721463   .0212896    12.78   0.000     .2304195    .3138732
            4 1  |   .2492807   .0263204     9.47   0.000     .1976938    .3008677
            5 0  |   .2993476   .0099511    30.08   0.000     .2798439    .3188513
            5 1  |    .275277     .02144    12.84   0.000     .2332553    .3172986
            6 0  |   .3277877   .0114108    28.73   0.000     .3054229    .3501524
            6 1  |   .3026314   .0243073    12.45   0.000     .2549899    .3502729
            7 0  |   .3573089   .0253801    14.08   0.000     .3075647     .407053
            7 1  |   .3312072   .0344955     9.60   0.000     .2635973    .3988171
            8 0  |   .3877332   .0413639     9.37   0.000     .3066615     .468805
            8 1  |   .3608442   .0483547     7.46   0.000     .2660707    .4556177
            9 0  |   .4188664   .0580044     7.22   0.000     .3051799     .532553
            9 1  |   .3913623   .0639059     6.12   0.000     .2661091    .5166155
           10 0  |   .4505018   .0748097     6.02   0.000     .3038776    .5971261
           10 1  |   .4225655   .0802194     5.27   0.000     .2653383    .5797927
    ------------------------------------------------------------------------------
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	131.7 KB
ID:	1400675


    Then still using the model plot, I instead use predict
    Code:
    logit EVENT PERIOD pr2 i.ELL if AUT==1
    predict PREDVALS4, pr
    bysort PERIOD ELL: gen count5 = _n
    graph twoway connect PREDVALS4 PERIOD if count5 ==1 & ELL == 1 || ///
                 connect PREDVALS4 PERIOD if count5==1 & ELL == 0, ///
                 ytitle(Fitted Hazard Probability) ///
                 xlabel(1 "16" 2 "17" 3 "18" 4 "19" 5 "20" 6 "21" 7 "22" 8 "23" 9 "24" 10 "25") ///
                 legend(position(6) rows(1) ring(0) size(small) region(lstyle(none) lcolor(none) color(none)) label(1 "ELL") label(2 "Non-ELL")) ///
                 graphregion(color(white) fcolor(white) lcolor(white)) xtitle(Year) title("ELL (Autism)", size(small))
    But I then get a totally different plot.

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	137.2 KB
ID:	1400676


    Can anyone tell me why is this happening? Is it because margins and predict are calculating different results? If yes, then what are they specifically? Thanks.

  • #2
    margins needs to know that PERIOD and p2 belong together. To do that you need to include the square through the factor variable notation: c.PERIOD##c.PERIOD . For more see help fvvarlist
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Hi Maarten, thanks for your reply. I actually tried the # but margins command afterwards shows results to be not estimable, which I would guess is caused by the multicollinearity issue. So in my occasion where I have PERIOD and pr2 both in the model, what should I use in order to make margins work?

      Comment


      • #4
        You cannot use margins without using factor variables, so the answer to your question is that that is impossible.

        So now we need to diagnose what went wrong with the model with factor variables. Did you use PERIOD##PERIOD, or PERIOD#PERIOD, or c.PERIOD#c.PERIOD, or c.PERIOD##c.PERIOD, or .... In other words can you tell use exactly what your told Stata, what Stata answered you. Not just the margins, but the exact model and its output.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Yes, you are correct and it works now. Thanks a lot!

          Comment


          • #6
            could you explain me which is the difference between predictors and margins commands?

            Comment


            • #7
              The -predict- command calculates a separate predicted value for every observation in the data set for which all of the predictor variables are non-missing.

              The -margins- command calculates average predicted values for groups of observations, the groups being defined by levels of some variable(s) in the model.

              So, for example, if you have a data set where each observation is a person, -predict- calculates predicted values for every person. -margins- calculates average predictions for subgroups such as all men, or all people over age 65, or all Buddhists, etc.

              Comment


              • #8
                Oh, perfect, thank you very much! Now is more clear!

                Comment

                Working...
                X