Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Mihaly Fazekas <misi.fazekas@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: creating moving averages for long periods |
Date | Thu, 11 Jul 2013 21:00:06 +0100 |
Thanks a lot Nick for the quick and clear response. It works fine. Actually, I only need running sums rather than running averages (I referred to averages in my post cause I thought that was a more standard problem). Your code works perfectly for running sums after removing the parts ismiss and count parts. Best, Mihaly > 2013/7/11 Nick Cox <njcoxstata@gmail.com>: >> 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 >> njcoxstata@gmail.com >> >> On 11 July 2013 15:03, Mihaly Fazekas <misi.fazekas@gmail.com> 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/ > > > > -- > Mihály Fazekas > PhD candidate > University of Cambridge, UK > Faculty of Politics, Psychology, Sociology and International Studies > www.mihalyfazekas.eu * * 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/