计算移动平均线的方法有很多。
在 r 基数中,这可以工作
filter(x, rep(1/2,2)) #this calculates moving average of 2 numbers in a sequence
filter(x, rep(1/3,3)) #this calculates moving average of 3 numbers in a sequence
对于 k 个连续观察
filter(x, rep(1/k,k))
e.g.
x <- c(3,5,7,3,4,2,6,4,7,2,1,9, 1, 10, 1,12)
filter(x, rep(1/2,2))
# [1] 4.0 6.0 5.0 3.5 3.0 4.0 5.0 5.5 4.5 1.5 5.0 5.0 5.5 5.5 6.5 NA
您还应该查找以下软件包:zoo
and TTR
套餐提供更多选择
举个简单的例子,该函数runMean
TTR 超级简单
runMean(x,2) #gives rolling mean of every 2 consecutive observations
runMean(x,k) #gives rolling mean of every k consecutive observations