With no claims to being the most efficient way, here is one method:
qui {
egen group = group(company product)
gen volatility = .
tsset group year
forv i = 1/`=r(imax)' {
reg sales year if group == `i'
predict res if e(sample), res
replace volatility = res if group == `i'
drop res
}
}
It is not clear to me how you are constructing the weights, but this might
be what you are after:
bysort group: gen weight = sales[_N]
table company [aw =weight], c(mean vol)
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Jason Hwang
> Sent: Thursday, July 14, 2005 5:05 PM
> To: [email protected]
> Subject: st: Detrending/Summarizing Panel Data
>
> Hi, would someone help me with this problem?
>
> I have millions of observations of the form:
>
> company product year sales
> 100 4 1990 312
> 100 4 1991 400
> 100 4 1992 350
> 100 4 1993 280
> 100 4 1994 302
> ...
>
> I'm trying to measure the volatility of sales after taking out a
> company-product-specific time trend.
>
> If there was just one company-product, then I could regress sales on time,
> then measure the volatility of the residuals. Given that I have tens of
> thousands of company-pairs though, what would be the most efficient way of
> doing this?
>
> Also, I would like to ultimately generate a table showing for each
> company, average sales volatility for all products sold by the company,
> weighted by last period's sales volume. This seems like something that
> should be easy to do once I've detrended sales for each company-product.
> I would appreciate pointers to the appropriate commands.
>
> Thank you very much for your help.
>
> Jason Hwang
*
* 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/