来自 zoo 包的 rollmean returns 意外结果

rollmean from zoo package returns unexpected results

我正在使用此代码:

library(dplyr)
library(lubridate)
library(zoo)

temp <- data.frame(
        date = as.Date(c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01", "2015-11-01", "2015-12-01"))
        , value = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    ) %>%
    arrange(
        date
    ) %>%
    mutate(
        value_rollmean = rollmean(value, k = 2, fill = NA)
    ) 

temp

奇怪returns:

         date value value_rollmean
1  2015-12-01    12             NA
2  2015-11-01    11           11.5
3  2015-10-01    10           10.5
4  2015-09-01     9            9.5
5  2015-08-01     8            8.5
6  2015-07-01     7            7.5
7  2015-06-01     6            6.5
8  2015-05-01     5            5.5
9  2015-04-01     4            4.5
10 2015-03-01     3            3.5
11 2015-02-01     2            2.5
12 2015-01-01     1            1.5 

为什么最后一个条目是 2015 年 12 月 1 日,而不是第一个条目是 2015 年 1 月 1 日?

预期输出:

         date value value_rollmean
1  2015-01-01     1             NA
2  2015-02-01     2             NA
3  2015-03-01     3            1.5
4  2015-04-01     4            2.5
5  2015-05-01     5            3.5
6  2015-06-01     6            4.5
7  2015-07-01     7            5.5
8  2015-08-01     8            6.5
9  2015-09-01     9            7.5
10 2015-10-01    10            8.5
11 2015-11-01    11            9.5
12 2015-12-01    12           10.5

看来我必须对日期进行排序(为什么有人会尝试使用不同方向的移动平均线?!)

library(dplyr)
library(lubridate)
library(zoo)

temp <- data.frame(
        date = as.Date(c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01", "2015-11-01", "2015-12-01"))
        , value = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    ) %>%
    arrange(
        desc(date)
    ) %>%
    mutate(
        value_rollmean = rollmean(value, k = 2, fill = NA)
    ) %>%
    arrange(
        date
    )

temp

很高兴得到纠正,但对于这种情况,我认为您需要使用 rollapply() 来利用 width 参数,该参数似乎在特定的便利条件下不可用职能。作为列表传递的宽度被视为偏移量,因此您可以:

library(zoo)
library(dplyr)

dat %>%
  mutate(value_rollmean = rollapply(value, width = list(-(2:1)), mean, fill = NA)) 

         date value value_rollmean
1  2015-01-01     1             NA
2  2015-02-01     2             NA
3  2015-03-01     3            1.5
4  2015-04-01     4            2.5
5  2015-05-01     5            3.5
6  2015-06-01     6            4.5
7  2015-07-01     7            5.5
8  2015-08-01     8            6.5
9  2015-09-01     9            7.5
10 2015-10-01    10            8.5
11 2015-11-01    11            9.5
12 2015-12-01    12           10.5

数据:

dat <- data.frame(
  date = as.Date(c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01", "2015-11-01", "2015-12-01"))
  , value = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
)