Question

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.

Was it helpful?

Solution

I realize this is an old post but I wanted to try and provide some specifics based on my understanding of the function. As the previous poster mentioned, the default 'centre' argument is 'true' which has the effect of applying an order 2 MA to the MA specified in the function if the order specified is even. This means that 'centre' equals 'true', given an order specified of 4 results in a 2X4 MA. This converts an asymmetrical MA to a centered one of the order specified. A centered 4 MA is also a weighted MA of order 5 with the weights taking on (1/8th,1/4th,1/4th,1/4th,1/8th). If your data were say quarterly, this ensures that each quarter within a year gets equal weight; if you were currently at Q2 the previous and upcoming Q4 values would each be weighted 1/8th and give Q4 overall the same weighted contribution as other quarters.

OTHER TIPS

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

Author of the forecast package posted an answer at https://stats.stackexchange.com/questions/83194/using-moving-average-smoothing-in-forecast-package. Set centre=FALSE and shift result by order/2.

x<-c(1,5,2,8,6,3,2,4,7)
.ord=4
ma(x,order=.ord, centre=FALSE)[(ceiling(.ord/2):(length(x)-floor(.ord/2))]
# [1] 4.00 5.25 4.75 4.75 3.75 4.00

or just drop the na's with centre=FALSE; then pad front with .ord-1 NA's if you want length(x) return value

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top