Announcement

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

  • creating a loop for multiple regressions

    Dear Statalist,

    I am a beginner in Stata and panel data, therefore please excuse me beforehand if my question is novice -- and thank you in advance for any input or suggestions.
    I have data on 62 teams, that looks like this:

    Team Week Treat projco hirchsc
    1 1 0 13 16
    1 2 0 13 16
    1 3 0 13 16
    2 1 0 31 70
    2 2 0 31 70
    2 3 0 31 70
    ......
    62 50 1 55 76
    62 51 1 55 76
    62 52 1 55 76

    I want to create a loop that will run a regression for each of the 62 teams, store the coefficients for each of the regressions and plot these. I have tried many times, but either my syntax is not working, or I will get the same regression 62 times, which is not what I want.

    This is my code so far, but it is obviously not right,

    foreach i in Team 1/62 {
    2. xtreg projco hirchsc treat c.treat#c.hirchsc, i(Team)
    3. eststo reg_`i'
    }



    Thank you very much in advance and I hope I am not bothering too much,

    Best wishes,

    Elena.





  • #2
    The will be at least closer to what you want, if indeed you want to run separate regressions on each time, rather than a panel regression across all teams.
    Code:
    forvalues i = 1/62 {
        regress projco hirchsc treat c.treat#c.hirchsc if Team==`i'
        eststo reg_`i'
    }
    although you could write this slightly more briefly as
    Code:
    forvalues i = 1/62 {
        regress projco c.treat##c.hirchsc if Team==`i'
        eststo reg_`i'
    }
    because the ## interaction generates the "main effects" for each variable along with the "interaction effect".

    Comment

    Working...
    X