Dear all,
I would like to calculate a moving average of a variable x considering five years of age. I want to do this by year and sex. Hence, in the end I will have a smoothed average x for each age in each year by sex. I do NOT want a moving average over five years (2008, 2009...). Please find an example below.
Some background: My data is provided on the individual level, I want to aggregate it so that I only have aggregated values by cells of age, year, and sex containing the average x. Because the data is so noisy with respect to age, I want to apply a moving average by age.
I know there are several posts dealing with "moving average by group" applying rolling or rangestat, but I did not manage to apply these to my example.
Thanks for your help!
Best,
Stephanie
I would like to calculate a moving average of a variable x considering five years of age. I want to do this by year and sex. Hence, in the end I will have a smoothed average x for each age in each year by sex. I do NOT want a moving average over five years (2008, 2009...). Please find an example below.
Some background: My data is provided on the individual level, I want to aggregate it so that I only have aggregated values by cells of age, year, and sex containing the average x. Because the data is so noisy with respect to age, I want to apply a moving average by age.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(sex age year x) 1 40 2008 .2 1 41 2008 .4 0 40 2008 .2 0 40 2008 1 0 42 2008 .2 1 40 2009 .7 1 40 2009 .8 1 41 2009 .1 1 42 2009 1 0 42 2009 1 end
Thanks for your help!
Best,
Stephanie
Comment