Announcement

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

  • "As if" prediction for each observation

    Suppose I've run the following regression on the auto database

    Code:
    regress price i.foreign##c.mpg
    I understand that

    Code:
     margins, at(foreign=(0 1))
     //or equally well
     margins foreign
    will give me the average predicted price treating every car as domestic and treating every car as foreign. What I hope to be able to get, however, is two predictions for each observation, as if it were domestic and as if it were foreign. Something like:

    Code:
    predict xb_domestic_hat, xb asif(foreign=0)
    predict xb_foreign_hat, xb asif(foreign=1)
    Unfortunately, the "asif" option doesn't seem to actually exist...

    Have I overlooked a way to do this? Thank you for any suggestions.
    _______________________________________

    Glenn Hoetker
    Professor in Business Strategy

    Melbourne Business School, University of Melbourne
    200 Leicester Street, Carlton, Victoria 3053, Australia
    Email: [email protected]

    I acknowledge the Traditional Owners of the land on which I work, the Wurundjeri people of the Kulin Nations, and pay my respects to their Elders, past and present.

  • #2
    Code:
    clear all
    sysuse auto
    
    regress price i.foreign##c.mpg
    
    // store a copy of foreign
    clonevar copy = foreign
    
    // predict as if domestic
    replace foreign = 0
    predict xb_domestic_hat, xb
    
    // predict as if foreign
    replace foreign = 1
    predict xb_foreign_hat, xb
    
    // restore foreign
    replace foreign = copy
    drop copy
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      And for a visualization of the result:
      Code:
      twoway (scatter price mpg) (scatter xb_hat mpg, ms(Oh) msize(*2)) ///
          (scatter xb_domestic_hat mpg, ms(Oh)) (scatter xb_foreign_hat mpg, ms(Oh))    ///
          , legend(rows(1) order(1 2 3 "As if domestic" 4 "As if foreign"))
      which renders:
      Click image for larger version

Name:	GraphAsIfPrediction.png
Views:	1
Size:	70.3 KB
ID:	1447668
      http://publicationslist.org/eric.melse

      Comment

      Working...
      X