Consider the 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. b. Compute a 95% prediction interval for the first forecast using y ± 1.96s where s is the standard deviation of the residuals. Compare your interval with the interval produced by R.
## Time-Series [1:188] from 1980 to 1996: 76378 71947 33873 96428 105084 ...
## Jan Feb Mar Apr May Jun
## 1980 76378 71947 33873 96428 105084 95741
## 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
## 95%
## 119020.8
## 95%
## 78611.97
## [1] 118952.8
## [1] 78679.97
Data set books contains the daily sales of paperback and hardcover books at the same store. The task is to forecast the next four days’ sales for paperback and hardcover books. a. Plot the series and discuss the main features of the data. b. Use the ses() function to forecast each series, and plot the forecasts. c. Compute the RMSE values for the training data in each case.
## Time-Series [1:30, 1:2] from 1 to 30: 199 172 111 209 161 119 195 195 131 183 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:2] "Paperback" "Hardcover"
## Time Series:
## Start = 1
## End = 6
## Frequency = 1
## Paperback Hardcover
## 1 199 139
## 2 172 128
## 3 111 172
## 4 209 139
## 5 161 191
## 6 119 168
## [1] 1
# b)
library(fpp2)
ses_paper <- ses(books[, "Paperback"], h = 4)
ses_hard <- ses(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(ses_paper, series = "Paperback") +
ylab("Sales amount") +
ggtitle("Sales of Paperback books")
autoplot(books[, "Hardcover"], series = "Hardcover") +
autolayer(ses_hard, series = "Hardcover") +
ylab("Sales amount") +
ggtitle("Sales of Hardcover books")
## [1] 33.63769
## [1] 31.93101
We will continue with the daily sales of paperback and hardcover books in data set books. a. Apply Holt’s linear method to the paperback and hardback series and compute four-day forecasts in each case. 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. c. Compare the forecasts for the two series using both methods. Which do you think is best? 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.
library(fpp2)
# a)
holt_paper <- holt(books[, "Paperback"], h = 4)
holt_hard <- holt(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(holt_paper, series = "Paperback") +
ylab("Sales amount") +
ggtitle("Sales of Paperback books")
autoplot(books[, "Hardcover"], series = "Hardcover") +
autolayer(holt_hard, series = "Hardcover") +
ylab("Sales amount") +
ggtitle("Sales of Hardcover books")
## [1] 31.13692
## [1] 27.19358
## 95%
## 275.0205
## 95%
## 143.913
## 95%
## 275.3523
## 95%
## 138.867
## 95%
## 307.4256
## 95%
## 192.9222
## 95%
## 304.3403
## 95%
## 174.7799
For this exercise use data set eggs, the price of a dozen eggs in the United States from 1900–1993. Experiment with the various options in the holt() function to see how much the forecasts change with damped trend, or with a Box-Cox transformation. Try to develop an intuition of what each argument is doing to the forecasts. [Hint: use h=100 when calling holt() so you can clearly see the differences between the various options when plotting the forecasts.] Which model gives the best RMSE?
## Time-Series [1:94] from 1900 to 1993: 277 315 315 321 315 ...
## Time Series:
## Start = 1900
## End = 1905
## Frequency = 1
## [1] 276.79 315.42 314.87 321.25 314.54 317.92
# Simple Holt with no specific parameters
holt_eggs <- holt(eggs, h = 100)
autoplot(holt_eggs) +
autolayer(holt_eggs$fitted)
# Price forecast below zero, which is not possible
# Holt with Damped Trend
holt_damp_eggs <- holt(eggs, damped = TRUE, h = 100)
autoplot(holt_damp_eggs) +
autolayer(holt_damp_eggs$fitted)
# Price forecast does not go below zero but the down trend is not showing
# Holt with Box Cox Transformation
holt_BoxCox_eggs <- holt(eggs, lambda = BoxCox.lambda(eggs), h = 100)
autoplot(holt_BoxCox_eggs) +
autolayer(holt_BoxCox_eggs$fitted)
# Price forecast does not go below zero and the down trend is shown
# Holt with Damped Trend and Box Cox Transformation
holt_BoxCox_damp_eggs <- holt(eggs, damped = TRUE, lambda = BoxCox.lambda(eggs), h = 100)
autoplot(holt_BoxCox_damp_eggs) +
autolayer(holt_BoxCox_damp_eggs$fitted)
# Price forecast does not go below zero but the trend while decreasing, seems to be flattening aroudn value 50
# RMSE calculations
sqrt(mean(holt_eggs$residuals^2))
## [1] 26.58219
## [1] 26.54019
## [1] 1.032217
## [1] 1.039187
Recall your retail time series data (from Exercise 3 in Section 2.10). a. Why is multiplicative seasonality necessary for this series? b. Apply Holt-Winters’ multiplicative method to the data. Experiment with making the trend damped. c. Compare the RMSE of the one-step forecasts from the two methods. Which do you prefer? d. Check that the residuals from the best method look like white noise. 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?
retaildata <- readxl::read_excel("retail.xlsx", skip=1)
retail_ts <- ts(retaildata[,"A3349873A"], frequency=12, start=c(1982,4))
autoplot(retail_ts)
# b)
hw_retail <- hw(retail_ts, seasonal = "multiplicative")
hw_damp_retail <- hw(retail_ts, seasonal = "multiplicative", damped = TRUE)
autoplot(hw_retail)
# c)
error_hw_retail <- tsCV(retail_ts, hw, h = 1, seasonal = "multiplicative")
error_hw_damp_retail <- tsCV(retail_ts, hw, h = 1, seasonal = "multiplicative", damped = TRUE)
sqrt(mean(error_hw_retail^2, na.rm = TRUE))
## [1] 14.72762
## [1] 14.94306
##
## Ljung-Box test
##
## data: Residuals from Holt-Winters' multiplicative method
## Q* = 40.405, df = 8, p-value = 2.692e-06
##
## Model df: 16. Total lags used: 24
#ggAcf(hw_damp_retail)
# e)
retail_ts_train <- window(retail_ts, end = c(2010, 12))
retail_ts_test <- window(retail_ts, start = 2011)
hw_retail_train <- hw(retail_ts_train, h = 36, seasonal = "multiplicative", damped = FALSE)
autoplot(hw_retail_train)
## ME RMSE MAE MPE MAPE
## Training set 0.03021223 9.107356 6.553533 0.001995484 3.293399
## Test set 78.34068365 94.806617 78.340684 19.945024968 19.945025
## MASE ACF1 Theil's U
## Training set 0.4107058 0.02752875 NA
## Test set 4.9095618 0.52802701 1.613903
For the same retail data, try an STL decomposition applied to the Box-Cox transformed series, followed by ETS on the seasonally adjusted data. How does that compare with your best previous forecasts on the test set?
retail_ts_train <- window(retail_ts, end = c(2010, 12))
retail_ts_test <- window(retail_ts, start = 2011)
fc_stl_boxcox_retail <- retail_ts_train %>%
stlm(
s.window = 13,
robust = TRUE,
method = "ets",
lambda = BoxCox.lambda(retail_ts_train)
) %>%
forecast(h = 36, lambda = BoxCox.lambda(retail_ts_train))
## Warning in InvBoxCox(fcast$mean, lambda, biasadj, fcast): biasadj
## information not found, defaulting to FALSE.
## ME RMSE MAE MPE MAPE MASE
## Training set -0.6782982 8.583559 5.918078 -0.3254076 2.913104 0.3708823
## Test set 82.1015276 98.384220 82.101528 21.0189982 21.018998 5.1452516
## ACF1 Theil's U
## Training set 0.02704667 NA
## Test set 0.52161725 1.679783