Below are the solutions to these exercises on forecasting with exponential smoothing.
#################### # # # Exercise 1 # # # #################### df <- read.csv("unemployment.csv") unempl <- ts(df, start = c(2012, 1), frequency = 12) plot(unempl)

#################### # # # Exercise 2 # # # #################### require(forecast) fcast_ses <- ses(unempl, h = 12) plot(fcast_ses)

#################### # # # Exercise 3 # # # #################### require(forecast) fit_ets_default <- ets(unempl) fcast_ets_default <- forecast(fit_ets_default, h = 12) plot(fcast_ets_default)

#################### # # # Exercise 4 # # # #################### require(forecast) summary(fit_ets_default)
## ETS(M,A,M) ## ## Call: ## ets(y = unempl) ## ## Smoothing parameters: ## alpha = 0.5717 ## beta = 1e-04 ## gamma = 1e-04 ## ## Initial states: ## l = 8.4353 ## b = -0.0564 ## s=0.9567 0.9453 0.957 0.9669 1.0245 1.0591 ## 1.0365 0.9642 0.9403 1.0224 1.0542 1.0729 ## ## sigma: 0.021 ## ## AIC AICc BIC ## 37.38299 50.98299 73.81628 ## ## Training set error measures: ## ME RMSE MAE MPE MAPE MASE ## Training set -0.009791689 0.1271141 0.102154 -0.1209349 1.687472 0.1322298 ## ACF1 ## Training set 0.08649403
# The first line in the printed summary is "ETS(M,A,M)" # It implies that (1) the model has error, trend, and seasonal components, # (2) error and seasonal components are multiplicative, # trend component is additive. #################### # # # Exercise 5 # # # #################### require(forecast) fit_ets_damped_trend <- ets(unempl, damped = TRUE) fcast_ets_damped_trend <- forecast(fit_ets_damped_trend, h = 12) plot(fcast_ets_damped_trend)

#################### # # # Exercise 6 # # # #################### require(forecast) fit_ets_no_trend <- ets(unempl, model = "ZNZ") fcast_ets_no_trend <- forecast(fit_ets_no_trend, h = 12) plot(fcast_ets_no_trend)

#################### # # # Exercise 7 # # # #################### require(forecast) fit_bats <- bats(unempl, use.damped.trend = TRUE) fcast_bats <- forecast(fit_bats, h = 12) plot(fcast_bats)

#################### # # # Exercise 8 # # # #################### require(forecast) acc_bats <- accuracy(fcast_bats) print(acc_bats)
## ME RMSE MAE MPE MAPE MASE ## Training set -0.02284822 0.1503005 0.1073459 -0.2853875 1.707524 0.1389502 ## ACF1 ## Training set 0.1717618
mae_bats <- acc_bats[1, "MAE"] #################### # # # Exercise 9 # # # #################### require(forecast) # the function that selects the forecast with the smallest mean absolute error best_forecast <- function(series, forecasting_functions) { smallest_mae <- Inf best_forecast <- NULL for (f in forecasting_functions) { fit <- f(series) fcast <- forecast(fit, h = 12) mae <- accuracy(fcast)[1, "MAE"] if (mae < smallest_mae) { smallest_mae <- mae best_forecast <- fcast } } return(best_forecast) } # run the function with the unpemployment time series and a list of forecasting models forecasting_functions <- c(ets, bats, auto.arima) fcast_best <- best_forecast(unempl, forecasting_functions) plot(fcast_best)

#################### # # # Exercise 10 # # # #################### # the same as in the previous exercise except of the commented line best_forecast <- function(series, forecasting_functions) { smallest_mae <- Inf best_forecast <- NULL for (f in forecasting_functions) { fit <- f(series) fcast <- forecast(fit, h = 12) mae <- accuracy(fcast)[1, "MAE"] cat(sprintf("%-35s %f\n", fcast$method, mae)) # a line that prints mae's if (mae < smallest_mae) { smallest_mae <- mae best_forecast <- fcast } } return(best_forecast) } forecasting_functions <- c(ets, bats, auto.arima) fcast_best <- best_forecast(unempl, forecasting_functions)
## ETS(M,A,M) 0.102154 ## BATS(0.005, {0,0}, 1, {12}) 0.108308 ## ARIMA(2,1,2)(1,0,0)[12] with drift 0.144354
Leave a Reply