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

st: RE: Novice Question...


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Novice Question...
Date   Sat, 17 May 2003 21:06:32 +0100

Jonathan M. McGaharan

> I recently downloaded the following program to compute
> moving averages.  I have never used a nonstandard stata
> program before and cannot get it to work.  I created a
> folder c:\ado\personal and copied the code into a file
> named mavg.ado.  I have version 7 of stata if that makes a
> difference.  I get an error when stata tries to load the
> command.  Any ideas?
>
>
> thanks!
>
>  mavg <varname>, generate(<newvarname>) t(n)
> Where n is the number of preceding observations you want to average.
>
> /* mavg begins here */
>
> program mavg
>         version 8.0
>         syntax varname(numeric) , Generate(string)
> t(string) [missing]
>
>         capture confirm new variable `generate'
>         if _rc {
>                 di as err "`generate' invalid new variable name"
>                 exit 198
>         }
>         capture confirm integer number `t'
>         if _rc {
>                 di as err "t() must be an integer"
>                 exit 198
>         }
>
>         local k = `t' - 1
>         local N = _N
>         tempvar copyvar sumvar
>         qui gen double `copyvar' = `varlist'
>         if "`missing'" == "" {
>                 qui replace `copyvar' = 0 if `copyvar' >= .
>         }
>         quietly gen double `sumvar' = .
>
>         *-------
>         * handle _n >= t
>
>         forvalues obs = 1/`N' {
>            local sum = 0
>            forvalues i = 0/`k' {
>               local sum = `copyvar'[`obs'-`i'] + `sum'
>            }
>            quietly replace `sumvar' = `sum' in `obs'
>         }
>
>         quietly generate `generate' = `sumvar' / `t'
>
>         *-------
>         * handle observations 1 - `t'
>
>         forvalues j = 1/`k' {
>            local sum = 0
>            forvalues i = 1/`j' {
>               local sum = `copyvar'[`i'] + `sum'
>            }
>            quietly replace `sumvar' = `sum' in `j'
>            quietly replace `generate' = `sumvar' / `j' in `j'
>         }
> end
>
>
> /* mavg.ado ends here */

I can't find this program with -findit- and don't know
its provenance. Perhaps anyone who wrote it or who knows
who wrote it can comment, and/or relay the comments
below to the author(s). Incidentally it is generally
regarded as good practice for a user-written program to include
some indication of authorship and date.

The line

version 8.0

at the beginning of the program indicates that it requires Stata 8.
However, looking at the code, I cannot see that this is strictly
true. If you edited the first two lines to

program def mavg
	version 7

I think it would work for you. However, I have another suggestion,
for the following reasons:

1. This program is less general than others available to you.
You can get the moving average of the last # values, but
no weighting is allowed, among several other limitations.

2. It uses a very slow algorithm, although in practice that
is probably trivial.

3. It also has a puzzling feature, a default procedure which
replaces missings in data by 0 before averaging. On the
face of it, that is pretty extraordinary.

4. This program is not linked to -tsset-, so it may not
perform properly for gaps, it cannot be used for panel data,
and it depends entirely on your having the data in the
correct -sort- order.

Naturally some or all of these facts may be documented
in a help file or elsewhere.

My other suggestion therefore is to turn out to a more general
program. -egen, filter()- within the package -egenmore- on SSC
is one with which I am familiar. The first URL below my signature
gives advice on SSC.

In addition, some of the detail at

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

may be of interest.

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