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

From |
Cameron Hooper <chooper@umich.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Programming question |

Date |
Fri, 04 Mar 2005 13:06:31 -0500 |

David Kantor wrote:

David Harrison suggested:At 12:00 PM 3/4/2005 -0500, Cameron Hooper wrote:A novice programming question.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.

forvalues i = 1(1)<number of companies> {

for values j = 1(1)<number of observations for company `i'> {

<do stuff>

}

}

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?

Thanks.

Cameron

*

* 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:*David Kantor <dkantor@jhu.edu>

**References**:**Re: st: Programming question***From:*David Kantor <dkantor@jhu.edu>

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

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