5

I am using a forval loop to run 3,500 regressions, one for each group. I then need to summarize the results. Typically, when I use loops to run regressions I use the estimates store function followed by estout. Below is a sample code. But I believe there is a limit of 300 that this code can handle. I would very much appreciate if someone could let me know how to automate the process for 3,500 regressions.

Sample code:

forval j = 1/3500 {

    regress y x if group == `j'
    estimates store m`j', title(Model `j')
}

estout m* using "Results.csv", cells(b t)   ///
   legend label varlabels(_cons constant)   ///
       stats(r2 df_r N, fmt(3 0 1) label(R-sqr dfres N)) replace
Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
user1542743
  • 105
  • 1
  • 10
  • Nick and others, I have not been able to get statsby to work. Would appreciate an example showing how to extract the results. Thanks! – user1542743 Dec 18 '13 at 18:21
  • It's the other way round: this site is focused on posters asking for specific advice on specific code. Show us what didn't work. – Nick Cox Dec 18 '13 at 18:35
  • I tried something like this: statsby _b _se, by (group) saving(my_reg): regress ret2 ret2_lag. But then I am not sure what to do after that. How can I see the results? I have searched the web but can't find any example. I know fewer people use Stata today but then this seems like such a standard issue. – user1542743 Dec 18 '13 at 18:38
  • ... and in what sense didn't that work? – Nick Cox Dec 18 '13 at 18:40
  • I changed the code to: statsby _b _se, by (group) saving($path\my_reg.dta): regress ret2 ret2_lag. Seems to be working. Thanks, Nick. If I run into issues as I implement this fully, will write back. Reps to you. – user1542743 Dec 18 '13 at 18:44
  • 3500 regressions might not be instant. – Nick Cox Dec 18 '13 at 18:50
  • Yeah, I am testing right now with a few groups. Once I get what I need, I will implement for the 3,500. – user1542743 Dec 18 '13 at 18:53
  • 1
    You can also use `byvar`: http://ideas.repec.org/c/boc/bocode/s392602.html – Metrics Dec 18 '13 at 19:11
  • You guys are absolutely amazing! I am very thankful to you both for taking the time to respond to my question. Your generosity has saved me so much time and I am very grateful for that. – user1542743 Dec 18 '13 at 19:24

1 Answers1

1

Here's an example using statsby where I run a regression of price on mpg for each of the 5 groups defined by the rep78 variable and store the results in Stata dataset called my_regs:

sysuse auto, clear
statsby _b _se, by(rep78) saving(my_regs): reg price mpg
use my_regs.dta

If you prefer, you can omit the saving() option and then your dataset will be replaced in memory by the regression results, so you won't need to open the file directly with use.

dimitriy
  • 9,077
  • 2
  • 25
  • 50