DATA 624 Homework 5 Chapter 7 Forecasting
DATA 624 Homework 5 Chapter 7 Forecasting
- Question 7.1
- Consider the pigs series — the number of pigs slaughtered in Victoria each month.
- 7.1 a) Use the ses() function in R to find the optimal values of α and ι0, and generate forecasts for the next four months.
- Simple Expontential Smoothing Plot
- 7.1 b) Compute a 95% prediction interval for the first forecast using y^± 1.96 s where s is the standard deviation of the residuals.
- Question 7.5
- Question 7.6
- 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.
- Question 7.7
- Question 7.8
- 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?
- Question 7.9
Question 7.1
Consider the pigs series — the number of pigs slaughtered in Victoria each month.
7.1 a) Use the ses() function in R to find the optimal values of α and ι0, and generate forecasts for the next four months.
The ses function generates and α of 0.2971488 and a ι0 of 77260.0561459.
##
## 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
Simple Expontential Smoothing Plot
7.1 b) Compute a 95% prediction interval for the first forecast using y^± 1.96 s where s is the standard deviation of the residuals.
## Lower Upper
## 78679.97 118952.84
Question 7.5
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.
###Plot the series and discuss the main features of the data.
The sales of paperback and hardcover books generally increased as time went on with lots of fluctuations. But the fluctuations doesn’t show particular frequency that they can be thought of as cycle This is also only thirty days of data so it is not really possible to speak to any seasonaltiy components.
Question 7.5 b) Use the ses() function to forecast each series, and plot the forecasts.
Question 7.5c) Compute the RMSE values for the training data in each case.
The RMSE for the paperback model is 33.64 and 31.93 for the hardback model.
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 7.175981 33.63769 27.8431 0.4736071 15.57784 0.7021303 -0.2117522
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 9.166735 31.93101 26.77319 2.636189 13.39487 0.7987887 -0.1417763
Question 7.6
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.
paperback <- books[,1]
hardcover <- books[,2]
holt_paperback <- holt(paperback, h = 4)
summary(holt_paperback)
##
## Forecast method: Holt's method
##
## Model Information:
## Holt's method
##
## Call:
## holt(y = paperback, h = 4)
##
## Smoothing parameters:
## alpha = 1e-04
## beta = 1e-04
##
## Initial states:
## l = 170.699
## b = 1.2621
##
## sigma: 33.4464
##
## AIC AICc BIC
## 318.3396 320.8396 325.3456
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -3.717178 31.13692 26.18083 -5.508526 15.58354 0.6602122
## ACF1
## Training set -0.1750792
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 31 209.4668 166.6035 252.3301 143.9130 275.0205
## 32 210.7177 167.8544 253.5811 145.1640 276.2715
## 33 211.9687 169.1054 254.8320 146.4149 277.5225
## 34 213.2197 170.3564 256.0830 147.6659 278.7735
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.
Holt’s method does a better job than the SES models, because the RMSE is smaller for both the paperback and hardback series. This is understandable because Holt’s includes a trend component. SES assumes there is no trend. This does not really fit these timeseries.
## [1] "Paperback RMSE: 31.1369230162347"
## [1] "Hardcover RMSE: 27.1935779818511"
c. Compare the forecasts for the two series using both methods. Which do you think is best?
Holt’s method better captures the trend in the sales time series.
grid.arrange(
autoplot(ses(paperback)) + autolayer(fitted(ses(paperback)), series = "SES") + autolayer(fitted(holt(paperback)), series = "HOLT") + labs(title = "Paperback"),
autoplot(ses(hardcover)) + autolayer(fitted(ses(hardcover)), series = "SES") + autolayer(fitted(holt(hardcover)), series = "HOLT") + labs(title = "Hardcover")
)
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.
## Paperback: 148.4384 to 270.4951
Question 7.7
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?
The Box-Cox model gives the best RMSE, and it accounts for the downward trend in the time series.
holt_orig <- holt(eggs, h = 100)
holt_bc <- holt(eggs, lambda = BoxCox.lambda(eggs), h = 100)
holt_damp <- holt(eggs, damped = TRUE, h = 100)
holt_damp_bc <- holt(eggs, damped = TRUE, lambda = BoxCox.lambda(eggs), h = 100)
grid.arrange(
autoplot(holt_orig) + labs(title = paste("Original", ", RMSE =", round(RMSE(holt_orig$x, holt_orig$fitted),3))),
autoplot(holt_bc) + labs(title = paste("Box-Cox", ", RMSE =", round(RMSE(holt_bc$x, holt_bc$fitted),3))),
autoplot(holt_damp) + labs(title = paste("Dampened", ", RMSE =", round(RMSE(holt_damp$x, holt_damp$fitted),3))),
autoplot(holt_damp_bc) + labs(title = paste("Dampened, Box-Cox", ", RMSE =", round(RMSE(holt_damp_bc$x, holt_damp_bc$fitted),3)))
)
Question 7.8
Recall your retail time series data (from Exercise 3 in Section 2.10).
a. Why is multiplicative seasonality necessary for this series?
Multiplicative seasonality is necessary for this series because the seasonality variability increases as time passes.
library(readxl)
retaildata <- read_excel("retail.xlsx", skip = 1)
retail_ts <- ts(retaildata[, "A3349873A"], frequency = 12, start = c(1982, 4))
autoplot(retail_ts) + ylab("Retail Sales")
b.Apply Holt-Winters’ multiplicative method to the data. Experiment with making the trend damped.
We can see that damped methods generated less agressive forecast compared to un-damped method
fit.hw <- hw(retail_ts, h=120, seasonal = "multiplicative")
fit.hw.damped <- hw(retail_ts, h=120, seasonal = "multiplicative", damped = TRUE)
autoplot(retail_ts) +
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")
c. Compare the RMSE of the one-step forecasts from the two methods. Which do you prefer?
*Damped method has lower RMSE so we will prefer damped method with seasonal multiplication I use the time series cross validation function**
fit.hw.errors <- tsCV(retail_ts, hw, h=1, seasonal="multiplicative")
fit.hw.damped.errors <- tsCV(retail_ts, hw, h = 1, seasonal = "multiplicative", damped = TRUE)
print(paste('HW RMSE = ', sqrt(mean(fit.hw.errors ^2, na.rm = TRUE))))
## [1] "HW RMSE = 14.7276169362671"
## [1] "HW Damped RMSE = 14.9430568939693"
d. Check that the residuals from the best method look like white noise.
##
## Ljung-Box test
##
## data: Residuals from Damped Holt-Winters' multiplicative method
## Q* = 42.932, df = 7, p-value = 3.437e-07
##
## Model df: 17. 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 damped holt’s method performed better than the naive bayes
retail_ts.train <- window(retail_ts, end=c(2010,12))
retail_ts.test <- window(retail_ts, start=2011)
fit.hw.damped <- hw(retail_ts.train, seasonal="multiplicative", damped=TRUE)
fit.seasonal.naive <- snaive(retail_ts.train)
print(paste('RMSE for HW Damped Method =',accuracy(fit.hw.damped, retail_ts.test)[2] , 'RMSE for seasonal naive method = ', accuracy(fit.seasonal.naive, retail_ts.test)[2]))
## [1] "RMSE for HW Damped Method = 67.4739545359792 RMSE for seasonal naive method = 55.3"
Question 7.9
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)
l = BoxCox.lambda(retail_ts.train)
fit.stl= stlf(retail_ts.train, lambda = l)
fit.ets = ets(seasadj(decompose(retail_ts.train, "multiplicative")))
autoplot(retail_ts.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(retail_ts.test, series = "test")
Compare the RMSE
## [1] "RMSE for STL Method = 56.4621438297631 RMSE for ets method = 8.5512675661209"
STL method outperforms ETS method forecast on seasonally adjusted time series. The previous method (HW with damped) has RMSE = 5.20, STL method outperforms HW with damped model also