if (!require("fpp2")) install.packages("fpp2")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("gridExtra")) install.packages("gridExtra")
library(fma)
library(forecast)ses() function in R to find the optimal values of \(\alpha\) and \(\ell_0\), and generate forecasts for the next four months.## 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
## 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
s <- sd(fc$residuals)
mean_fc <- fc$mean[1]
print(paste0("Lower Confidence Interval: ", round(mean_fc - (1.96*s), 2)))## [1] "Lower Confidence Interval: 78679.97"
## [1] "Upper Confidence Interval: 118952.84"
## Time Series:
## Start = 1
## End = 30
## Frequency = 1
## Paperback Hardcover
## 1 199 139
## 2 172 128
## 3 111 172
## 4 209 139
## 5 161 191
## 6 119 168
## 7 195 170
## 8 195 145
## 9 131 184
## 10 183 135
## 11 143 218
## 12 141 198
## 13 168 230
## 14 201 222
## 15 155 206
## 16 243 240
## 17 225 189
## 18 167 222
## 19 237 158
## 20 202 178
## 21 186 217
## 22 176 261
## 23 232 238
## 24 195 240
## 25 190 214
## 26 182 200
## 27 222 201
## 28 217 283
## 29 188 220
## 30 247 259
fc_paperback <- ses(books[,1], h=4)
fc_hardcover <- ses(books[,2], h=4)
#forecast paperback
forecast(fc_paperback)## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 31 207.1097 162.4882 251.7311 138.8670 275.3523
## 32 207.1097 161.8589 252.3604 137.9046 276.3147
## 33 207.1097 161.2382 252.9811 136.9554 277.2639
## 34 207.1097 160.6259 253.5935 136.0188 278.2005
#plot paperback
a<- autoplot(fc_paperback)
#plot forecast
b<- autoplot(fc_hardcover)
grid.arrange(a, b, nrow = 2)## [1] "RMSE values Paperback: 33.64"
## [1] "RMSE values Hardcover: 31.93"
# holt method
holt_Paperback <- holt(books[, "Paperback"], h = 4)
holt_Hardcover <- holt(books[, "Hardcover"], h = 4)
#plot
a<- autoplot(holt_Paperback)
b<- autoplot(holt_Hardcover)
grid.arrange(a, b, nrow = 2)## [1] "Holt’s method, RMSE values Paperback: 31.14"
## [1] "Holt’s method, RMSE values Hardcover: 27.19"
Holt’s method is using one more parameter than SES which improves the RMSE value
Comparing both models, holt is the best model in terms of RMSE values.
PAPERBACK
#PAPERBACK
s <- sd(fc_paperback$residuals)
mean_fc <- fc_paperback$mean[1]
print(paste0("SES, Lower Confidence Interval: ", round(mean_fc - (1.96*s), 2)))## [1] "SES, Lower Confidence Interval: 141.6"
## [1] "SES, Upper Confidence Interval: 272.62"
#from model
print(paste0("SES, Lower Confidence Interval from formula: ", round(forecast(fc_paperback)$lower[1, "95%"],2) ))## [1] "SES, Lower Confidence Interval from formula: 138.87"
## [1] "SES, Upper Confidence Interval from formula: 275.35"
s <- sd(holt_Paperback$residuals)
mean_fc <- holt_Paperback$mean[1]
print(paste0("Holt, Lower Confidence Interval: ", round(mean_fc - (1.96*s), 2)))## [1] "Holt, Lower Confidence Interval: 147.84"
## [1] "Holt, Upper Confidence Interval: 271.09"
#from model
print(paste0("Holt, Lower Confidence Interval from formula: ", round(forecast(holt_Paperback)$lower[1, "95%"],2) ))## [1] "Holt, Lower Confidence Interval from formula: 143.91"
## [1] "Holt, Upper Confidence Interval from formula: 275.02"
Lower and Upper intervals in paperback for both the time series using ses and holt methods are comparitively similar.
HARDCOVER
s <- sd(fc_hardcover$residuals)
mean_fc <- fc_hardcover$mean[1]
print(paste0("SES, Lower Confidence Interval: ", round(mean_fc - (1.96*s), 2)))## [1] "SES, Lower Confidence Interval: 178.58"
## [1] "SES, Upper Confidence Interval: 300.54"
#from model
print(paste0("SES, Lower Confidence Interval from formula: ", round(forecast(fc_hardcover)$lower[1, "95%"],2) ))## [1] "SES, Lower Confidence Interval from formula: 174.78"
## [1] "SES, Upper Confidence Interval from formula: 304.34"
s <- sd(holt_Hardcover$residuals)
mean_fc <- holt_Hardcover$mean[1]
print(paste0("Holt, Lower Confidence Interval: ", round(mean_fc - (1.96*s), 2)))## [1] "Holt, Lower Confidence Interval: 195.96"
## [1] "Holt, Upper Confidence Interval: 304.38"
#from model
print(paste0("Holt, Lower Confidence Interval from formula: ", round(forecast(holt_Hardcover)$lower[1, "95%"],2) ))## [1] "Holt, Lower Confidence Interval from formula: 192.92"
## [1] "Holt, Upper Confidence Interval from formula: 307.43"
Upper Confidence interval in Hardcover seems similar in both method SES and Holt. However, there is differece in lower confidence interval.
[Hint: use h=100 when calling holt() so you can clearly see the differences between the various options when plotting the forecasts.]
fc1 <- holt(eggs, h=100)
fc2 <- holt(eggs, damped=TRUE, h=100)
fc3 <- holt(eggs, lambda="auto", h=100)
fc4 <- holt(eggs, damped=TRUE, lambda="auto", h=100)
a<- autoplot(fc1)
b<- autoplot(fc2)
c<- autoplot(fc3)
d<- autoplot(fc4)
grid.arrange(a,b,c,d, nrow = 2)Which model gives the best RMSE?
## [1] "RMSE - Holt : 26.58"
## [1] "RMSE - Holt damped : 26.54"
## [1] "RMSE - Holt box-cox : 26.39"
## [1] "RMSE - Holt damped box-cox : 26.53"
Comparing the accuracy of all four method reveals that the RMSE was almost similar, but the best method when compared to other methods is Holt’s Method with Box-Cox transformation since RMSE is lowest
library(readxl)
library(seasonal)
retaildata <- readxl::read_excel("C:/Users/patel/Documents/Data_624/retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349335T"],
frequency=12, start=c(1982,4))
autoplot(myts)It is clear from the graph that seasonality variations are changing with increase in time. In that case, multiplicative seasonality is necessary.
fc_myts <- hw(myts, seasonal="multiplicative", h=100)
fc_myts_d <- hw(myts, damped=TRUE, seasonal="multiplicative", h=100)
a<- autoplot(fc_myts)
b<- autoplot(fc_myts_d)
grid.arrange(a,b, ncol = 2)## [1] "RMSE - Holt : 25.2"
## [1] "RMSE - Holt damped : 25.1"
Since RMSE is 0.1 lower for damped method campare to Holt method, it is the best method.
##
## Ljung-Box test
##
## data: Residuals from Damped Holt-Winters' multiplicative method
## Q* = 285.62, df = 7, p-value < 2.2e-16
##
## Model df: 17. Total lags used: 24
It doesnot seem any white Noise
myts_train <- window(myts, end = c(2010, 12))
myts_test <- window(myts, start = 2011)
myts_train_d <- hw(myts_train, damped = TRUE, seasonal = "multiplicative")
print(paste0("1. RMSE - Holt damped : " ))## [1] "1. RMSE - Holt damped : "
## Training set Test set
## 25.68057 41.08034
## [1] "2. RMSE - naïve approach : "
## Training set Test set
## 72.20702 145.46662
RMSE in Holt damped method is lower compare to snaive method. Hence, the Holt-Winter’s Multiplicative Damped method outperformed seasonal naive forecast.