Announcement

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

  • Save regression coefficients and number of obs in new variable

    Dear Statalist users,

    I would like to run a regression by different groups (roughly 1000), saving regression coefficients and number of observations in a new variable, such that for each individual observation in the group I have a variable showing the respective regression coefficient that the regression for that group yielded.

    I have had a look at combining by, sort and regress:
    Code:
    sort group
    by group: regress y x
    and, alternatively, statsby
    Code:
    statsby _b, by(group): regress y x
    but was not able to find out how to store the coefficient values in a new variable.

    How can I do it? And, what is the most convenient way of doing this? Do you have any other suggestions?

    Many thanks,
    Milan

  • #2
    Since this is precisely what statsby does for you, it is not clear what you are unclear about. But consider this example


    Code:
    sysuse auto, clear
    statsby n=e(N) _b , by(rep78) : regress mpg weight
    
    * optional!
    merge 1:m rep78 using auto
    egen tag = tag(rep78) 
    The next to last line shows that you can have the results as new variables in your original dataset should you wish. But watch out for multiple counting; the tag variable you can create with egen can be useful there. See the help for egen.
    Last edited by Nick Cox; 05 Dec 2016, 04:37.

    Comment


    • #3
      Thank you very much. Apparently, I had misunderstood how the command works. I should have read the description more carefully. Now that I have gone through your example, it seems very straightforward. Apologies for the question and thank you for the excellent clarification.

      Comment

      Working...
      X