Hi all, I'm trying to create a moving average by unit id. My data is panel (country year). I'm trying to create a variable that records the average count of a variable by country (unit id). So for year 1993, if I had data from 1990, the average would be the mean count for 1990, 1991, 1992, excluding the current year.
so the data would look like this:
I've tried two things: egen movave=ma(count) but this gives the error for missing values in the count variable. I read the help and this should treat missings as zero in calculating the count (so year 1994 could be missing but treated as zero). I'm fine with this. However to get the command to work I had to recode the count (.=0). The major concern I have though is that the above is not by unit id--that is the moving average continues across time.
I also tried tssetting the data by tsset unitid year and then running the ma command
tssmooth ma movave = count , window(2 1 2)
However again this does not record the moving average by unitid. My second issue with this is that I do not have a theoretical reasoning of which polynomial I'd use to smooth out the average given as what I want is pretty straightforward. Tssmooth seems to me to be more suitable for say time series stock data that needs a smoothing function.
thus, I think the egen command is a better fit but I don't know how to finetune it to run despite missings and perhaps more importantly, create a moving average by unitid (by country).
I am guessing I'd have to write a loop and run foreach on unitid...but I'm not sure.
Any help would be appreciated! I should also mention that the panel is unbalanced b/c it's country data and there are new countries entering the data of course post-Cold War for instance. I'm mentioning this b/c there are shortcuts with lag specifiers if each panel had the same number of time points.
thanks much in advance!
apologies everyone the above is not what I want. I left it in there as that's a different problem: that is the past 3 years moving average. No what I'd like is a running average. Meaning that it'd calculate the average count from 1990 or whichever is the first date recorded per unitid up till the past year.
so it'd be like this
Yes I think it does require a loop but I can't wrap my mind around it.
so the data would look like this:
unitid | count | movave | |
1990 | 1 | 10 | |
1991 | 1 | 5 | 10 |
1992 | 1 | 10 | 7.5 |
1993 | 1 | 20 | 8.333333 |
1994 | 1 | 0 | 8.75 |
1990 | 2 | 5 | |
1991 | 2 | 5 | 5 |
1992 | 2 | 10 | 5 |
1993 | 2 | 10 | 7.5 |
1994 | 2 | 10 | 10 |
I also tried tssetting the data by tsset unitid year and then running the ma command
tssmooth ma movave = count , window(2 1 2)
However again this does not record the moving average by unitid. My second issue with this is that I do not have a theoretical reasoning of which polynomial I'd use to smooth out the average given as what I want is pretty straightforward. Tssmooth seems to me to be more suitable for say time series stock data that needs a smoothing function.
thus, I think the egen command is a better fit but I don't know how to finetune it to run despite missings and perhaps more importantly, create a moving average by unitid (by country).
I am guessing I'd have to write a loop and run foreach on unitid...but I'm not sure.
Any help would be appreciated! I should also mention that the panel is unbalanced b/c it's country data and there are new countries entering the data of course post-Cold War for instance. I'm mentioning this b/c there are shortcuts with lag specifiers if each panel had the same number of time points.
thanks much in advance!
apologies everyone the above is not what I want. I left it in there as that's a different problem: that is the past 3 years moving average. No what I'd like is a running average. Meaning that it'd calculate the average count from 1990 or whichever is the first date recorded per unitid up till the past year.
so it'd be like this
unitid | count | movave | |
1990 | 1 | 10 | |
1991 | 1 | 5 | 10 |
1992 | 1 | 10 | 7.5 |
1993 | 1 | 20 | 8.333333 |
1994 | 1 | 0 | 9 |
1990 | 2 | 5 | |
1991 | 2 | 5 | 5 |
1992 | 2 | 10 | 5 |
1993 | 2 | 10 | 6.666667 |
1994 | 2 | 10 | 7.5 |
Comment