Hello Stata users,
I would like to see how the average values based on a specific time window affect future values up to five calendar years in the future. Specifically, in a time series analysis, how the average values of the past three calendar years affect the future values over the next (1, 2, 3, 4, 5) calendar years. I have provided a minimal example below.
Having this example in mind, one can see that we can take averages within the specific three year window multiple times, as we have rolling three year averages. For example, the average value for the years 1981, 1982, and 1983, based on var_x, will affect that variable in the years '84, '85, '86, '87, and '88. This process continues for all other potential combinations as time progresses.
I have seen commands such as: rolling, mvsumm, tsegen that could deal with rolling averages, however, when I tried them I got the error that the panel has repeated time values, which is to be expected, as there might be multiple events in the same year.
What I tried, is to collapse the data based on id-year and then I used the following code:
Nonetheless, I am not happy with this, as I would like the whole process to happen in the original file.
Eventually, I would like to run regressions of future values of var_x on past_val (of course, the example here is too small, so something like that cannot be performed).
I would be grateful for any assistance in this matter.
I would like to see how the average values based on a specific time window affect future values up to five calendar years in the future. Specifically, in a time series analysis, how the average values of the past three calendar years affect the future values over the next (1, 2, 3, 4, 5) calendar years. I have provided a minimal example below.
Code:
clear all set more off input /// id year var_x 1 1975 0.475697247 1 1981 0.075769328 1 1982 0.904638688 1 1983 0.4612996 1 1983 0.955458246 1 1983 0.014580401 1 1985 0.380078561 1 1985 0.340454901 1 1987 0.008309949 1 1987 0.159163017 1 1987 0.763778463 1 1989 0.003309207 2 1995 0.37667648 2 1998 0.547401227 2 1999 0.294790755 2 2001 0.484209626 2 2001 0.040294883 2 2001 0.03179457 2 2002 0.431170685 2 2003 0.743045929 2 2003 0.278046652 2 2003 0.233674965 2 2004 0.71374945 end
I have seen commands such as: rolling, mvsumm, tsegen that could deal with rolling averages, however, when I tried them I got the error that the panel has repeated time values, which is to be expected, as there might be multiple events in the same year.
What I tried, is to collapse the data based on id-year and then I used the following code:
Code:
bysort id (year): gen Past_1YR = var_x[_n-1] if year[_n] - year[_n-1] == 1 bysort id (year): gen Past_2YR = var_x[_n-2] if year[_n] - year[_n-2] == 2 bysort id (year): replace Past_2YR = var_x[_n-1] if year[_n] - year[_n-1] == 2 bysort id (year): gen Past_3YR = var_x[_n-3] if year[_n] - year[_n-3] == 3 bysort id (year): replace Past_3YR = var_x[_n-2] if year[_n] - year[_n-2] == 3 bysort id (year): replace Past_3YR = var_x[_n-1] if year[_n] - year[_n-1] == 3 egen Past_val = rmean(Past_1YR Past_2YR Past_3YR) drop Past_1YR Past_2YR Past_3YR
Eventually, I would like to run regressions of future values of var_x on past_val (of course, the example here is too small, so something like that cannot be performed).
I would be grateful for any assistance in this matter.
Comment