Moving Average code in R ~ "ma" function

SamRoy picture SamRoy · Jan 24, 2014 · Viewed 17.2k times · Source

I have some time series data points and I like to perform a simple Moving Average method on them. If I use the function "ma" from package "forecast", I get the following:

library(forecast)
x<-c(1,5,2,8,6,3,2,4,7)
ma(x,order= 4)
[1] NA    NA   4.625   5.000   4.750   4.250   3.875    NA    NA

Now can anybody please tell me what is the logic here? Because obviously this does not follow the usual rule of 4 point simple MA process here.

Answer

Troy picture Troy · Jan 24, 2014

I think forecast has a special smoother of some sort. How about

require(zoo)
rollmean(x,4,,align="center")
# [1] 4.00 5.25 4.75 4.75 3.75 4.00