Question

I have a panelled time series dataset (it's all congressional bills submitted by a given member by session, Year is the time var, Memberid is the panel var). Visually:

Year       Memberid       Sub
1995       01179          4
1996       01179          0
1995       12242          2

What I'm looking to to do is to create a moving average variable across all panels. So, for instance, the moving average will be with average number of bills submitted by ALL members within the last x years. I've tried using tssmooth ma but that only returns the moving average within each panel.

Was it helpful?

Solution

Please post complete code for future questions.

Below some code that can get you started. I stick with your tsmooth ma and make an assumption regarding the weights assigned to each member, by year. Specifically, I average the metric by year and then compute the moving average. You may want to change the weighting scheme.

clear all
set more off

input ///
str1 memberid year metric
"A" 1981 35
"A" 1983 36
"A" 1982 40
"B" 1982 29
"B" 1983 18
"C" 1980 74
"C" 1981 64
"C" 1982 98
end

sort year
list, sepby(year)

* compute mean for each year
by year: egen me = mean(metric)

* keep only one observation per year (means are repeated)
by year: keep if _n == 1
drop memberid metric

list

* compute moving average using one lag, present, and one lead
tsset year
tssmooth ma newmetric=me, window(1 1 1)

list

More succinctly, the lines

by year: egen me = mean(metric)
by year: keep if _n == 1
drop memberid metric

can be substituted for

collapse (mean) metric, by(year)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top