Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 73427

Moving Sum Unbalanced Panel

$
0
0
Hi everyone,

I am trying to generate a rolling sum over the last 12 observations in an unbalanced panel. I tried movingsumm and egen commands but did not succeed, thus I thought about creating a brief loop which also worked elsewhere in a similar intention. However, I am unable to retrieve the desired moving average. Note: I - tsset - my variables already prior to that calculus which are named pvar and tvar.


Data looks like this:

:
 
 clear input byte pvar str10 tvar double alpha  1 "2005m1" . . 1 "2005m2" . . 1 "2005m3" . . 1 "2005m4" 0.03  1 "2005m5" 0.02  1 "2005m6" 0.01  1 "2005m7" 0.04  1 "2005m8" 0.08  1 "2005m9" 0.04  1 "2005m10" 0.08  1 "2005m11" 0.03  1 "2005m12" 0.04  1 "2006m1" 0.01 1 "2006m2" 0.02 1 "2006m3" 0.01 1 "2006m4" 0.09   
 2 "1996m3" . 2 "1996m4" .   end
The code I tried is down below. It should calculate the sum over the last 12 months, which is 0.41 for sum_alpha in 2006m3 and 0.47 in 2006m4. I also tried replacing the missing values by 0 first, but this neither helped.

:
sort pvar tvar
gen sum_alpha = alpha

forvalues i = 1/11 {
     by pvar: replace sum_alpha = sum_alpha+sum_alpha[_n-`i']
}
Is there something I am missing?

Alternatively, I tried this which neither worked (again I - tsset - the panel correctly).

:
sort pvar tvar
by pvar: egen sum_alpha  = sum(L1.alpha-L12.alpha)
It seems such a simple request that I must be missing something in my command lines. Any help is highly appreciated!! Many thanks in advance!

Viewing all articles
Browse latest Browse all 73427

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>