Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: how can i make my loop run faster?


From   Stefano Rossi <sr525@cornell.edu>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: how can i make my loop run faster?
Date   Sun, 18 Sep 2011 19:52:11 -0400

Dear Statalist Users,

I wonder if you can help me make a faster loop?
I have an unbalanced panel of about 30,000 firms and 200 periods, and for each "firm i-period t" pair I need to run 10 regressions on the 12 observations from t-1 to t-12 of the same firm i, and another 10 regressions on the observations from t+1 to t+12 of the same firm i.  I have come up with the following program, which works well as it does what it should do, but it is very slow (due to the many ifs I suspect) - here's a simplified version of it with just two regressions:

forval z = 1/30000 {
levelsof period if firm==`z', local(sample) 
foreach j of local sample {
       local k = `j' - 13
       capture reg y x if firm ==`z' & period<`j' & period>`k' & indicator==1
       if _rc==0 { 
       predict y_hat, xb
       replace before = y_hat[_n-1] if firm == `z' & period == `j' 
       drop y_hat
       }
       local w = `j' + 13
       capture reg y x if firm ==`z' & period>`j' & period<`w' & indicator==1
       if _rc==0 { 
       predict y_hat, xb
replace after = y_hat[_n+1] if firm == `z' & period == `j' 
       drop y_hat
       }
       }
}

Right now, it takes several minutes for each firm, so if I run it for the whole sample it would take weeks.
Is there any way to make it (a lot) faster? 


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index