运行 的移位表示 R 中 rollapply 函数的输出

Shifting of running mean output from the rollapply function in R

我正在尝试绘制具有相应 9 年 运行 平均值的时间序列。我正在使用 "zoo" 包中的 rollapply 函数。

我不知道为什么 "running mean" 时间序列没有正确对齐,即使我更改了函数中的 "align"。

时间序列是从1969年到2009年

这是我使用的数据:

structure(list(Year = 1961:2009, Rain = c(7.6656130268, 8.1981182796, 
14.4514275121, 13.1530337942, 9.2569892473, 14.1592933948, 10.8212829069, 
3.2401689708, 14.5850998464, 9.614093702, 13.1677048572, 4.7452764977, 
20.7346774194, 9.3896697389, 21.9528735632, 22.5482334869, 6.0696620584, 
7.100640041, 4.706154987, 7.9103302611, 9.9548387097, 8.0649001536, 
6.2932888395, 3.8337173579, 23.5, 2.4107142857, 14.7172784575, 
9.7700076805, 7.6785330261, 7.5453917051, 8.8073044123, 7.7576420891, 
17.0896697389, 10.2380952381, 19.1981460882, 7.0900537634, 5.0630184332, 
22.1928955453, 17.3850945495, 14.71593702, 12.7344086022, 6.0408602151, 
8.0338524286, 7.1766513057, 21.8706989247, 10.6695852535, 21.4467185762, 
10.5718894009, 3.9693548387)), .Names = c("Year", "Rain"), class = 
"data.frame", row.names = c(NA, 
-49L))

这是我的脚本:

dat<- read.csv("test.csv",header=TRUE,sep=",")
dat[dat == -999]<- NA
dat[dat == -888]<- 0
dat<-data.frame(dat)

dat$mav <- rollapply(dat$Rain,width=9,mean,fill=NA,align="right")


p <- ggplot(dat, aes(x = Year))
p <- p + geom_line(aes(y = Rain,color="test"))
p <- p + geom_point(aes(y = Rain,color="test"),size=1)
p <- p + geom_line(aes(y=mav, color = "9-year running mean") , lwd = 1)
p <- p + theme(panel.background=element_rect(fill="white"),
         plot.margin = unit(c(0.5,0.5,0.5,0.5),"cm"),
         panel.border=element_rect(colour="black",fill=NA,size=1),
         axis.line.x=element_line(colour="black"),
         axis.line.y=element_line(colour="black"),
         axis.text=element_text(size=20,colour="black",family="serif"),
         axis.title=element_text(size=15,colour="black",family="serif"),
         legend.position = "top")
p <- p + scale_colour_manual(name="",values=c("test"="steelblue4","9-year running mean"="green"))
p <- p + scale_y_continuous(breaks=seq(0,50, by=10),limits=c(0,50), expand=c(0,0))
p <- p + scale_x_discrete(limits = c(seq(1961,2009,9)),expand=c(0,0))
p <- p + labs(x="Year",y="Rainfall(mm/day)")

这是输出图像:

我期待的是:

[a] 运行 平均值的时间序列应该从 1969 开始,最后一个值应该是 2000。但是在输出图像中,时间序列向右移动并在 2009 结束.

[b] 当我将 'align' 设置为 "center" 时,运行 均值从 1965 年开始。

[c] 关于如何在 R 中正确执行此操作的任何建议?

我认为您可能误解了宽度、填充和对齐方式在滚动应用中的工作原理。

vec <- 1:10
rollapply(vec, 5, mean, fill=NA, align='right')
#  [1] NA NA NA NA  3  4  5  6  7  8

它首先取 n=5 个值并计算平均值:

mean(vec[1:5])
# [1] 3

放在哪里?因为我们说 align='right',它把它放在最右边的位置,所以索引 5.

#  [1]  1  2  3  4  5  6  7  8  9 10
#                   ^
#                   3

并且既然你说了 fill=NA,它会保留前面的空格并用 NA

填充它们
#  [1]  1  2  3  4  5  6  7  8  9 10
#       ^  ^  ^  ^
#  [1] NA NA NA NA  3

对于下一次迭代,它取第 2 到第 6 个位置的平均值:

mean(vec[2:6])
# [1] 4

然后它放在第 6 个位置:

#  [1]  1  2  3  4  5  6  7  8  9 10
#                      ^
#  [1] NA NA NA NA  3  4

当我们到达最后一次迭代时,我们正在计算位置 len-n+1 (10-5+1=6) 到 len (10),所以

mean(vec[6:10])
# [1] 8

所以放在最后一个位置

#  [1]  1  2  3  4  5  6  7  8  9 10
#                                  ^
#  [1] NA NA NA NA  3  4  5  6  7  8

因此,因为我们有 width=5fill=NA,所以我们将有 5-1=4 个空间填充 NA。 (如果数据中有更多 NA,可能会有更多。)如果我们选择 width=5 而没有 fill,那么我们将有 5-1=4 个空格 缺失,意思

# [1] 3 4 5 6 7 8

如果我们完成了 width=5, fill=NA, align='left',那么我们应该看到:

rollapply(vec, 5, mean, fill=NA, align='left')
#  [1]  3  4  5  6  7  8 NA NA NA NA

因为我们要求移除NA副,并且我们说将每个宽度为5的window的每个值放在最左边。最后一次迭代(mean(vec[6:10])值为 8) 被放置在宽度为 5 的最后一个 window 的最左侧位置,这意味着右侧有四个空格具有已知的未知值。