Below are the solutions to these exercises on forecasting using linear models.

#################### # # # Exercise 1 # # # #################### df <- read.csv("ecommerce.csv") series <- ts(df, frequency = 4, start = c(1999, 4)) plot(series)

#################### # # # Exercise 2 # # # #################### require(forecast) fcast_naive <- naive(series, h = 8) #################### # # # Exercise 3 # # # #################### require(forecast) plot(fcast_naive)

#################### # # # Exercise 4 # # # #################### require(forecast) model_linear <- tslm(series ~ trend + season) fcast_linear <- forecast(model_linear, h = 8) plot(fcast_linear)

#################### # # # Exercise 5 # # # #################### fcast_linear_residuals <- fcast_linear$residuals #################### # # # Exercise 6 # # # #################### require(forecast) model_arima <- auto.arima(series) fcast_arima <- forecast(model_arima, h = 8) plot(fcast_arima)

#################### # # # Exercise 7 # # # #################### require(forecast) fcast_arima_summary <- summary(fcast_arima)

## ## Forecast method: ARIMA(0,1,0)(2,1,0)[4] ## ## Model Information: ## Series: series ## ARIMA(0,1,0)(2,1,0)[4] ## ## Coefficients: ## sar1 sar2 ## 0.2700 0.3421 ## s.e. 0.1137 0.1175 ## ## sigma^2 estimated as 2092622: log likelihood=-556.39 ## AIC=1118.78 AICc=1119.18 BIC=1125.25 ## ## Error measures: ## ME RMSE MAE MPE MAPE MASE ## Training set 97.00685 1371.25 921.3438 0.07940248 2.968625 0.1564942 ## ACF1 ## Training set 0.01029391 ## ## Forecasts: ## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 ## 2017 Q1 100381.6 98527.7 102235.5 97546.32 103216.8 ## 2017 Q2 105849.6 103227.8 108471.4 101839.93 109859.3 ## 2017 Q3 108442.8 105231.8 111653.8 103531.96 113353.6 ## 2017 Q4 140288.8 136581.0 143996.5 134618.24 145959.3 ## 2018 Q1 115787.3 110178.6 121396.0 107209.51 124365.1 ## 2018 Q2 121787.1 114775.1 128799.1 111063.21 132511.0 ## 2018 Q3 124482.7 116304.8 132660.6 111975.70 136989.7 ## 2018 Q4 157800.4 148603.2 166997.6 143734.55 171866.3

#################### # # # Exercise 8 # # # #################### fcast_arima_summary[["Point Forecast"]][8] # forecast value

## [1] 157800.4

fcast_arima_summary[["Lo 95"]][8] # lower band of the confidence interval

## [1] 143734.6

fcast_arima_summary[["Hi 95"]][8] # upper band of the confidence interval

## [1] 171866.3

#################### # # # Exercise 9 # # # #################### fcast_arima_residuals <- fcast_arima$residuals #################### # # # Exercise 10 # # # #################### require(forecast) dm.test(fcast_linear_residuals, fcast_arima_residuals, alternative = "greater")

## ## Diebold-Mariano Test ## ## data: fcast_linear_residualsfcast_arima_residuals ## DM = 3.9013, Forecast horizon = 1, Loss function power = 2, ## p-value = 0.000111 ## alternative hypothesis: greater

# p-value = 0.000111. It implies that the null hypothesis that the two forecasts # are equal can be rejected at conventional confidence levels in favour of # the alternative hypothesis, which states that the second forecast # (based on the ARIMA model) is more accurate that the first forecast # (based on a linear regression).

Andy says

Are we sure that the answer to exercise 10 is absolutely correct? I think that the “dm.test” function should be used with the parameter and value “h=8” as this is the forecast horizon in calculating the two models. In the solution, we are using a forecast horizon of 1.