Announcement

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

  • Bug in margins with factor variable notation for quadratic term

    Description:

    I think I found a bug with margins. specifically with the atmeans option. If the model is estimated with factor variable notation for a squared term, atmeans uses the square of the mean of the independent variable instead of the mean of the squared variable.

    Simple code to replicate issue:
    Code:
    clear all
    sysuse auto
    gen weight2 = c.weight#c.weight
    summ weight c.weight#c.weight weight2
    reg price c.weight##c.weight
    margins, atmeans
    reg price weight weight2
    margins, atmeans
    The output, where the first "margins" output is incorrect (it uses the square of the mean of weight) and the second one is correct (it uses the mean of weight2):

    Code:
    Adjusted predictions                            Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    at           : weight          =    3019.459 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           _cons |   5263.004   374.2033    14.06   0.000     4516.864    6009.144
    ------------------------------------------------------------------------------
    
    Adjusted predictions                            Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    at           : weight          =    3019.459 (mean)
                   weight2         =     9713003 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           _cons |   6165.257   270.6208    22.78   0.000     5625.655     6704.86
    ------------------------------------------------------------------------------

    Technical details:

    Stata/SE 14.2 for Windows (64-bit x86-64)
    Revision 13 Jul 2017

  • #2
    Originally posted by Ariel Karlinsky View Post
    Description:
    If the model is estimated with factor variable notation for a squared term, atmeans uses the square of the mean of the independent variable instead of the mean of the squared variable.
    I would argue that what margins does is correct. Because there is only one variable, weight, the predicted value when that variable is set to its mean should indeed be based on the square of the mean, not the mean of the square. Using the mean of the square implies that there are two separate variables that can change values independently from each other, which clearly is not the case.

    Best
    Daniel

    Comment


    • #3
      I agree with Daniel. For more, see

      https://www3.nd.edu/~rwilliam/xsoc73994/Margins01.pdf

      See especially slides 11-14.

      Incidentally, as the handout also explains, I personally prefer the default asobserved behavior over atmeans.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

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

      Comment

      Working...
      X