Announcement

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

  • bysort regress followed by predict

    Good afternoon,

    If I run
    bysort name : regress retrf A B C
    followed by
    predict newvar
    does Stata assign fitted values based on just the last regression? If so, what's the best way of embedding predict so that Stata assigns fitted values based on one regression at a time?

    Thank you very much.

  • #2
    does Stata assign fitted values based on just the last regression?
    Yes, that's what Stata does.

    what's the best way of embedding predict so that Stata assigns fitted values based on one regression at a time
    There are a couple of different approaches. The simplest one, provided you data set is not extremely large, is:
    Code:
    gen newvar = .
    levelsof name, local(names)
    foreach n of local names {
        regress retrf A B C if name == `"`n'"'
        predict prediction
        replace newvar = prediction if name == `"`n'"'
        drop prediction
    }
    Note: Above code assumes that name is a string variable. If it is numeric, then replace `"`n'"' by just `n', throughout.

    Comment


    • #3
      Thank you very much. I'm glad I asked. I wouldn't have thought of something like
      `"`n'"' on my own.

      Comment

      Working...
      X