Limited stats education, so possibly am trying to define something that is a simple function, or why I am unable to find an existing answer
The problem is to calculate a weighted mean for a time series, with greater weight to the most recent data. The weighting should follow "one side of a gaussian curve" function ("S" curve?), highest value starting from the most recent (last) point. I realise there would be a couple coefficients to define the gradient of the curve, but assume "normal"
If you want the curve to be steeper, you can set the sd
argument of dnorm
to less than 1, and if you want it more gradual, increase its value. At the moment the example shows the default, with sd = 1.
EDIT
An alternative that might allow for better control would be a logistic curve:
w <- plogis(seq(-1, 1, length = length(d)), scale = 0.3)
w <- w / sum(w)
plot(w)
w <- plogis(seq(-1, 1, length = length(d)), scale = 0.15)
w <- w / sum(w)
plot(w)
CodePudding user response:
To run a weighted mean along a time series, I would recommend using convolve
for efficiency reason, rather than trying to reimplement it. For instance:
d <- c(7, 8, 10, 7, 8, 11, 9, 6, 13, 10, 11, 11)
k <- dnorm(seq(-2,2, length.out = 5))
convolve(d, k/sum(k), type = "filter")
[1] 9.466427 7.427122 8.213693 10.465371 8.894341 7.066883 11.933909
[8] 10.425011