[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Programming question |

Date |
Fri, 4 Mar 2005 18:19:59 -0000 |

Frankly, I think you will be better off starting with -rollreg-, notwithstanding the apparent quirk that Kit Baum is currently looking at. For example, your draft code assumes that your identifiers are all present within a sequence; calculates the SD of residuals even though that is a result left behind by -regress-; wires in your own particular variable names; makes presumptions about the sort order of observations. None of these is necessarily fatal, but the whole means that this program may not bear very much pressure. Rolling regressions is a little tricky as a Stata programming problem. Even if it weren't it would make sense to start from Kit's program as the best that's visible in Stataland. Nick n.j.cox@durham.ac.uk Cameron Hooper > David Kantor wrote: > > At 12:00 PM 3/4/2005 -0500, Cameron Hooper wrote: > > > >> A novice programming question. > >> > >> forvalues i = 1(1)<number of companies> { > >> for values j = 1(1)<number of observations for company `i'> { > >> <do stuff> > >> } > >> } > >> > > > > If you tell us more about what you want to do, then we may > find that you > > really don't need to do all that explicit looping. > Experience has shown > > that it is very rare that you really need such looping. > > > > David Harrison suggested: > > summ id, meanonly > forvalues i = 1/`r(max)' { > qui count if id==`i' > forvalues j = 1/`r(N)' { > <do stuff> > } > } > > Which does what I asked for (thanks David). However my > ultimate goal is > more complex than my original question suggested and upon > using David's > code I've seen a new difficulty. (I'm learning as I go!) Here > is my full > problem. > > I want to estimate rolling regressions on a firm by firm > basis. Then for > each regression I want to find the standard deviation of the > residuals. > Something like: > > program rstdres > version 8.0 > syntax varlist , window(int) gen(string) > generate `gen' = . > tempvar resid > sum id, meanonly > quietly{ > for values i = 1/'r(max)' { > count if id==`i' > while `b' < `r(N)' { > regress y x in `a'/`b' if id == `i' /// See comment below > cap drop `resid' > predict `resid' if e(sample), resid > summarize `resid' > replace `gen' = r(sd) in `b' > local a = `a' + 1 > local b = `b' + 1 > } > }} > > My problem now is getting `a' and `b'. At the moment that are just > placeholders in the code. They represent the rolling window. I don't > know how to set them properly. Here is how they should behave: > > id x y > 1 1 5 > 1 2 6 > 1 4 2 > 2 2 5 > 2 8 9 > 2 6 1 > 2 2 4 > 2 4 9 > > When id == 2 I and `window' = 3 then I want a and b to be: > > Iteration > 1 a = 4 b = 6 > 2 a = 5 b = 7 > 3 a = 6 b = 8 > > Can you suggest how to achieve this. Or am I approaching the entire > problem in the wrong way? * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: Programming question***From:*Cameron Hooper <chooper@umich.edu>

- Prev by Date:
**Re: st: 'Rolling' Slope Dummy** - Next by Date:
**Re: st: Programming question** - Previous by thread:
**Re: st: Programming question** - Next by thread:
**Re: st: Programming question** - Index(es):

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