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

st: RE: Generating 'moving sum' variable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Generating 'moving sum' variable
Date   Wed, 3 Sep 2003 15:48:48 +0100

KRISTIEN VERHEYEN
 
> I have a survival-time dataset with daily observations per 
> subject. One of
> the (numeric) variables represents daily exercise distance. 
> I now want to
> create a new variable that represents the sum of exercise 
> distance in the
> previous week (or 7 days). It sounds similar to the 'moving 
> average' option
> for egen but there doesn't seem to be a 'moving sum' 
> option. 

This is a linear filter with weights 1 1 1 1 1 1 1. 
Alternatively, it is a just an equally-weighted moving
average, multiplied by 7. Note, however, that -- strange
though it may seem -- -egen, ma()- is a fairly poor 
way of calculating moving averages, and has long since
been superseded by more flexible commands: 

1. Given an expression, -egen, ma()- creates a #-period moving 
average of that expression. By default, # is taken as 3. 
# must be odd (not a problem is this case). 

2. -egen, ma()- may not be combined with -by varlist:-, 
and for that reason alone it is not applicable to panel data. 

3. In any case, it stands outside the set of commands 
specifically written for time series. 

4. -egen, ma()- does not support unequal weights (not 
a problem in this case). 

Alternative approaches: 

First, -tsset- your data if not done already. 

Stata 7 or Stata 8 
==================

The -egen- function -filter()- is in the user-written 
package -egenmore- on SSC. 

Thus 

. ssc inst egenmore
. whelp egenmore
. egen movsumm = filter(exercise_distance), c(1 1 1 1 1 1 1) 
lags(1/7) 

Stata 8
=======

-tssmooth ma- doesn't seem to allow general linear
filters, but in this case you have a choice: 

either 

. tssmooth ma movsumm = exercise_distance, we(1 1 1 1 1 1 1 <0>)

or 

. tssmooth ma movsumm = exercise_distance, w(7) 

In both cases, 

. replace movsumm = movsumm * 7 

The two differ in how far they fill in stuff at the beginning
of the series. In the case of -egen, filter()- this is 
explained at 

http://www.stata.com/support/faqs/stat/moving.html

Nick 
[email protected] 

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