Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: how can i make my loop run faster?


From   Stefano Rossi <[email protected]>
To   "[email protected]" <[email protected]>
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index