Announcement

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

  • Combine two margins calculations in same output

    Hi,

    I wonder if it as possible to have different calculations of margins (one over a variable, one general) in order to be able to compare them after posted them.
    The marginal effects I'm calculated come from a probit model.

    Code:
    . probit Y  i.Year##(ib(0).Woman  ib(0).Less_18)  , vce(robust)  noomitted
    
    Iteration 0:  Log pseudolikelihood = -16109.479  
    Iteration 1:  Log pseudolikelihood = -15789.582  
    Iteration 2:  Log pseudolikelihood = -15784.681  
    Iteration 3:  Log pseudolikelihood = -15784.679  
    Iteration 4:  Log pseudolikelihood = -15784.679  
    
    Probit regression                                       Number of obs = 66,655
                                                            Wald chi2(32) = 649.99
                                                            Prob > chi2   = 0.0000
    Log pseudolikelihood = -15784.679                       Pseudo R2     = 0.0202
    
    ------------------------------------------------------------------------------
                 |               Robust
               Y | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
            Year |
           2010  |   .1281352   .0863191     1.48   0.138    -.0410472    .2973176
           2011  |   .0060481   .0854791     0.07   0.944    -.1614879    .1735841
           2012  |   .0287616   .0814254     0.35   0.724    -.1308293    .1883526
           2013  |  -.0023281   .0789545    -0.03   0.976     -.157076    .1524199
           2014  |   .0294893   .0793437     0.37   0.710    -.1260215    .1850002
           2015  |   .0738905    .081634     0.91   0.365    -.0861093    .2338903
           2016  |   .0573486   .0818858     0.70   0.484    -.1031446    .2178418
           2017  |   .1550162   .0858574     1.81   0.071    -.0132611    .3232936
           2018  |   .0300782   .0858373     0.35   0.726    -.1381597    .1983161
           2019  |   .0372958   .0881112     0.42   0.672     -.135399    .2099907
                 |
           Woman |
         1. Yes  |   .5333064   .0702314     7.59   0.000     .3956554    .6709574
                 |
         Less_18 |
         1. Yes  |   .1970167   .0673456     2.93   0.003     .0650217    .3290117
                 |
      Year#Woman |
    2010#1. Yes  |  -.1455256   .0992115    -1.47   0.142    -.3399766    .0489253
    2011#1. Yes  |  -.2722308   .0972943    -2.80   0.005    -.4629241   -.0815374
    2012#1. Yes  |  -.2519585   .0943133    -2.67   0.008    -.4368093   -.0671078
    2013#1. Yes  |   -.231189   .0923654    -2.50   0.012    -.4122219   -.0501561
    2014#1. Yes  |  -.2627628   .0926483    -2.84   0.005      -.44435   -.0811756
    2015#1. Yes  |  -.2993844   .0949187    -3.15   0.002    -.4854215   -.1133472
    2016#1. Yes  |  -.2547177   .0950445    -2.68   0.007    -.4410014    -.068434
    2017#1. Yes  |  -.3310648   .0986541    -3.36   0.001    -.5244231   -.1377064
    2018#1. Yes  |  -.0919079   .0998531    -0.92   0.357    -.2876165    .1038006
    2019#1. Yes  |  -.1722308   .1010827    -1.70   0.088    -.3703493    .0258877
                 |
    Year#Less_18 |
    2010#1. Yes  |  -.0021239   .0945272    -0.02   0.982    -.1873938    .1831461
    2011#1. Yes  |   .0963348   .0888794     1.08   0.278    -.0778657    .2705352
    2012#1. Yes  |   .0418818   .0897145     0.47   0.641    -.1339553    .2177189
    2013#1. Yes  |   .0563873   .0905064     0.62   0.533    -.1210019    .2337765
    2014#1. Yes  |    .061398   .0880866     0.70   0.486    -.1112485    .2340446
    2015#1. Yes  |   .0667762   .0895451     0.75   0.456     -.108729    .2422815
    2016#1. Yes  |  -.0124785   .0872498    -0.14   0.886    -.1834849    .1585279
    2017#1. Yes  |   .0340821   .0895138     0.38   0.703    -.1413617     .209526
    2018#1. Yes  |  -.0845595   .0910924    -0.93   0.353    -.2630972    .0939783
    2019#1. Yes  |   -.003382    .090501    -0.04   0.970    -.1807607    .1739967
                 |
           _cons |   1.134713   .0608663    18.64   0.000     1.015417    1.254009
    ------------------------------------------------------------------------------
    Then I calculate marginal effect (global one, not year specific).

    Code:
    . margins  , dydx(Woman Less_18)
    
    Average marginal effects                                Number of obs = 66,655
    Model VCE: Robust
    
    Expression: Pr(Y), predict()
    dy/dx wrt:  1.Woman 1.Less_18
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
           Woman |
         1. Yes  |   .0472766    .003443    13.73   0.000     .0405284    .0540248
                 |
         Less_18 |
         1. Yes  |   .0261693   .0019901    13.15   0.000     .0222688    .0300698
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    And I'm interested in the effect on a specific year (to see if the is an evolution in the effect).

    Code:
    . margins  , dydx(Woman Less_18)  over(Year)
    
    Average marginal effects                                Number of obs = 66,655
    Model VCE: Robust
    
    Expression: Pr(Y), predict()
    dy/dx wrt:  1.Woman 1.Less_18
    Over:       Year
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
    0.Woman      |  (base outcome)
    -------------+----------------------------------------------------------------
    1.Woman      |
            Year |
           2009  |   .0746144   .0127245     5.86   0.000     .0496749    .0995539
           2010  |   .0499041   .0109911     4.54   0.000      .028362    .0714463
           2011  |   .0414397    .012102     3.42   0.001     .0177202    .0651591
           2012  |   .0437596    .011085     3.95   0.000     .0220335    .0654858
           2013  |   .0480971   .0107824     4.46   0.000      .026964    .0692303
           2014  |   .0415898   .0104321     3.99   0.000     .0211432    .0620364
           2015  |   .0345284   .0104533     3.30   0.001     .0140402    .0550166
           2016  |    .042104   .0109987     3.83   0.000      .020547     .063661
           2017  |   .0275068   .0104202     2.64   0.008     .0070836      .04793
           2018  |   .0642928   .0127358     5.05   0.000     .0393311    .0892545
           2019  |   .0528174   .0127015     4.16   0.000      .027923    .0777118
    -------------+----------------------------------------------------------------
    0.Less_18    |  (base outcome)
    -------------+----------------------------------------------------------------
    1.Less_18    |
            Year |
           2009  |   .0188117   .0059635     3.15   0.002     .0071235       .0305
           2010  |   .0186067   .0058635     3.17   0.002     .0071144     .030099
           2011  |    .037299   .0067525     5.52   0.000     .0240644    .0505336
           2012  |   .0301738   .0069441     4.35   0.000     .0165638    .0437839
           2013  |   .0327143      .0072     4.54   0.000     .0186025     .046826
           2014  |   .0327338   .0067081     4.88   0.000     .0195861    .0458815
           2015  |   .0325373   .0067895     4.79   0.000     .0192301    .0458445
           2016  |   .0232434   .0066888     3.47   0.001     .0101335    .0363533
           2017  |   .0266264   .0064072     4.16   0.000     .0140685    .0391843
           2018  |   .0125332   .0066481     1.89   0.059    -.0004968    .0255631
           2019  |   .0221292   .0065912     3.36   0.001     .0092106    .0350478
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    I want to compare these last marginal effects (over Years) with the global one (previous margins output). Do you have an idea on how can I do to post this two kinds of marginal effects calculations into a single posted results?
    Last edited by valérie orozco; 06 May 2025, 01:49.

  • #2
    The default margins calculation is somewhat complex, as it considers hypothetical scenarios. For example, in your case, it assumes that everyone in the sample is female (regardless of their actual gender), calculates their predicted probabilities, and does the same assuming they are all male. In contrast, using the -over()- option does the same thing but calculates the marginal effects over defined groups.

    Thus, the relationship between these two sets of marginal effects is straightforward: the former is simply a weighted average of the latter. Therefore, we can combine them using, for instance, xlincom from SSC. Here is an illustration:

    Code:
    webuse lbw, clear
    probit low i.race##i.smoke i.ht
    margins, dydx(ht)
    margins, dydx(ht) over(race) post
    tab race
    xlincom (1.ht:1bn.race) (1.ht:2.race) (1.ht:3.race) ///
     ((.5079*_b[1.ht:1bn.race]) + (.1376*_b[1.ht:2.race]) + (.3545*_b[1.ht:3.race]) ), post

    Res.:

    Code:
    . margins, dydx(ht)
    
    Average marginal effects                                   Number of obs = 189
    Model VCE: OIM
    
    Expression: Pr(low), predict()
    dy/dx wrt:  1.ht
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
            1.ht |   .2377479   .1416949     1.68   0.093    -.0399689    .5154647
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    
    . margins, dydx(ht) over(race) post
    
    Average marginal effects                                   Number of obs = 189
    Model VCE: OIM
    
    Expression: Pr(low), predict()
    dy/dx wrt:  1.ht
    Over:       race
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
    0.ht         |  (base outcome)
    -------------+----------------------------------------------------------------
    1.ht         |
            race |
          White  |   .2170212   .1389204     1.56   0.118    -.0552579    .4893002
          Black  |   .2469114   .1380679     1.79   0.074    -.0236966    .5175195
          Other  |   .2638899   .1486651     1.78   0.076    -.0274884    .5552682
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    
    . tab race
    
           Race |      Freq.     Percent        Cum.
    ------------+-----------------------------------
          White |         96       50.79       50.79
          Black |         26       13.76       64.55
          Other |         67       35.45      100.00
    ------------+-----------------------------------
          Total |        189      100.00
    
    . xlincom (1.ht:1bn.race) (1.ht:2.race) (1.ht:3.race) ///
    >  ((.5079*_b[1.ht:1bn.race]) + (.1376*_b[1.ht:2.race]) + (.3545*_b[1.ht:3.race]) ), post
    
            lc_1:  1.ht:1bn.race = 0
            lc_2:  1.ht:2.race = 0
            lc_3:  1.ht:3.race = 0
            lc_4:  (.5079*_b[1.ht:1bn.race]) + (.1376*_b[1.ht:2.race]) + (.3545*_b[1.ht:3.race]) = 0
    
    ------------------------------------------------------------------------------
                 | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
            lc_1 |   .2170212   .1389204     1.56   0.118    -.0552579    .4893002
            lc_2 |   .2469114   .1380679     1.79   0.074    -.0236966    .5175195
            lc_3 |   .2638899   .1486651     1.78   0.076    -.0274884    .5552682
            lc_4 |    .237749   .1416948     1.68   0.093    -.0399677    .5154658
    ------------------------------------------------------------------------------
    Last edited by Andrew Musau; 06 May 2025, 03:33.

    Comment


    • #3
      Dear Andrew,
      thank you for you clear explanations and ideas.
      it is perfect!

      Comment

      Working...
      X