Announcement

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

  • Marginal Effect at means on a linear regression

    Hello everyone!

    I am fairly new to STATA, just started a couple of months ago according to the curriculum of my graduate degree for labor market economics. One thing that I am revising right now and I cannot find anywhere (probably due to lack of experience) is how to get the marginal effect of a variable out of a linear regression.

    I will try to be more detailed:

    The dataset is about information on flight prices and distances and so on. After performing a regression on the model

    (reg log(passengers) fare distance distance_squared market_concentration)

    I get my output as expected.

    What I want to get next though is the marginal effect of distance at means.
    The way I tried to go about it was either:

    margins (dydx) dist, atmeans --- which did not work because this is not supposed to be used for a linear regression as far as I take it, and
    after regressing the same model but with i.distance I tried to do margins again but I did not get anything.

    My problem is not calculating the numerical value of the effect, since I can do that with math, but I want to be able to do it in STATA and for that I have not found a way yet. Could someone please help me out?

    Thank you in advance.

  • #2
    Several problems here.

    First, the basic margins syntax needs fixing.

    Consider the code:
    Code:
    sysuse auto, clear
    gen mpg2 = mpg^2
    
    regress price mpg mpg2
    margins , dydx(mpg) atmeans
    You get the following:
    Code:
    . regress price mpg mpg2
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     18.28
           Model |   215835615         2   107917807   Prob > F        =    0.0000
        Residual |   419229781        71  5904644.81   R-squared       =    0.3399
    -------------+----------------------------------   Adj R-squared   =    0.3213
           Total |   635065396        73  8699525.97   Root MSE        =    2429.9
    
    ------------------------------------------------------------------------------
           price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             mpg |  -1265.194   289.5443    -4.37   0.000    -1842.529   -687.8593
            mpg2 |   21.36069   5.938885     3.60   0.001     9.518891    33.20249
           _cons |   22716.48   3366.577     6.75   0.000     16003.71    29429.24
    ------------------------------------------------------------------------------
    
    . margins , dydx(mpg) atmeans
    
    Conditional marginal effects                                Number of obs = 74
    Model VCE: OLS
    
    Expression: Linear prediction, predict()
    dy/dx wrt:  mpg
    At: mpg  =  21.2973 (mean)
        mpg2 = 486.5946 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             mpg |  -1265.194   289.5443    -4.37   0.000    -1842.529   -687.8593
    ------------------------------------------------------------------------------
    However, the result for margins is not really correct, since margins has no clue that mpg2 is the square of mpg. The correct way is to do this instead:
    Code:
    regress price c.mpg##c.mpg
    margins , dydx(mpg) atmeans
    which produces:
    Code:
    . regress price c.mpg##c.mpg
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     18.28
           Model |   215835615         2   107917807   Prob > F        =    0.0000
        Residual |   419229781        71  5904644.81   R-squared       =    0.3399
    -------------+----------------------------------   Adj R-squared   =    0.3213
           Total |   635065396        73  8699525.97   Root MSE        =    2429.9
    
    ------------------------------------------------------------------------------
           price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             mpg |  -1265.194   289.5443    -4.37   0.000    -1842.529   -687.8593
                 |
     c.mpg#c.mpg |   21.36069   5.938885     3.60   0.001     9.518891    33.20249
                 |
           _cons |   22716.48   3366.577     6.75   0.000     16003.71    29429.24
    ------------------------------------------------------------------------------
    
    . margins , dydx(mpg) atmeans
    
    Conditional marginal effects                                Number of obs = 74
    Model VCE: OLS
    
    Expression: Linear prediction, predict()
    dy/dx wrt:  mpg
    At: mpg = 21.2973 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             mpg |  -355.3442   58.86206    -6.04   0.000    -472.7118   -237.9766
    ------------------------------------------------------------------------------
    Notice how the regression results are basically the same, but the margins output has changed.
    Last edited by Hemanshu Kumar; 07 Dec 2022, 09:00.

    Comment


    • #3
      Adding to the discussion
      Margins "only" knows how to handle interactions (polynomials of order N). But struggles with other function transformations.
      Say x_2_5 = x^2.5, as Hemanshu Kumar says, margins have no idea that x_2_5 is related to x. (for all purposes it assumes to be independent from each other.
      There are two ways around it tho (but is some times costly)

      1. know your calculus, get the marginal effects analytically, and let margins do the rest

      Code:
      sysuse auto, clear 
      gen mpg2=mpg*mpg
      regress price mpg mpg2
      
      margins, expression(2*mpg*_b[mpg2]+_b[mpg]) <- marginal effect
      2. use f_able (from ssc)
      Code:
      sysuse auto, clear
      fgen mpg2=mpg*mpg
      regress price mpg mpg2
      f_able mpg2 , auto
      margins, dydx(mpg)
      This command helps margins remember mpg2 is indeed a function of MPG.
      HTH

      Comment


      • #4
        Alright, thank you both so much!

        Code:
         
         sysuse auto, clear  gen mpg2=mpg*mpg regress price mpg mpg2  margins, expression(2*mpg*_b[mpg2]+_b[mpg]) <- marginal effect
        This worked like a charm!

        Comment

        Working...
        X