Announcement

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

  • Extracting Marginal Effects from Interaction Terms in an OLS Regression

    Comrades,

    I am trying to compute marginal effects for several interaction terms in two separate models--one based on a binary outcome (using a probit model) and one based on a continuous outcome (using OLS). I came across a really great blog post, from Vince Wiggins, that details a clever way of extracting marginal effects from interactions terms in non-linear models: http://www.stata.com/statalist/archi.../msg00301.html. This clarified things for my probit model.

    Here is the trouble: I recently reviewed Richard Williams slides on marginal effects (http://www3.nd.edu/~rwilliam/stats/Margins01.pdf) and read that marginal effects in OLS regressions should be identical to the OLS coefficients themselves. When I compute marginal effects in my OLS model for variables that are not included in interactions, the marginal effects are equivalent to the analogous coefficients, as would be expected based on my reading of Richard's slides. However, when I compute marginal effects in my OLS model for any terms included in an interaction--as well as when I compute marginal effects for the interactions themselves using Vince Wiggins' method--I get a slightly different result than the OLS-based coefficients for each term.

    Is this to be expected?

    Cheers,
    Francis
    Last edited by Francis Pearman; 28 Jul 2016, 11:19.

  • #2
    In general, marginal effects in OLS will not be equal to coefficients as long as there are interactions. Consider this model: E[y|x,z] = a + b*x + c*z + d*(x*z).

    The marginal effect of x on y is dy/dx = b + d*z. It is a function of coefficients and depend on the value of z.

    If d=0 (not interactions), then dy/dx = b, and coefficient will be equal to marginal effect.

    You don't provide code, so it's hard to know what if what you are comparing is apples to apples.

    Below, I calculate the marginal effects and the interaction marginal effects, with margins and by hand, for OLS and probit. The differences are very slight, if any.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . replace price = price/1000 //convert price to 1Ks
    variable price was int now float
    (74 real changes made)
    
    .
    . /* (1) OLS */
    . reg foreign c.price##c.mpg
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(3, 70)        =      7.23
           Model |  3.65748906         3  1.21916302   Prob > F        =    0.0003
        Residual |  11.8019704        70  .168599577   R-squared       =    0.2366
    -------------+----------------------------------   Adj R-squared   =    0.2039
           Total |  15.4594595        73  .211773417   Root MSE        =    .41061
    
    -------------------------------------------------------------------------------
          foreign |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
            price |  -.0229588   .0682421    -0.34   0.738    -.1590634    .1131457
              mpg |   .0207536   .0225257     0.92   0.360    -.0241726    .0656797
                  |
    c.price#c.mpg |   .0040798   .0038539     1.06   0.293    -.0036065     .011766
                  |
            _cons |  -.5066533   .4584344    -1.11   0.273    -1.420972    .4076653
    -------------------------------------------------------------------------------
    
    . /* OLS Marginal Effect of MPG */
    . margins, dydx(mpg) at(price=15 mpg=25)
    
    Conditional marginal effects                    Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    dy/dx w.r.t. : mpg
    at           : price           =          15
                   mpg             =          25
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |   .0819498   .0385043     2.13   0.037     .0051554    .1587442
    ------------------------------------------------------------------------------
    
    .
    . /* Note that ME of MPG does not depend on mpg, unlike with probit below */
    . margins, dydx(mpg) at(price=15 mpg=0)
    
    Conditional marginal effects                    Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    dy/dx w.r.t. : mpg
    at           : price           =          15
                   mpg             =           0
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |   .0819498   .0385043     2.13   0.037     .0051554    .1587442
    ------------------------------------------------------------------------------
    
    .
    . /* OLS ME by Hand */
    . nlcom mpg_me:_b[mpg]+_b[c.price#c.mpg]*15
    
          mpg_me:  _b[mpg]+_b[c.price#c.mpg]*15
    
    ------------------------------------------------------------------------------
         foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mpg_me |   .0819498   .0385043     2.13   0.033     .0064828    .1574169
    ------------------------------------------------------------------------------
    
    .
    . /*VW Interaction: Should Match OLS Coefficient c.price#c.mpg  */
    . margins, dydx(mpg) at(price==16) at(price==15) contrast(atcontrast(r._at))
    
    Contrasts of average marginal effects
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    dy/dx w.r.t. : mpg
    
    1._at        : price           =          16
    
    2._at        : price           =          15
    
    ------------------------------------------------
                 |         df           F        P>F
    -------------+----------------------------------
    mpg          |
             _at |          1        1.12     0.2934
                 |
     Denominator |         70
    ------------------------------------------------
    
    --------------------------------------------------------------
                 |   Contrast Delta-method
                 |      dy/dx   Std. Err.     [95% Conf. Interval]
    -------------+------------------------------------------------
    mpg          |
             _at |
       (2 vs 1)  |  -.0040798   .0038539      -.011766    .0036065
    --------------------------------------------------------------
    
    . /* VW Interaction by Hand: Difference in OLS Marginal Effects with price of 16 versus 15 */
    . nlcom mpgXprice_me: (_b[mpg]+_b[c.price#c.mpg]*16)-(_b[mpg]+_b[c.price#c.mpg]*15)
    
    mpgXprice_me:  (_b[mpg]+_b[c.price#c.mpg]*16)-(_b[mpg]+_b[c.price#c.mpg]*15)
    
    ------------------------------------------------------------------------------
         foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    mpgXprice_me |   .0040798   .0038539     1.06   0.290    -.0034737    .0116332
    ------------------------------------------------------------------------------
    
    .
    .
    . /* (2) Probit */
    . probit foreign c.price##c.mpg, nolog
    
    Probit regression                               Number of obs     =         74
                                                    LR chi2(3)        =      18.25
                                                    Prob > chi2       =     0.0004
    Log likelihood = -35.910518                     Pseudo R2         =     0.2026
    
    -------------------------------------------------------------------------------
          foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
            price |  -.0359184   .2423192    -0.15   0.882    -.5108553    .4390184
              mpg |    .081867   .0790696     1.04   0.300    -.0731066    .2368405
                  |
    c.price#c.mpg |   .0108245   .0130317     0.83   0.406    -.0147172    .0363662
                  |
            _cons |  -3.491194   1.716571    -2.03   0.042    -6.855611   -.1267773
    -------------------------------------------------------------------------------
    
    . margins, dydx(mpg) at(price=15 mpg=25)
    
    Conditional marginal effects                    Number of obs     =         74
    Model VCE    : OIM
    
    Expression   : Pr(foreign), predict()
    dy/dx w.r.t. : mpg
    at           : price           =          15
                   mpg             =          25
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |   .0112968    .021733     0.52   0.603    -.0312991    .0538928
    ------------------------------------------------------------------------------
    
    . /* Probit ME by Hand: */
    . nlcom mpg_me: normalden(_b[_cons]+_b[mpg]*25 + _b[price]*15 + _b[c.price#c.mpg]*15*25)*(_b[mpg]+_b[c.price#c.mpg]*15)
    
          mpg_me:  normalden(_b[_cons]+_b[mpg]*25 + _b[price]*15 + _b[c.price#c.mpg]*15*25)*(_b[mpg]+_b[c.price#c.mpg]*15)
    
    ------------------------------------------------------------------------------
         foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mpg_me |   .0112968    .021733     0.52   0.603    -.0312991    .0538928
    ------------------------------------------------------------------------------
    
    .
    . /* Note that ME depends on mpg now */
    . margins, dydx(mpg) at(price=15 mpg=0)
    
    Conditional marginal effects                    Number of obs     =         74
    Model VCE    : OIM
    
    Expression   : Pr(foreign), predict()
    dy/dx w.r.t. : mpg
    at           : price           =          15
                   mpg             =           0
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |    .000029   .0002541     0.11   0.909    -.0004691     .000527
    ------------------------------------------------------------------------------
    
    .
    . /* VW Interaction */
    . margins, dydx(mpg) at(price==15 mpg=25) at(price==16 mpg=25) contrast(atcontrast(r._at))
    
    Contrasts of conditional marginal effects
    Model VCE    : OIM
    
    Expression   : Pr(foreign), predict()
    dy/dx w.r.t. : mpg
    
    1._at        : price           =          15
                   mpg             =          25
    
    2._at        : price           =          16
                   mpg             =          25
    
    ------------------------------------------------
                 |         df        chi2     P>chi2
    -------------+----------------------------------
    mpg          |
             _at |          1        0.84     0.3608
    ------------------------------------------------
    
    --------------------------------------------------------------
                 |   Contrast Delta-method
                 |      dy/dx   Std. Err.     [95% Conf. Interval]
    -------------+------------------------------------------------
    mpg          |
             _at |
       (2 vs 1)  |  -.0042443   .0046444     -.0133472    .0048585
    --------------------------------------------------------------
    
    . /* VW Interaction by Hand: Difference in Marginal Effects of MPG with price of 16 versus 15  */
    . nlcom mpgXprice_me: normalden(_b[_cons]+_b[mpg]*25 + _b[price]*16 + _b[c.price#c.mpg]*16*25)*(_b[mpg]+_b[c.price#c.mpg]*16) ///
    >                     -normalden(_b[_cons]+_b[mpg]*25 + _b[price]*15 + _b[c.price#c.mpg]*15*25)*(_b[mpg]+_b[c.price#c.mpg]*15)
    
    mpgXprice_me:  normalden(_b[_cons]+_b[mpg]*25 + _b[price]*16 + _b[c.price#c.mpg]*16*25)*(_b[mpg]+_b[c.price#c.mpg]*16) -normalden(_b[_cons]+_b[mpg]*25
    > + _b[price]*15 + _b[c.price#c.mpg]*15*25)*(_b[mpg]+_b[c.price#c.mpg]*15)
    
    ------------------------------------------------------------------------------
         foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    mpgXprice_me |   -.004246   .0046449    -0.91   0.361    -.0133499    .0048579
    ------------------------------------------------------------------------------


    Code:
    cls
    sysuse auto, clear
    replace price = price/1000 //convert price to 1Ks
    
    /* (1) OLS */
    reg foreign c.price##c.mpg
    /* OLS Marginal Effect of MPG */
    margins, dydx(mpg) at(price=15 mpg=25)
    
    /* Note that ME of MPG does not depend on mpg, unlike with probit below */
    margins, dydx(mpg) at(price=15 mpg=0)
    
    /* OLS ME by Hand */
    nlcom mpg_me:_b[mpg]+_b[c.price#c.mpg]*15
    
    /*VW Interaction: Should Match OLS Coefficient c.price#c.mpg  */
    margins, dydx(mpg) at(price==16) at(price==15) contrast(atcontrast(r._at))
    /* VW Interaction by Hand: Difference in OLS Marginal Effects with price of 16 versus 15 */
    nlcom mpgXprice_me: (_b[mpg]+_b[c.price#c.mpg]*16)-(_b[mpg]+_b[c.price#c.mpg]*15)
    
    
    /* (2) Probit */
    probit foreign c.price##c.mpg, nolog
    margins, dydx(mpg) at(price=15 mpg=25)
    /* Probit ME by Hand: */
    nlcom mpg_me: normalden(_b[_cons]+_b[mpg]*25 + _b[price]*15 + _b[c.price#c.mpg]*15*25)*(_b[mpg]+_b[c.price#c.mpg]*15)
    
    /* Note that ME depends on mpg now */
    margins, dydx(mpg) at(price=15 mpg=0)
    
    /* VW Interaction */
    margins, dydx(mpg) at(price==15 mpg=25) at(price==16 mpg=25) contrast(atcontrast(r._at))
    /* VW Interaction by Hand: Difference in Marginal Effects of MPG with price of 16 versus 15  */
    nlcom mpgXprice_me: normalden(_b[_cons]+_b[mpg]*25 + _b[price]*16 + _b[c.price#c.mpg]*16*25)*(_b[mpg]+_b[c.price#c.mpg]*16) ///
                        -normalden(_b[_cons]+_b[mpg]*25 + _b[price]*15 + _b[c.price#c.mpg]*15*25)*(_b[mpg]+_b[c.price#c.mpg]*15)

    Comment

    Working...
    X