I'm quite new in time series and I'm wondering, is there any similar function as sma()
from smooth
package to fit weighted moving average (WMA) for my time serie?
I would like to fit a WMA model with weights
weights <- (1/35)*c(-3, 12, 17, 12, -3)
I'm able to calculate the values of WMA with filter()
function but I would love to get output similar to sma()
function (including e.g. residuals, BIC, ...)
Income <- c(44649, 47507, 49430, 51128, 54453, 58712, 60533,
63091, 63563, 62857, 62481, 63685, 65596)
Year <- c(2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012)
df <- data.frame(Year, Income)
library(smooth)
# simple moving average model
(sma5_fit <- sma(df$Income, order = 5))
# weighted moving average
wma5 <- filter(df$Income, filter = (1/35)*c(-3, 12, 17, 12, -3), sides = 2)
Any suggestions welcomed!
EDIT: It would be also nice to calculate first 2 and last 2 values of weighted moving average. Now, I have to calculate them by hand with following code (weights come from Kendall's Time Series book):
n <- length(Income)
wma5[n-1] <- sum(1/35 * c(2, -8, 12, 27, 2) * c(Income[(n-4):(n)]))
wma5[n] <- sum(1/70 * c(-1, 4, -6, 4, 69) * c(Income[(n-4):(n)]))
wma5[2] <- sum(1/35 * c(2, 27, 12, -8, 2) * c(Income[1:5]))
wma5[1] <- sum(1/70 * c(69, 4, -6, 4, -1) * c(Income[1:5]))