data.table 的 frollmean() 和 forecast 的 ma() 之间的细微差别,顺序相同

Slight difference between data.table's frollmean() and forecast's ma() with even order

我正在对时间趋势进行去季节性化处理,我意识到 data.table 的 frollmean() 和 forecast 的 ma() 产生的结果略有不同,顺序相同(例如:季度数据,n = 4).起初,我认为 frollmean(n = 4) 和 ma(order = 4) 之间的区别只是因为 ma() 有一个舍入方法。来自文档:

k=(m-1)/2 [m = order]

When an even order is specified, the observations averaged will include one more observation from the future than the past (k is rounded up). If centre is TRUE, the value from two moving averages (where k is rounded up and down respectively) are averaged, centering the moving average.

但是,如下所示,即使对 frollmean(n = 4) 和 frollmean(n = 5) 进行平均,差值 dif 也不为零并且始终大于 0(对于这个任意时间序列)。对于奇数顺序(例如:n = 3)不会发生这种情况。有什么想法吗?

# toy example
set.seed(0)
dt = data.table(x = 1:100 + 10*rnorm(100))
dt[, fm4 := frollmean(x = x, n = 4, align = "center")]
dt[, fm5 := frollmean(x = x, n = 5, align = "center")]
dt[, fm4p5 := .5 * (fm4 + fm5)]
dt[, ma4 := ma(x = x, order = 4, centre = TRUE)]
dt[, dif := fm4p5 - ma4]
plot(dt[["dif"]])
mean(dt[["dif"]], na.rm = TRUE)

我认为这意味着 ma 正在平均两个 4 长度的 rollmeans,一个稍微超前,一个稍微滞后。即

dt[, fm4c := (fm4+shift(fm4))/2]
dt[, sd(fm4c-ma4, na.rm = TRUE)]
#> [1] 5.599379e-15