Announcement

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

  • Extracting marginal effects

    Hi all,

    There may be a straightforward answer for this question, but I can't find it anywhere. I am specifying a simple OLS regression of the following form:

    Y = alpha + Beta1 * A + Beta2 * A2+ Beta3 * (A - B)2

    where A and B are continous variables, and "alpha" and "beta" are model parameters.

    In the past I have been creating a new variable z = (A - B)2 in order to run this using the
    Code:
    reg
    command. However, I want to plot the marginal effect of A on Y using
    Code:
    marginsplot
    , which shouldn't ignore the fact that z is a function of A. In addition, I cannot reorder the terms because I am specifically interested in analysing the Beta3 coefficient.

    Any idea of how to go out this problem?

    Thanks,
    Jack


  • #2
    Code:
    ssc install f_able, replace
    help f_able

    Comment


    • #3
      f_able, suggested in #2 by Andrew Musau, is a nice program. If for some reason that doesn't suffice you could try either nl or gmm, e.g.
      Code:
      cap preserve
      cap drop _all
      
      set obs 100
      set seed 2345
      
      gen A=runiform()
      gen B=runiform()
      gen y=1 + A + A^2 + (A-B)^2 + rnormal(0,1)
      
      nl (y = {alpha} + {beta1}*A + {beta2}*A^2 + {beta3}*(A-B)^2), variables(A B)
      margins, dydx(*)
      
      cap restore
      Results:
      Code:
      . nl (y = {alpha} + {beta1}*A + {beta2}*A^2 + {beta3}*(A-B)^2), variables(A B)
      
      Iteration 0:  residual SS =  82.20916
      Iteration 1:  residual SS =  82.20916
      
      
            Source |      SS            df       MS
      -------------+----------------------------------    Number of obs =        100
             Model |  11.789461          3  3.92982025    R-squared     =     0.1254
          Residual |  82.209157         96  .856345381    Adj R-squared =     0.0981
      -------------+----------------------------------    Root MSE      =   .9253893
             Total |  93.998617         99  .949480983    Res. dev.     =   264.1974
      
      ------------------------------------------------------------------------------
                 y | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
            /alpha |   1.202186   .3333567     3.61   0.000     .5404785    1.863894
            /beta1 |   1.518958   1.422263     1.07   0.288    -1.304211    4.342127
            /beta2 |  -.2374228    1.39287    -0.17   0.865    -3.002248    2.527403
            /beta3 |   .6301616   .5753816     1.10   0.276    -.5119618    1.772285
      ------------------------------------------------------------------------------
      Note: Parameter alpha is used as a constant term during estimation.
      
      . margins, dydx(*)
      
      Average marginal effects                                   Number of obs = 100
      Model VCE: GNR
      
      Expression: Fitted values, predict()
      dy/dx wrt:  A B
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
      -------------+----------------------------------------------------------------
                 A |   1.213612   .3485474     3.48   0.000     .5304712    1.896752
                 B |    .085941   .0784701     1.10   0.273    -.0678576    .2397396
      ------------------------------------------------------------------------------

      Comment


      • #4
        Taking John's example, I have argued elsewhere that in cases where variables are not explicitly present in the estimated model, one can also manually compute the derivative and use the -expression()- option of margins. Given:

        $$y = \beta_0 + \beta_1 A + \beta_2 A^2 + \beta_3 (A-B)^2$$

        The partial derivative is:

        $$\frac{\partial y}{\partial A} = \beta_1 + \left(2\beta_2\times A\right)+ \left(2\beta_{3}\times(A-B)\right).$$



        Code:
        cap preserve
        cap drop _all
        
        set obs 100
        set seed 2345
        
        gen A=runiform()
        gen B=runiform()
        gen y=1 + A + A^2 + (A-B)^2 + rnormal(0,1)
        gen Z= A-B
        gen Z2= (A-B)^2
        
        regress y c.A##c.A Z2
        margins, expression(_b[A]+ (2*_b[c.A#c.A]*A) + (2*_b[Z2]*Z))
        Res.:

        Code:
         . regress y c.A##c.A Z2
        
              Source |       SS           df       MS      Number of obs   =       100
        -------------+----------------------------------   F(3, 96)        =      4.59
               Model |  11.7894607         3  3.92982022   Prob > F        =    0.0048
            Residual |  82.2091566        96  .856345382   R-squared       =    0.1254
        -------------+----------------------------------   Adj R-squared   =    0.0981
               Total |  93.9986173        99  .949480983   Root MSE        =    .92539
        
        ------------------------------------------------------------------------------
                   y | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
                   A |   1.518958   1.422263     1.07   0.288    -1.304211    4.342127
                     |
             c.A#c.A |  -.2374228    1.39287    -0.17   0.865    -3.002248    2.527403
                     |
                  Z2 |   .6301615   .5753816     1.10   0.276    -.5119619    1.772285
               _cons |   1.202186   .3333567     3.61   0.000     .5404785    1.863894
        ------------------------------------------------------------------------------
        
        .
        . margins, expression(_b[A]+ (2*_b[c.A#c.A]*A) + (2*_b[Z2]*Z))
        warning: option expression() does not contain option predict() or xb().
        
        Predictive margins                                         Number of obs = 100
        Model VCE: OLS
        
        Expression: _b[A]+ (2*_b[c.A#c.A]*A) + (2*_b[Z2]*Z)
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   std. err.      z    P>|z|     [95% conf. interval]
        -------------+----------------------------------------------------------------
               _cons |   1.213612   .3485474     3.48   0.000     .5304712    1.896752
        ------------------------------------------------------------------------------
        
        .
        Last edited by Andrew Musau; 21 Jul 2023, 08:57.

        Comment


        • #5
          Dear Andrew Musau and John Mullahy ,

          Thank you very much for your detailed and helpful answers. I ended up running with Andrew Musau 's last option.

          Just a quick follow up: any idea how to overlay marginsplots for different expressions? Eg:

          - expression 1: _b[A]+ (2*_b[c.A#c.A]*A) + (2*_b[Z2]*Z)
          - expression 2: _b[A]+ (2*_b[c.A#c.A]*A)

          Thanks,
          Jack

          Comment


          • #6
            Edit: the -combomarginsplot- command worked fine

            Comment

            Working...
            X