fit = ses(pigs, h = 4)
print(fit$model)
## Simple exponential smoothing
##
## Call:
## ses(y = pigs, h = 4)
##
## Smoothing parameters:
## alpha = 0.2971
##
## Initial states:
## l = 77260.0561
##
## sigma: 10308.58
##
## AIC AICc BIC
## 4462.955 4463.086 4472.665
print(fit)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Sep 1995 98816.41 85605.43 112027.4 78611.97 119020.8
## Oct 1995 98816.41 85034.52 112598.3 77738.83 119894.0
## Nov 1995 98816.41 84486.34 113146.5 76900.46 120732.4
## Dec 1995 98816.41 83958.37 113674.4 76092.99 121539.8
print(paste('Prediction interval of R', round(fit$lower[1, '95%'],2), '-', round(fit$upper[1,'95%']),2))
## [1] "Prediction interval of R 78611.97 - 119021 2"
print(paste('Prediction interval using residuals', round(fit$mean[1] - 1.96 * sd(fit$residuals),2), '-', round(fit$mean[1] + 1.96 * sd(fit$residuals),2)))
## [1] "Prediction interval using residuals 78679.97 - 118952.84"
autoplot(books) +
ggtitle('Daily sales of paper and hardcover books sale')
fit_pb_ses <- ses(books[, "Paperback"], h = 4)
fit_hc_ses <- ses(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(fit_pb_ses, series = "Paperback", PI = FALSE) +
autolayer(books[, "Hardcover"], series = "Hardcover") +
autolayer(fit_hc_ses, series = "Hardcover", PI=FALSE) +
ylab("Books Sale") +
ggtitle("Daily Paperback and hardcover books sales")
print(paste('RMSE for Paperback book sales =' , accuracy(fit_pb_ses)[2], 'RMSE for Hardcover books sale =', accuracy(fit_hc_ses)[2]))
## [1] "RMSE for Paperback book sales = 33.637686782912 RMSE for Hardcover books sale = 31.9310149844547"
fit_pb_h <- holt(books[, "Paperback"], h = 4)
fit_hc_h<- holt(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(fit_pb_h, series = "Paperback", PI = FALSE) +
autolayer(books[, "Hardcover"], series = "Hardcover") +
autolayer(fit_hc_h, series = "Hardcover", PI=FALSE) +
ylab("Books Sale") +
ggtitle("Daily Paperback and hardcover books sales")
print(paste('RMSE for Paperback book sales =' , accuracy(fit_pb_h)[2], 'RMSE for Hardcover books sale =', accuracy(fit_hc_h)[2]))
## [1] "RMSE for Paperback book sales = 31.1369230162347 RMSE for Hardcover books sale = 27.1935779818511"
print(paste('Prediction interval of R SES (Papeback)', round(fit_pb_ses$lower[1, '95%'],2), '-', round(fit_pb_ses$upper[1,'95%']),2))
## [1] "Prediction interval of R SES (Papeback) 138.87 - 275 2"
print(paste('Prediction interval of R SES (Hardcover)', round(fit_hc_ses$lower[1, '95%'],2), '-', round(fit_hc_ses$upper[1,'95%']),2))
## [1] "Prediction interval of R SES (Hardcover) 174.78 - 304 2"
print(paste('Prediction interval using RMSE SES (Paperback)', round(fit_pb_ses$mean[1] - 1.96 * accuracy(fit_pb_ses)[2],2), '-', round(fit_pb_ses$mean[1] + 1.96 * accuracy(fit_pb_ses)[2],2)))
## [1] "Prediction interval using RMSE SES (Paperback) 141.18 - 273.04"
print(paste('Prediction interval using RMSE SES (Hardcover)', round(fit_hc_ses$mean[1] - 1.96 * accuracy(fit_hc_ses)[2],2), '-', round(fit_hc_ses$mean[1] + 1.96 * accuracy(fit_hc_ses)[2],2)))
## [1] "Prediction interval using RMSE SES (Hardcover) 176.98 - 302.14"
print(paste('Prediction interval of R Holt (Papeback)', round(fit_pb_h$lower[1, '95%'],2), '-', round(fit_pb_h$upper[1,'95%']),2))
## [1] "Prediction interval of R Holt (Papeback) 143.91 - 275 2"
print(paste('Prediction interval of R Holt (Hardcover)', round(fit_hc_h$lower[1, '95%'],2), '-', round(fit_hc_h$upper[1,'95%']),2))
## [1] "Prediction interval of R Holt (Hardcover) 192.92 - 307 2"
print(paste('Prediction interval using RMSE Holt (Paperback)', round(fit_pb_h$mean[1] - 1.96 * accuracy(fit_pb_h)[2],2), '-', round(fit_pb_h$mean[1] + 1.96 * accuracy(fit_pb_h)[2],2)))
## [1] "Prediction interval using RMSE Holt (Paperback) 148.44 - 270.5"
print(paste('Prediction interval using RMSE Holt (Hardcover)', round(fit_hc_h$mean[1] - 1.96 * accuracy(fit_hc_h)[2],2), '-', round(fit_hc_h$mean[1] + 1.96 * accuracy(fit_hc_h)[2],2)))
## [1] "Prediction interval using RMSE Holt (Hardcover) 196.87 - 303.47"
fit.holt <- holt(eggs, h=100)
fit.holt.damped <- holt(eggs, damped=TRUE, h=100)
fit.holt.boxcox <- holt(eggs, lambda = BoxCox.lambda(eggs), h=100)
fit.holt.boxcox.damped <- holt(eggs, lambda = BoxCox.lambda(eggs), h=100, damped=TRUE)
autoplot(eggs) +
autolayer(fit.holt, series="Holt's method", PI=FALSE) +
autolayer(fit.holt.damped, series="Damped Holt's method", PI=FALSE) +
autolayer(fit.holt.boxcox, series="Box-Cox", PI=FALSE)+
autolayer(fit.holt.boxcox.damped, series="Box-Cox-Damped", PI=FALSE)+
ggtitle("Forecasts from Holt's method") + xlab("Year") +
ylab("Eggs") +
guides(colour=guide_legend(title="Forecast"))
print(paste('Accuracy Holt', accuracy(fit.holt)[2]))
## [1] "Accuracy Holt 26.5821881569903"
print(paste('Accuracy Holt Damped', accuracy(fit.holt.damped)[2]))
## [1] "Accuracy Holt Damped 26.5401852798325"
print(paste('Accuracy Holt BoxCox', accuracy(fit.holt.boxcox)[2]))
## [1] "Accuracy Holt BoxCox 26.3937555344296"
print(paste('Accuracy Holt BoxCox Damped', accuracy(fit.holt.boxcox.damped)[2]))
## [1] "Accuracy Holt BoxCox Damped 26.5332106346358"
retaildata <- readxl::read_excel("retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349627V"],
frequency=12, start=c(1982,4))
autoplot(myts)
fit.hw <- hw(myts, h=120, seasonal = "multiplicative")
fit.hw.damped <- hw(myts, h=120, seasonal = "multiplicative", damped = TRUE)
autoplot(myts) +
autolayer(fit.hw, series="Holt's method", PI=FALSE) +
autolayer(fit.hw.damped, series="Damped Holt's method", PI=FALSE) +
ggtitle("Multiplicative seasonal forecast") + xlab("Year") +
ylab("MyTs")
fit.hw.errors <- tsCV(myts, hw, h=1, seasonal="multiplicative")
fit.hw.damped.errors <- tsCV(myts, hw, h = 1, seasonal = "multiplicative", damped = TRUE)
print(paste('HW RMSE = ', sqrt(mean(fit.hw.errors ^2, na.rm = TRUE))))
## [1] "HW RMSE = 6.53834715081406"
print(paste('HW Damped RMSE = ', sqrt(mean(fit.hw.damped.errors ^2, na.rm = TRUE))))
## [1] "HW Damped RMSE = 6.38516811589195"
checkresiduals(fit.hw.damped)
##
## Ljung-Box test
##
## data: Residuals from Damped Holt-Winters' multiplicative method
## Q* = 100.48, df = 7, p-value < 2.2e-16
##
## Model df: 17. Total lags used: 24
myts.train <- window(myts, end=c(2010,12))
myts.test <- window(myts, start=2011)
fit.hw.damped <- hw(myts.train, seasonal="multiplicative", damped=TRUE)
fit.seasonal.naive <- snaive(myts.train)
print(paste('RMSE for HW Damped Method =',accuracy(fit.hw.damped, myts.test)[2] , 'RMSE for seasonal naive method = ', accuracy(fit.seasonal.naive, myts.test)[2]))
## [1] "RMSE for HW Damped Method = 5.20815797873691 RMSE for seasonal naive method = 28.4"
l = BoxCox.lambda(myts.train)
fit.stl= stlf(myts.train, lambda = l)
fit.ets = ets(seasadj(decompose(myts.train, "multiplicative")))
autoplot(myts.train, series = "train") +
autolayer(forecast(fit.stl, h = 24, PI=F), series = "STL Forecast") +
autolayer(forecast(fit.ets, h = 24, PI=F), series = "ETS Forcast") +
autolayer(myts.test, series = "test")
print(paste('RMSE for STL Method =',accuracy(fit.stl, myts.test)[2] , 'RMSE for ets method = ', fit.ets$mse ^0.5 ))
## [1] "RMSE for STL Method = 0.909249211067817 RMSE for ets method = 5.26043688985084"