如何在R中拟合时间序列移动平均线?



我有一个类似于示例的栅格堆栈,周期为62天。我想拟合一个时间序列移动平均线,长度等于62天。在R中怎么做呢?

library(greenbrown)
data("ndvimap")
#here I have an issue as I do not know how to set the length and run the window
#filt<-focal(ndvimap, fun=mean, na.rm=T)#it does not work

有人能帮我吗?

我不明白你是想每62层还是每62天总结一次你的堆栈。然而,对于这两种情况,这里有一个可能的解决方案:

library(greenbrown)
library(raster)
data("ndvimap")
#do the mean every 62 layers
step <- 62
nlayer <- nlayers(ndvimap)
sequence <-
split(1:nlayer, rep(1:round(nlayer / step), c(
rep(step, 5), nlayer - floor(nlayer / step) * step
)))
res <- lapply(sequence,FUN = function(x){
mean <- calc(ndvimap[[x]],mean)
})
res <- stack(res)
#do the mean every 2 layers (~62 days)
step <- 2
sequence <- split(1:nlayer,rep(1:(nlayer/step),each=step))
res <- lapply(sequence,FUN = function(x){
mean <- calc(ndvimap[[x]],mean)
})
res <- stack(res)

最新更新