0

to speed up the code, I would like to export results to a file for every 10 regressions. Is it possible to do something like the following?

forvalues i = 1(1)20{
ppmlhdfe y  ${varlist`i'}, absorb(year) cluster(year)
estimates store result`i'
if mod(`i', 10) == 0 { 
   outreg2 result* using "$outputdir1\results.csv" 
   est clear
}
}

In this pseudo-code, I mean that we save results for every 10 regressions, and clear the estimations in memory move on to the next 10 regressions. Is it possible in Stata?

Angel Chen
  • 67
  • 1
  • 2
  • 10

1 Answers1

0

You can certainly do things conditional on the loop index as in

if mod(`i', 10) == 0 { 

} 

I am not sure that I understand most of the rest of the code. outreg2 is being asked to put stuff in exactly the same file; I don't use outreg2 and have no idea what makes sense there. Similarly, on the face of it, you are storing estimates before you have them.

What is the real saving here? Why execute regressions if you don't wnat to see the results?

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Thanks a lot for your comments Nick! I found that writing results for each regression drag the code slow. So I'm trying to save the estimations and write them once every 10 regressions. In this way, we avoid accessing the hard drive too frequently. Btw, I cleaned up the pseudo-code according to your comments a bit. – Angel Chen May 13 '21 at 14:11
  • Again, I don't really follow. How big is each set of results? Why not write them out once after the loop? – Nick Cox May 13 '21 at 14:52
  • Each set of results is simply one column if we export to a csv file. I timed the code, regressions take 15 seconds to complete but writing to a file on hard drive takes 30 sec. So I'm trying to reduce the number of writes to files. – Angel Chen May 14 '21 at 04:13