问题
I am looking to create a product that takes the values 15 observations before a given instance for an entire dataset.
My dataset has dates (in chronological order) but there are gaps. Here is an example:
date wage_thousands moving_15_day_product
1/1/2000 3 .
1/3/2000 2 .
1/7/2000 3 .
1/10/2000 6 .
1/12/2000 6 .
1/14/2000 2 .
1/15/2000 1 .
1/16/2000 1 .
1/19/2000 2 .
1/21/2000 1 .
1/22/2000 2 .
1/24/2000 3 .
1/26/2000 1 .
1/28/2000 1 .
1/29/2000 2 .
2/1/2000 1 31,104
2/10/2000 5 51,850
2/12/2000 9 233,280
I am looking to automatically create moving_15_day_product since my dataset is big. I tried some variations of inrange
and it did not work, maybe I was using it incorrectly.
Thanks
回答1:
Moving products are easier to think about if you focus on the moving sums of logarithms that you need to exponentiate to get moving products.
The sum of the last 15 days is just the difference between the cumulative sum up to now and the cumulative sum up to 15 days ago.
Gaps are not especially problematic so long as you tsset
the data and apply time series operators.
So, a sketch is
tsset date
gen sumlog = sum(log(wage))
gen prod15 = exp(sumlog - L15.sumlog)
However, I have not tested this. Also, a statistical rather than programming comment is that working with moving products seems awkward compared with just working on a logarithmic scale, but your choice of measure may be dictated by the conventions of your field.
来源:https://stackoverflow.com/questions/14608369/stata-moving-finite-product