Announcement

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

  • plotting the marginal effect

    Usually, we plot the marginal effect using the code below:

    reg y c.x##c.x
    margins, at x=(low (.1) high)
    marginsplot

    However, my supervisor said the interaction of x should be centered to mitigate the multicolinear issue. then the code becomes:

    center x, pre(c_)
    gen square_x=c_x*c_x
    reg y x square_x#square_x

    Undert this circumstance, how to plot the marginal effect?

    Thanks all!

  • #2
    Several things are troubling here.

    1. If you use a command that is not included in Stata, please refer where to get it (center in this case, which is available from SSC).
    2. Your original example doesn't calculate a marginal effect, but rather fitted values at different values of x.
    3. Your original example also doesn't make the right use of the -at- option.

    Having said that, consider the following
    Code:
    clear all
    set more off
    sysuse auto
    center mpg
    
    reg price mpg c.c_mpg#c.c_mpg
    
    quiet margins, expression(_b[mpg] + 2 * _b[c.c_mpg#c.c_mpg] * c_mpg) at(c_mpg = (-10(.5)20))
    marginsplot
    After all, you can always use the expression of the partial effect of a quadratic to get to what you want.
    Last edited by Alfonso Sánchez-Peñalver; 12 Nov 2019, 19:32.
    Alfonso Sanchez-Penalver

    Comment


    • #3
      Originally posted by Alfonso Sánchez-Peñalver View Post
      Several things are troubling here.

      1. If you use a command that is not included in Stata, please refer where to get it (center in this case, which is available from SSC).
      2. Your original example doesn't calculate a marginal effect, but rather fitted values at different values of x.
      3. Your original example also doesn't make the right use of the -at- option.

      Having said that, consider the following
      Code:
      clear all
      set more off
      sysuse auto
      center mpg
      
      reg price mpg c.c_mpg#c.c_mpg
      
      quiet margins, expression(_b[mpg] + 2 * _b[c.c_mpg#c.c_mpg] * c_mpg) at(c_mpg = (-10(.5)20))
      marginsplot
      After all, you can always use the expression of the partial effect of a quadratic to get to what you want.
      Thanks!
      I think I wrote in a wrong way. What I want is the U shape picture between mpg and price, how to get this? Thanks~

      Code:
       
       clear all set more off sysuse auto center mpg  reg price mpg c.c_mpg#c.c_mpg

      Comment


      • #4
        Code:
        reg price c.c_mpg##c.c_mpg
        quiet margins, at(c_mpg = (-10(.5)20))
        marginsplot
        You will find that this regression gives you the same slopes as the previous one, the only difference is in the intercept.
        Last edited by Alfonso Sánchez-Peñalver; 12 Nov 2019, 20:01.
        Alfonso Sanchez-Penalver

        Comment


        • #5
          I'll add that I am a big fan of the user-written mcp, which makes it easy to do marginsplots involving continuous variables. Some highlights are covered at

          https://www3.nd.edu/~rwilliam/stats3/Margins03.pdf
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Originally posted by Alfonso Sánchez-Peñalver View Post
            Code:
            reg price c.c_mpg##c.c_mpg
            quiet margins, at(c_mpg = (-10(.5)20))
            marginsplot
            You will find that this regression gives you the same slopes as the previous one, the only difference is in the intercept.
            Not like this.
            I mean after running:

            reg price mpg c.c_mpg#c.c_mpg

            how to get the U shape?

            Comment


            • #7
              Originally posted by Richard Williams View Post
              I'll add that I am a big fan of the user-written mcp, which makes it easy to do marginsplots involving continuous variables. Some highlights are covered at

              https://www3.nd.edu/~rwilliam/stats3/Margins03.pdf
              Thanks for your information.

              Comment


              • #8
                Originally posted by Fred Lee View Post

                Not like this.
                I mean after running:

                reg price mpg c.c_mpg#c.c_mpg

                how to get the U shape?
                As I posted in my example, I told you that the regression I do there is identical to the one using mpg and c.c_mpg#c.c_mpg, that the only thing that changes is the intercept. So that is a valid way.
                Alfonso Sanchez-Penalver

                Comment


                • #9
                  Originally posted by Alfonso Sánchez-Peñalver View Post

                  As I posted in my example, I told you that the regression I do there is identical to the one using mpg and c.c_mpg#c.c_mpg, that the only thing that changes is the intercept. So that is a valid way.
                  Except for the constant, the coefficient of mpg is also changed. The significance of mpg is not changed in this example, but I saw this way did change the significance level of one-degree term.
                  I am wondering whether the coefficient change will impact the figure I wnat to plot.
                  Thanks!

                  Comment


                  • #10
                    You are estimating the identical quadratic curve either way; the coefficient on mpg is different because that curve is located at a different position on the x-scale.

                    I believe that the advice to center variables in this context is a few decades out of date. Doing so does not affect the substance of the estimates, it just affects the numeric stability. However, Stata centers the variables internally before doing calculations, then un-centers them, so I believe it make literally no difference.

                    In any case, compare the plots produced here:

                    Code:
                    sysuse auto, clear
                    sum mpg
                    local mean = r(mean)
                    gen Cmpg = mpg-`mean'
                    reg price mpg c.mpg#c.mpg
                    margins, at(mpg=(12(1)41))
                    marginsplot, recastci(rarea) name(uncentered, replace) xlab(12 41)
                    reg price Cmpg c.Cmpg#c.Cmpg
                    sum mpg
                    margins, at(Cmpg=(`=12-`mean''(1)`=41-`mean''))
                    marginsplot, recastci(rarea) name(centered, replace) xlab(`=12-`mean'' `=41-`mean'')

                    Comment


                    • #11
                      Originally posted by Nicholas Winter View Post
                      You are estimating the identical quadratic curve either way; the coefficient on mpg is different because that curve is located at a different position on the x-scale.

                      I believe that the advice to center variables in this context is a few decades out of date. Doing so does not affect the substance of the estimates, it just affects the numeric stability. However, Stata centers the variables internally before doing calculations, then un-centers them, so I believe it make literally no difference.

                      In any case, compare the plots produced here:

                      Code:
                      sysuse auto, clear
                      sum mpg
                      local mean = r(mean)
                      gen Cmpg = mpg-`mean'
                      reg price mpg c.mpg#c.mpg
                      margins, at(mpg=(12(1)41))
                      marginsplot, recastci(rarea) name(uncentered, replace) xlab(12 41)
                      reg price Cmpg c.Cmpg#c.Cmpg
                      sum mpg
                      margins, at(Cmpg=(`=12-`mean''(1)`=41-`mean''))
                      marginsplot, recastci(rarea) name(centered, replace) xlab(`=12-`mean'' `=41-`mean'')
                      Thanks a lot!
                      Usually, I run analyses without centering. My supervisor's centering advice makes me confused. I think your comments are right and useful.

                      Comment


                      • #12
                        Originally posted by Nicholas Winter View Post
                        You are estimating the identical quadratic curve either way; the coefficient on mpg is different because that curve is located at a different position on the x-scale.

                        I believe that the advice to center variables in this context is a few decades out of date. Doing so does not affect the substance of the estimates, it just affects the numeric stability. However, Stata centers the variables internally before doing calculations, then un-centers them, so I believe it make literally no difference.

                        In any case, compare the plots produced here:

                        Code:
                        sysuse auto, clear
                        sum mpg
                        local mean = r(mean)
                        gen Cmpg = mpg-`mean'
                        reg price mpg c.mpg#c.mpg
                        margins, at(mpg=(12(1)41))
                        marginsplot, recastci(rarea) name(uncentered, replace) xlab(12 41)
                        reg price Cmpg c.Cmpg#c.Cmpg
                        sum mpg
                        margins, at(Cmpg=(`=12-`mean''(1)`=41-`mean''))
                        marginsplot, recastci(rarea) name(centered, replace) xlab(`=12-`mean'' `=41-`mean'')
                        Oh, a llittle different. My supervisor suggested me to run
                        reg price mpg c.Cmpg#c.Cmpg
                        rather than
                        reg price Cmpg c.Cmpg#c.Cmpg

                        Comment


                        • #13
                          Couldn't agree more with Nicholas Winter. To illustrate that either of the regressions I told you using the centered variable yields the same result here are the results from Stata:
                          Code:
                          . clear all
                          
                          . set more off
                          
                          . sysuse auto
                          (1978 Automobile Data)
                          
                          . center mpg
                          (generated variables: c_mpg)
                          
                          . 
                          . reg price mpg c.c_mpg#c.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 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                          ----------------+----------------------------------------------------------------
                                      mpg |  -355.3442   58.86206    -6.04   0.000    -472.7118   -237.9766
                                          |
                          c.c_mpg#c.c_mpg |   21.36069   5.938885     3.60   0.001     9.518891    33.20249
                                          |
                                    _cons |    13027.8   1191.358    10.94   0.000      10652.3     15403.3
                          ---------------------------------------------------------------------------------
                          
                          . reg price c.c_mpg##c.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 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                          ----------------+----------------------------------------------------------------
                                    c_mpg |  -355.3442   58.86206    -6.04   0.000    -472.7118   -237.9766
                                          |
                          c.c_mpg#c.c_mpg |   21.36069   5.938885     3.60   0.001     9.518891    33.20249
                                          |
                                    _cons |   5459.933   343.8718    15.88   0.000     4774.272    6145.594
                          ---------------------------------------------------------------------------------
                          This is what I said in #4 and #8.
                          Alfonso Sanchez-Penalver

                          Comment


                          • #14
                            Originally posted by Alfonso Sánchez-Peñalver View Post
                            Couldn't agree more with Nicholas Winter. To illustrate that either of the regressions I told you using the centered variable yields the same result here are the results from Stata:


                            This is what I said in #4 and #8.
                            Thanks a lot! I finally got you!
                            Now I know that reg price mpg c.c_mpg#c.c_mpg and reg price c.c_mpg##c.c_mpg is identical.
                            I also know literally there is no difference between centering and un-centering.
                            ut how to explain the difference of coefficient of mpg? The significance level is different (p=.006 vs. p=.003). Why the significance difference is ignored? Which one is more correct?
                            Attached Files
                            Last edited by Fred Lee; 13 Nov 2019, 08:13.

                            Comment


                            • #15
                              Centering can make regression coefficients easier to understand. In the original metric, X = 0 may be an unlikely or even impossible value. After centering, X = 0 stands for an "average" individual.

                              https://www3.nd.edu/~rwilliam/stats2/l53.pdf

                              I wrote that handout because students would say things like "After you control for the interaction of female*income, female no longer has an effect". Or worse, "the effect of female actually changes sign!" Statements like that reflect a failure to understand what the main effects of a variable actually mean once interactions are added to a model.

                              Even if you don't use centering, I think a discussion of it may help people to better understand how interaction effects work.

                              I agree that, if you are using margins, centering is not really necessary.
                              -------------------------------------------
                              Richard Williams, Notre Dame Dept of Sociology
                              Stata Version: 17.0 MP (2 processor)

                              EMAIL: [email protected]
                              WWW: https://www3.nd.edu/~rwilliam

                              Comment

                              Working...
                              X