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: creating moving averages for long periods


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: creating moving averages for long periods
Date   Thu, 11 Jul 2013 15:28:48 +0100

An alternative approach follows from the ideas I posted a few days ago at

http://www.stata.com/statalist/archive/2013-07/msg00192.html

but I have to say that your infill zeros should be missings -- or
should just be dropped

This is a sketch. Watch out for fencepost errors (off by one)

local y sz_s_iw_perd_sum
gen byte ismiss = missing(`y')
bysort panel (time) : gen sum = sum(`y' ) - sum(L364.y)
by panel : gen count = sum(ismiss) - sum(L364.ismiss)
gen mave = sum/count

You refer to the -egen- function -filter()-, which is from -egenmore- (SSC).

Nick
[email protected]

On 11 July 2013 15:03, Mihaly Fazekas <[email protected]> wrote:

> I am Mihaly Fazekas from the University of Cambridge working on a
> panel database containing daily data on companies' new contracts
> concluded for a period of several years.
>
> I want to calculate a 12 months running average of each company's
> contract volume (I will actually want total contract volumes but that
> is a simple multiplication once the average is obtained).
> Unfortunately, no running mean technique works as my time-span is too
> long (365 days). I tried the following three approaches without
> success:
>
> tssmooth ma sz_s_iw_sum12=sz_s_iw_perd_sum, w(364 1)
> egen sz_s_iw_sum12 = filter(sz_s_iw_perd_sum), lags(-364/0)
> by iw_id : gen sz_s_iw_sum12
> =L364.sz_s_iw_perd_sum+L363.sz_s_iw_perd_sum+...+sz_s_iw_perd_sum
>
> where sz_s_iw_sum12 is the new variable containing the running average
> for each day and sz_s_iw_perd_sum is the daily contract value for each
> firm in my panel. The panel is unbalanced, but I filled it in with 0-s
> where no contract was concluded. I am using stata 12. I have several
> thousand firms in the panel so efficiency of calculations is
> imperative.
*
*   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