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]

Re: st: making code faster


From   Christopher Baum <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: making code faster
Date   Tue, 15 May 2012 09:21:22 -0400

<>
On May 15, 2012, at 2:33 AM, Rudy wrote:

I have a lot of regressions to run (on stock returns of firms over a
periof of 7 years) in order to retrieve the residuals.

I am using the code


gen touse = !missing(excessreturn, marketreturn, firmno, year)

bysort touse firmno year : replace touse = 0 if _N == 1

egen group = group(firmno year) if touse

su group

local max = r(max)

gen residuals = .

qui forval i = 1/`max' {

reg excessreturn marketreturn if group == `i'

predict temp, residuals

replace residuals=temp if group == `i'

drop temp

As is often discussed on this list, -if- is significantly slower than -in- when you have many groups. Making one pass over the data to compute the first and last observation in each group (or mechanically, if it is a balanced panel) doesn't take that long. In your case, you can look for the first date and last date available for each firm-year; those are the boundaries to be passed to -in-.

Kit

Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
                             An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
  An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html


*
*   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