Announcement

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

  • Postestimation prediction question

    I'm running the following regression: Y = β0 + β1Χ1 + β2Χ2 + .. +.βnXn + ε.

    What's the easiest way to calculate Y_hat = b0 + b1Χ1 + b2Χ2_bar + .. +.bnXn_bar

    where the bs are estimates of βs, and Xi_bar is the mean of Xi?

    I did it by storing the means of Xis as scalar and going the longhand way but my code looks kludgy. I was wondering if there is a simpler, cleaner way to do it.

  • #2
    That'd be just one number, correct? -margins, atmean- after the regression command may be able to get what you need:

    Code:
    sysuse auto, clear
    
    * Storing the means for checking later:
    foreach x in weight mpg headroom length{
        quietly sum `x'
        scalar m`x' = r(mean)
    }
    
    * Here is the regression
    reg price weight mpg headroom length
    
    * The "long way":
    display _b[_cons] + _b[weight]*`=mweight' + _b[mpg]*`=mmpg' + _b[headroom]*`=mheadroom' + _b[length]*`=mlength'
    
    * Shortcut to get the same answer:
    margins, atmean

    Comment


    • #3
      Dear Ken:
      Thank you for your response. I should have been more explicit. The output is a vector: note the expression "b0 + b1Χ1 + b2Χ2_bar + .. +.bnXn_bar" in which I'm using the observed values of X1 but the means of X2 through Xn.

      I'm essentially following the long way you suggested. My code looks like:
      Code:
      display _b[_cons] + _b[weight]*weight + _b[mpg]*`=mmpg' + _b[headroom]*`=mheadroom' + _b[length]*`=mlength'
      I was hoping the margins command would have more options to do what I need it to do.

      Comment


      • #4
        I see. Sorry about the misunderstanding. In that case I think your way may be more direct. Perhaps change that into a -gen- command so that the results will be added to your data set.

        Comment


        • #5
          The help for margins says

          at((means) _all (asobserved) x2) is a convenient way to set all covariates except x2 to the mean.

          Is that what you want?

          Having said that, in an OLS regression I don't think any of this much matters -- I think you get the mean of Y whatever method you use.
          -------------------------------------------
          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