Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: RE: Calculating Moving Averages with Missing Values

 From "Nick Cox" To Subject st: RE: Calculating Moving Averages with Missing Values Date Tue, 30 Mar 2010 14:12:56 +0100

```Consider this output

. set obs 10
obs was 0, now 10

. gen t = _n

. gen y = _n

. tsset t
time variable:  t, 1 to 10
delta:  1 unit

. tssmooth ma y3 = y, w(1 1 1)
The smoother applied was
(1/3)*[x(t-1) + 1*x(t) + x(t+1)]; x(t)= y

. tssmooth ma y5 = y, w(2 1 2)
The smoother applied was
(1/5)*[x(t-2) + x(t-1) + 1*x(t) + x(t+1) + x(t+2)]; x(t)= y

. l

+---------------------+
|  t    y    y3    y5 |
|---------------------|
1. |  1    1   1.5     2 |
2. |  2    2     2   2.5 |
3. |  3    3     3     3 |
4. |  4    4     4     4 |
5. |  5    5     5     5 |
|---------------------|
6. |  6    6     6     6 |
7. |  7    7     7     7 |
8. |  8    8     8     8 |
9. |  9    9     9   8.5 |
10. | 10   10   9.5     9 |
+---------------------+

-tssmooth ma- fills as best it can at the ends of series (which is _not_
the same as treating missings as 0s, as otherwise the results above
would not be obtained).

Jeff can override the results of -tssmooth- if he doesn't want that.

replace y3 = . if missing(L1.y, F1.y)
replace y5 = . if missing(L1.y, L2.y, F1.y, F2.y)

For example, L1.y is missing at the beginning of a series y and F1.y at
the end. This works with panel data too.

A user-written alternative to -tssmooth- is the -egen- function
-filter()- from -egenmore-.

Alternatively, please spell out whatever way of dealing with ends you
prefer.

Nick
n.j.cox@durham.ac.uk

J.M.Chwieroth@lse.ac.uk

When I use the tssmooth function to create 2 or 5 year moving averages,
Stata calculates these moving averages until the end of the time-series
rather than stopping 1 year (in the case of the 2 year moving average)
or 4 years (in the case of the 5 year moving average) before the end of
the time-series? It thus counts forward missing values as zero.    Would
you know a possible strategy to fix  this problem?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```