Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: moving average of the last 55 time points


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: moving average of the last 55 time points
Date   Mon, 30 Jan 2012 10:34:20 +0000

Juan replied privately to me. As the Statalist FAQ explains, you
should _not_ do this.

I've reinstated the original exchange, without which the discussion
makes little sense.

Nick

On Mon, Jan 30, 2012 at 10:10 AM, Juan J. Fernandez
<[email protected]> wrote:>
> Dear Nick,
>
>

> Thank you very much for your prompt response to my post!
>
>
>
> I just tried your suggestion, but I believe that it doesn’t solve the
> problem, at least for my case. –egen , filter() – requires specifying the
> concrete lags. So it can take the moving sum of a fixed set of previous
> years (e.g. last 20 years).

I don't understand what you are getting at here. There is no
difference for a regular panel data setting which you implied in your
first post.

> The first problem is that it doesn´t calculate the average, but just the
> sum. Hence I need the denominator, which is hard to define, because there
> are some isolated missing country-years (so -g = _n-  just takes all years,
> not only those with a value in the IV).

Not so. The -normalise- option gives you a mean.

> The second problem is that –egen , filter() lag() – doesn’t prompt a value
> if there are less effective values than the specified lags. For instance, if
> I specify l(0/30) and for Angola there are no prior 30 values for this IV,
> then egen doesn’t even provide any value for that country.

It's not -egen- that makes that decision; it's -filter()-. It provides
a missing value and the logic is that it can't do what you asked for.
If you want it to do what it can with what is available, that was not
specified in your original post.

> The third problem is that I cannot combine -egen, filter()- with replace.
> This means that I cannot produce a single variable with values obtained with
> variable lags.

You just need two commands, not one. -replace- some variable with the
result of an -egen- calculation. As you can't combine -tssmooth- with
-replace- in this sense either, this is standard in moving average
calculations.

> I guess I will have to find another solution.

I think you would have got better advice if you had explained your
requirements more fully. The technique in

SJ-7-3  pr0033  . . . . . . . . . . . . . .  Stata tip 51: Events in intervals
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/07   SJ 7(3):440--443                                 (no commands)
        tip for counting or summarizing irregularly spaced
        events in intervals

should be adaptable to your problem, whatever it is precisely.
Nick

Earlier:

Nick Cox

Check out -filter()- from -egenmore- (SSC).

On Mon, Jan 30, 2012 at 8:45 AM, Juan Fernandez

> I am trying to obtain moving averages that take into account the
> previous 55 time points (in this case, years) in a panel data setting.
> However I cannot find any Stata command that can solve the problem. To
> my knowledge, -egen, ma()- doesn't do the trick because it cannot be
> combined with -by-. Moreover, -tssmooth ma, window()- doesn't do it
> either because it is limited in the number of lags it can consider.
>
> Is there any way to calculate a moving average for each case that
> takes into account the last 55 time data points (even if there are
> some isolated missings in a few years)?
>
> The data structure is as follows:
>
> Country                   Year         IV
> 1                              1945       20
> 1                              1946       33
> ...                              ...         ...
> 1                              2000       36
> 2                              1945       31
> 2                              1946       27
> ...                              ...         ...
> 2                              2000       29
>

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index