3

I was deseasonalizing a time trend and I realized that data.table's frollmean() and forecast's ma() produce slightly different results with even order (ex: quarterly data, n = 4). At first, I thought the difference between frollmean(n = 4) and ma(order = 4) was just because ma() has a rounding up method. From the documentation:

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.

However, as you can below, even when averaging frollmean(n = 4) and frollmean(n = 5), the difference dif is nonzero and consistently above 0 (for this arbitrary time series). This does not occur for odd order (ex: n = 3). Any ideas why?

# 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)

1 Answers1

2

I think what it means is that ma is averaging the two 4 length rollmeans, one slightly advanced of centre one slightly lagging. I.e.

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