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.. conditioning on a date


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: moving average.. conditioning on a date
Date   Mon, 5 Nov 2012 00:48:55 +0000

If I understand this correctly, Francesco wants running sums to be
calculated within spells of consecutive days, and separately by
panels.

Identifying spells of consecutive dates is an FAQ

FAQ     . . . . . . Identifying runs of consecutive observations in panel data
        . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and V. Wiggins
        8/02    How do I identify runs of consecutive observations
                in panel data?
http://www.stata.com/support/faqs/data-management/identifying-runs-of-consecutive-observations/

One solution covered there is to use -tsspell- from SSC

. tsset id date
. tsspell, f(L.date == .)
. bysort id _spell (_seq) : gen Y = sum(X) if _spell
. by id _spell : gen aveY = Y/_seq

Here I am guessing that the moving average required is running
sum/number in sequence

I am also guessing that the example should end

 1    sep-7   1    3

Note that unbalanced panels in no sense rule out the use of time
series operators.

The -egen- function -filter()- is from -egenmore- (SSC).

See also

SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata: Identifying spells
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/07   SJ 7(2):249--265                                 (no commands)
        shows how to handle spells with complete control over
        spell specification


Nick

On Sun, Nov 4, 2012 at 10:50 PM, Francesco <[email protected]> wrote:

> Many thanks for your suggestion but unfortunately it does not fit...
> movavg only computes moving average over the last n periods... which
> is not what I want
>
> In order to be more clear, my dataset looks like
>
> ID  date     X   Y
> 1    jan-1   1    1
> 1    feb-4   1    1
> 1    feb-5   1    2
> 1    sep-5   1    1
> 1    sep-6   1    2
> 1    sep-7   1    2
>
> Here Y is the running sum of X for values of X that occur at most 1
> days before the current observation ...
> Is there a way to obtain this variable (and a moving average version too) ?

 On 4 November 2012 23:35, Steve Samuels <[email protected]> wrote:

>> Sorry for the typo: The command is -movavg-. If you download it and read
>> the -help-, you will see that it does not require balanced panels.

On Nov 4, 2012, at 5:24 PM, Francesco wrote:

>> Well, Thank you for your kind suggestion (I already checked tssmooth
>> and egen filter) but I cannot find what I am looking for (in
>> particular for the running sum)
>> As the panel is unbalanced, I think I cannot use the L. operator ...

On 4 November 2012 22:51, Steve Samuels <[email protected]> wrote:

>>> The Statalist archives are not a good place to look for
>>> commands. Try -findit- first.  "findit moving average" would
>>> have turned up -movag- at SSC. If -findit- had not been successful.
>>> then a second  step would have been to Google "Stata moving average".

On Nov 4, 2012, at 3:58 PM, Marco Francesco wrote:

>>> I looked into the archives, and still I am not sure how to proceed :
>>>
>>> I have a panel dataset (id - day) and I would like to obtain a variable X that keeps the running sum (or the average) of variable Y's observations that occur in a given moving time window : that is between today's observation date and (today's observation date - X) where X is fixed.

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


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