I'm looking to calculate a specific type of 'diminishing' sum or decay value within a time series.
For instance, say I have some simple data:
thedata <- data.frame(magicseeds = c(30,20,10,40,20)
,week = seq(from = 1, to = 5, by = 1))
I would like to calculate the value of 'magicseeds', such that it has a diminishing impact over time. For instance, lets say each value has 20% of the value the following week, so the first value in the magicseeds column (30) would be 30*0.2+6*0.2 and so on until the time series ends(=7.488). The calculation for the last value (20) would be 0 as it is the last value in the time series.
The filter function is close, but is a cumulative sum that takes into account the total time series eg.
thedata$filtervalues <- with(thedata, as.numeric(stats::filter(magicseeds, filter=0.2, method="recursive")))
I've been testing out the rollapply function as well with little luck.
Final output would be this:
magicseeds week filtervalues calculation
1 30 1 7.488 sum(30*.20^1, 30*.20^2, 30*.20^3, 30*.20^4)
2 20 2 4.96 sum(20*.20^1, 20*.20^2, 20*.20^3)
3 10 3 2.4 sum(10*.20^1, 10*.20^2)
4 40 4 8.000 sum(40*.20^1)
5 20 5 0.000
UPDATE: This question has more or less been answered here: Iterate and sum a calculation across rows by varying row position