Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stata -- extract regression output for 3500 regressions run in a loop

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
like image 788
user1542743 Avatar asked Nov 10 '22 16:11

user1542743


1 Answers

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.

like image 137
dimitriy Avatar answered Nov 15 '22 11:11

dimitriy