Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Moving Averages


From   "Scott Merryman" <[email protected]>
To   <[email protected]>
Subject   st: Re: Moving Averages
Date   Wed, 21 May 2003 20:33:56 -0500

----- Original Message -----
From: "Jonathan M. McGaharan" <[email protected]>
To: <[email protected]>
Sent: Wednesday, May 21, 2003 2:14 PM
Subject: st: Moving Averages


> I am trying to construct a moving average variable in a panel data set.
Can anyone give me some advice? I want a variable that is the average of the
previous X prices. So far the only solutions I have come up with take way
too much time to compute for even the previous 5 obs, much less the 200 I
want. The other problem is I need to restrict the averages so they dont
cross over into another panel
>
>
> thanks so much!
>
> jon * * 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/


Jon,

-findit moving average- brings up, in part,

[TS]    tssmooth ma  . . . . . . . . . . . . . . . . . . Moving-average
filter
        (help tssmooth_ma)

FAQ     . . . . . . . . . . . . . . . . . . . . Moving averages for panel
data
        . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and C. F.
Baum
        12/01   How can I calculate moving averages for panel data?
                http://www.stata.com/support/faqs/stat/moving.html

There is also -egen , filter- part of Nick Cox's -egenmore-

tssmooth ma price_ma  = price, window(200) would produce a 200 lagged period
moving average.

At least on my old windows 98 machine calculating a 200 period lag moving
average over 100 panels took a about 200 seconds.

. set obs 100
obs was 0, now 100
r; t=0.00 20:26:08

. gen id = _n
r; t=0.00 20:26:10

. expand 500
(49900 observations created)
r; t=0.00 20:26:10

. bysort id: gen price = uniform()
r; t=0.22 20:26:12

. by id: gen time = _n
r; t=0.06 20:26:12

. tsset id time
       panel variable:  id, 1 to 100
        time variable:  time, 1 to 500
r; t=0.49 20:26:14

. tssmooth ma price_ma = price, window(200)
The smoother applied was
     by id : (1/200)*[x(t-200) + x(t-199) + x(t-198) + x(t-197) + x(t-196) +
x(t-195) + x(t-194) + x(t-193) + x(t-192) + x(t-191) +
     x(t-190) + x(t-189) + x(t-188) + x(t-187) + x(t-186) + x(t-185) +
x(t-184) + x(t-183) + x(t-182) + x(t-181) + x(t-180) + ...; x(t)=
     price r; t=197.51 20:29:32


Hope this helps,
Scott


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



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