Announcement

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

  • OLS margins, eydx(foreign) calculation by hand

    I am puzzled how Stata calculates the semi-elasticity in a n OLS model with a continuous outcome and a single binary regressor, y = a + b*x. The continuous case makes sense to me. The semi elasticity is (dy/dx)*(1/yhat) = b/yhat.

    I thought that this would carry over to the discrete case with a finite difference instead of a derivative. In a linear model, the finite difference and the derivative should be the same, so this becomes (a + b*1 - a - b*0)/yhat = b/yhat . However, this does not seem to be the case:


    Code:
    . /* Continuous Case */
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . reg price c.mpg
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(1, 72)        =     20.26
           Model |   139449474         1   139449474   Prob > F        =    0.0000
        Residual |   495615923        72  6883554.48   R-squared       =    0.2196
    -------------+----------------------------------   Adj R-squared   =    0.2087
           Total |   635065396        73  8699525.97   Root MSE        =    2623.7
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879
           _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03
    ------------------------------------------------------------------------------
    
    . margins, eydx(mpg)
    
    Average marginal effects                        Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    ey/dx w.r.t. : mpg
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      ey/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |  -.0421593   .0123505    -3.41   0.001    -.0667795    -.017539
    ------------------------------------------------------------------------------
    
    . gen double eydx = _b[mpg]/(_b[_cons] + _b[mpg]*mpg)
    
    . sum eydx
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
            eydx |         74   -.0421593    .0181532  -.1638066  -.0284862
    
    . /* Binary Case */
    . reg price i.foreign
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(1, 72)        =      0.17
           Model |  1507382.66         1  1507382.66   Prob > F        =    0.6802
        Residual |   633558013        72  8799416.85   R-squared       =    0.0024
    -------------+----------------------------------   Adj R-squared   =   -0.0115
           Total |   635065396        73  8699525.97   Root MSE        =    2966.4
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
         foreign |
        Foreign  |   312.2587   754.4488     0.41   0.680    -1191.708    1816.225
           _cons |   6072.423    411.363    14.76   0.000     5252.386     6892.46
    ------------------------------------------------------------------------------
    
    . margins, eydx(foreign)
    
    Conditional marginal effects                    Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    ey/dx w.r.t. : 1.foreign
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      ey/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
         foreign |
        Foreign  |   .0501439   .1200041     0.42   0.677    -.1890798    .2893677
    ------------------------------------------------------------------------------
    Note: ey/dx for factor levels is the discrete change from the base level.
    
    . gen double eydx1 = _b[1.foreign]/(_b[_cons] + _b[1.foreign]*foreign)
    
    . gen double eydx2 = _b[1.foreign]/(_b[_cons] + _b[1.foreign]*0)
    
    . gen double eydx3 = _b[1.foreign]/(_b[_cons] + _b[1.foreign]*1)
    
    . gen double eydx4 = _b[1.foreign]/(_b[_cons] + _b[1.foreign]*.2972973)
    
    . sum eydx?
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           eydx1 |         74    .0506747    .0011573   .0489075   .0514224
           eydx2 |         74    .0514224           0   .0514224   .0514224
           eydx3 |         74    .0489075           0   .0489075   .0489075
           eydx4 |         74    .0506481           0   .0506481   .0506481
    Any guidance would be appreciated.

  • #2
    Here's the solution:

    Code:
    /* Continuous Case: dln(y)/dx */
    sysuse auto, clear
    reg price c.mpg
    margins, eydx(mpg)
    gen double eydx = _b[mpg]/(_b[_cons] + _b[mpg]*mpg)
    sum eydx   
    
    /* Binary Case: ln(y(x=1)) - ln(y(x=1)) */
    reg price i.foreign
    margins, eydx(foreign)
    generate double eydx2 = log(_b[1.foreign] + _b[_cons]) - log(_b[_cons])
    sum eydx2

    Comment


    • #3
      Originally posted by Dimitriy V. Masterov View Post
      Here's the solution:

      Code:
      /* Continuous Case: dln(y)/dx */
      sysuse auto, clear
      reg price c.mpg
      margins, eydx(mpg)
      gen double eydx = _b[mpg]/(_b[_cons] + _b[mpg]*mpg)
      sum eydx
      
      /* Binary Case: ln(y(x=1)) - ln(y(x=1)) */
      reg price i.foreign
      margins, eydx(foreign)
      generate double eydx2 = log(_b[1.foreign] + _b[_cons]) - log(_b[_cons])
      sum eydx2

      How would you interpret eydx for the binary case?

      Also, I wonder if it would be meaningful to calculate the margins manually as
      (y(x=1) - y(x=0))/ y(x=0) instead, so that the marginal effects are interpreted as the average proportional gain or loss from going from x=0 to x=1, for an individual for whom x=0. That is, if this calculation is 0.09, a subject with x=0 would gain, on average, 9% more of y if x=1 instead. Would this make sense? Thank you!

      Comment


      • #4
        Code:
        . generate double eydx2 = log(_b[1.foreign] + _b[_cons]) - log(_b[_cons])
        
        . sum eydx2
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
               eydx2 |         74    .0501439           0   .0501439   .0501439
        My interpretation is that foreign origin is associated with a 5% price premium.

        Your suggestion is correct, and what I might do if the estimated effect was large. But it will return approximately the same answer as the log approach when the delta is small:

        Code:
        . 
        . /*  Binary Case: [y(x=1)) - y(x=0)]/y(x=0) */ 
        . reg price i.foreign
        
              Source |       SS           df       MS      Number of obs   =        74
        -------------+----------------------------------   F(1, 72)        =      0.17
               Model |  1507382.66         1  1507382.66   Prob > F        =    0.6802
            Residual |   633558013        72  8799416.85   R-squared       =    0.0024
        -------------+----------------------------------   Adj R-squared   =   -0.0115
               Total |   635065396        73  8699525.97   Root MSE        =    2966.4
        
        ------------------------------------------------------------------------------
               price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
             foreign |
            Foreign  |   312.2587   754.4488     0.41   0.680    -1191.708    1816.225
               _cons |   6072.423    411.363    14.76   0.000     5252.386     6892.46
        ------------------------------------------------------------------------------
        
        . margins, expression((_b[1.foreign] + _b[_cons])/_b[_cons])
        warning: option expression() does not contain option predict() or xb().
        warning: prediction constant over observations.
        
        Predictive margins                                          Number of obs = 74
        Model VCE: OLS
        
        Expression: (_b[1.foreign] + _b[_cons])/_b[_cons]
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   std. err.      z    P>|z|     [95% conf. interval]
        -------------+----------------------------------------------------------------
               _cons |   1.051422    .126175     8.33   0.000      .804124    1.298721
        ------------------------------------------------------------------------------
        
        . generate double eydx3 = (_b[1.foreign] + _b[_cons])/_b[_cons]
        
        . sum eydx3
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
               eydx3 |         74    1.051422           0   1.051422   1.051422

        Comment

        Working...
        X