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

st: new answer to old question

From   baum <[email protected]>
To   StataList <[email protected]>
Subject   st: new answer to old question
Date   Tue, 23 Jul 2002 13:51:47 -0400

Last week on Statalist Susan Lewis asked for assistance in computing a trailing 60-day standard deviation from a number of units' timeseries (that is, from a panel). Various inventive strategies were proposed, including a number that used the interaction of 'by' prefix and 'sum' (the running sum function) to apply the stats textbook formula for the variance: sum of squares minus the square of the mean. Although these were nifty solutions, I thought they were in general a very poor way to solve that problem from a numerical analyst's standpoint, since it is well known that (especially in single precision) that formula can be very imprecise. Since I did not have a better mousetrap to offer at the time, I kept quiet.

Now, after some analysis of the problem, here is a better mousetrap, which will also catch a variety of related wildlife. Nick Cox and I wrote 'statsmat', which will generate descriptive stats (including some that summarize won't). A long time ago (Stata 5 days) Nick wrote 'movsumm' which would compute one of summarize's statistics in a moving-window context--but not in a panel. It came to my mind that some bashing of these two elements of code might bring forth something useful. And voila...

'MVSUMM': module to generate moving-window descriptive statistics in time series or panel

mvsumm computes a moving-window descriptive statistic for tsvar
which must be a time series variable under the aegis of tsset.
If a panel calendar is in effect, the statistic is calculated for
each time series within the panel. The moving-window statistic
is placed in a new variable, specified with the generate()
option. The statistics available include minimum, maximum, other
key percentiles, mean and standard deviation: one of these
and/or other statistics returned by {cmd:summarize}, or easily
computable from what it returns, may be specified. aweights or
fweights may be specified. Although mvsumm works with unbalanced
panels (where the start and/or end points differ across units),
it does not allow gaps within the observations of a time series;
that is, the value of an observation for a given period may be
missing, but the observation itself must be defined. Gaps in time
series may be dealt with via the tsfill command.

Distribution-Date: 20020723

Author: Nicholas J. Cox, University of Durham
Support: email [email protected]

Author: Christopher F Baum, Boston College
Support: email [email protected]

Available from SSC via ssc install mvsumm. We thank Nick Winter for suggestions and assistance finding the bugs in an earlier version.

* For searches and help try:

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