pigs series — the number of pigs slaughtered in Victoria each month.(a) Use the ses() function in R to find the optimal values of α and ℓ0, and generate forecasts for the next four months.
The optimal α and ℓ0 are 0.2971 and 77260.05, respectively. The four month forecast is reflected in the plot below.
##
## Forecast method: Simple exponential smoothing
##
## Model Information:
## 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
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 385.8721 10253.6 7961.383 -0.922652 9.274016 0.7966249 0.01282239
##
## Forecasts:
## 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
(b)Compute a 95% prediction interval for the first forecast using ^y±1.96 where s is the standard deviation of the residuals. Compare your interval with the interval produced by R.
The two confidence intervals are (78679.97 - 118952.84) and (78611.97 - 119020.84). The second, R produced, interval is wider because the R factors in degrees of freedom when calculating variance.
sd <- sd(residuals(spigs))
con95 <- c(Lower = spigs$mean[1] - 1.96*sd, Upper = spigs$mean[1] + 1.96*sd)
con95## Lower Upper
## 78679.97 118952.84
## Lower Upper
## 78611.97 119020.84
(a) Plot the series and discuss the main features of the data.
The paperback and hardcover books have show a positive trend for the month (more sales at end of month). With only 30-days of data is difficult call out any weekly seasonality. It appears more hardcover books are sold during the period. Hardcover books also appear to have greater volatility.
autoplot(books) + theme_fivethirtyeight() +
labs(title = "Daily Same Store Sales", subtitle = 'Paperback vs Hardcover') (b) Use the ses() function to forecast each series, and plot the forecasts.
pb <- ses(books[, "Paperback"], h = 4)
hc <- ses(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(pb, series = "Paperback") +
autolayer(books[, "Hardcover"], series = "Hardcover") +
autolayer(hc, series = "Hardcover", PI = FALSE) + theme_fivethirtyeight() +
labs(title = "Paperback & Hardcover Book Daily Same Store Sales", subtitle = 'Actual and Forecast')(c) Compute the RMSE values for the training data in each case.
The RMSE for paperback books is 33.6376868, that compares to 31.931015 for hardcover books. Despite appearing to be more volatile, hardcover books had a lower RMSE.
(a) Apply Holt’s linear method to the paperback and hardback series and compute four-day forecasts in each case.
pb_h <- holt(books[, "Paperback"], h = 4)
hc_h <- holt(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(pb_h, series = "Paperback") +
autolayer(books[, "Hardcover"], series = "Hardcover") +
autolayer(hc_h, series = "Hardcover", PI = FALSE) + theme_fivethirtyeight() +
labs(title = "PB & HC Book Daily Same Store Sales", subtitle = 'Holt Linear Method')(b) Compare the RMSE measures of Holt’s method for the two series to those of simple exponential smoothing in the previous question. (Remember that Holt’s method is using one more parameter than SES.) Discuss the merits of the two forecasting methods for these data sets.
| SES Method | Holt Method | |
|---|---|---|
| Paperback | 33.6376868 | 31.136923 |
| Hardcover | 31.931015 | 27.193578 |
The Holt method yield lover RMSE compared to the SES method.
## [1] 31.13692
## [1] 27.19358
(c) Compare the forecasts for the two series using both methods. Which do you think is best?
The hardcover series yield lower RMSE under both SEs and Holt methodologies. It appears to be the better forecast from that perspective and also passes the eye test when comparing the autoplot of the series. That said, we are not dealing with a lot of data, so its difficult to give a definitive answer.
(d) Calculate a 95% prediction interval for the first forecast for each series, using the RMSE values and assuming normal errors. Compare your intervals with those produced using ses and holt.
| Lower Limit | Upper Limit | |
|---|---|---|
| 95% PI PB Holt fn | 143.9129858 | 275.0205452 |
| 95% PI PB formula | 148.4383964 | 270.4951346 |
| 95% PI HC Holt fn | 192.9221708 | 307.4255737 |
| 95% PI HC formula | 196.8744594 | 303.4732851 |
Enter commentary here!
holt_eggs <- holt(eggs, h = 100)
autoplot(holt_eggs) + autolayer(holt_eggs$fitted) +
theme_fivethirtyeight()holt_damped_eggs <- holt(eggs, damped = TRUE, h = 100)
autoplot(holt_damped_eggs) + autolayer(holt_damped_eggs$fitted) +
theme_fivethirtyeight()holt_BoxCox_eggs <- holt(eggs, lambda = BoxCox.lambda(eggs), h = 100)
autoplot(holt_BoxCox_eggs) + autolayer(holt_BoxCox_eggs$fitted) +
theme_fivethirtyeight()holt_BoxCox_damped_eggs <- holt(eggs, damped = TRUE, lambda = BoxCox.lambda(eggs), h = 100)
autoplot(holt_BoxCox_damped_eggs) + autolayer(holt_BoxCox_damped_eggs$fitted) +
theme_fivethirtyeight()a<- accuracy(holt_eggs)[2]
b<- accuracy(holt_damped_eggs)[2]
c<- accuracy(holt_BoxCox_eggs)[2]
d<- accuracy(holt_BoxCox_damped_eggs)[2]The table below indicates that the Box Cox Model yielded the best RMSE (26.393)
| Model | RMSE |
|---|---|
| Holt | 26.5821882 |
| ———————- | ——– |
| Holt Dampened | 26.5401853 |
| ———————- | ——– |
| Holt Box Cox | 26.3937555 |
| ———————- | ——– |
| HOlt Box Cos Dampened | 26.5332106 |
| ———————- | ——– |
(a) Why is multiplicative seasonality necessary for this series?
retaildata <- readxl::read_excel("retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349335T"], frequency=12, start=c(1982,4))
myCaption <- BoxCox.lambda(myts)
myts %>%
as_tsibble() %>%
mutate(BoxCox = BoxCox.lambda(value)) %>%
autoplot() + labs(title = "New South Wales Supermarket and grocery store Sales", subtitle = str_c('Transformation parameter lambda: ', myCaption), caption= str_c('Data Source: A3349335T')) +
theme_fivethirtyeight() + theme(plot.subtitle = element_text(color = "#008FD5")) (b) Apply Holt-Winters’ multiplicative method to the data. Experiment with making the trend damped.
HoltWin <- hw(myts, h=120, seasonal = "multiplicative")
HoltWinDamp <- hw(myts, h=120, seasonal = "multiplicative", damped = TRUE)
autoplot(myts) +
autolayer(HoltWin, series="Method: Holt-Winters", PI=FALSE) +
autolayer(HoltWinDamp, series="Method: Holt-Winters Dampend", PI=FALSE) +theme_fivethirtyeight() +
ggtitle("Multiplicative seasonal forecast") + xlab("Year") +
ylab("MyTs")(c) Compare the RMSE of the one-step forecasts from the two methods. Which do you prefer?
The RMSE of the Holt method with no dampening was 25.203811 compared to 25.1005865 for the Holt with Dampening method. The undampened approach yield the superior results.
(d) Check that the residuals from the best method look like white noise.
Owing to a p-value from the Ljung-Box test that approximates zero it seems there may be some autocorrelation in the model. This is consistent with residuals plot that appears to have decreasing variablility over time.
##
## Ljung-Box test
##
## data: Residuals from Holt-Winters' multiplicative method
## Q* = 250.64, df = 8, p-value < 2.2e-16
##
## Model df: 16. Total lags used: 24
(e) Now find the test set RMSE, while training the model to the end of 2010. Can you beat the seasonal naïve approach from Exercise 8 in Section 3.7?__
The RMSEs for the Holt winters was far superior to the Naive model (25/65 vs 72/109), the larege differences between training and test RMSE make speak to other challenges.
myts.train <- window(myts, end = c(2010, 12))
myts.test <- window(myts, start=2011)
Holtrain <- hw(myts.train, seasonal = "multiplicative")
HoltrainAcc <- accuracy(Holtrain, x = myts)
HoltrainAcc## ME RMSE MAE MPE MAPE MASE
## Training set 2.658058 25.00649 18.25149 0.2228273 1.965971 0.2958851
## Test set -49.514639 65.26190 52.87938 -2.2820578 2.442746 0.8572571
## ACF1 Theil's U
## Training set -0.006737509 NA
## Test set 0.438392290 0.5339184
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 61.56787 72.20702 61.68438 6.388722 6.404105 1.000000 0.6018274
## Test set 97.44583 109.62545 100.02917 4.629852 4.751209 1.621629 0.2686595
## Theil's U
## Training set NA
## Test set 0.9036205
The STL methodology outperformed the ETS as is evident from the plot below. The STL with decomposition looks to be the strongest performer.
STLDec <- stlf(myts.train, lambda = BoxCox.lambda(myts.train))
STLDecAcc <- accuracy(STLDec, myts.test)[2]
STLDecAcc ## [1] -85.57854
autoplot(myts.train, series = "train") +
autolayer(forecast(STLDec, h = 24, PI=FALSE), series = "STL Fcst") +
autolayer(forecast(ETS, h = 24, PI=FALSE), series = "ETS Fcst") +
autolayer(myts.test, series = "test") + theme_fivethirtyeight()