comb OLS AirPassengers

1 - Comb OLS

library(forecast)
library(ForecastComb)
custom_error <- function(actual, forecast_) {
  return(mean(abs(forecast_ - actual)))
}
h <- 29L

train_AirPassengers <- ts(AirPassengers[1:115], start = start(AirPassengers), frequency = frequency(AirPassengers))

arima_forecast <- forecast::forecast(forecast::auto.arima(train_AirPassengers), h=h)
ets_forecast <- forecast::forecast(forecast::ets(train_AirPassengers), h=h)
theta_forecast <- forecast::thetaf(train_AirPassengers, h=h)
ridge_forecast <- ahead::dynrmf(train_AirPassengers, h=h)
glmnet_forecast <- ahead::dynrmf(train_AirPassengers, h=h, 
fit_func = glmnet::cv.glmnet, predict_func = predict)
print(forecast::accuracy(arima_forecast, AirPassengers[116:144]))
##                      ME      RMSE       MAE        MPE     MAPE      MASE
## Training set  0.2145268  9.067565  6.898655 0.03414791 2.899017 0.3275496
## Test set     22.9591869 35.082793 28.547389 4.53360086 6.124545 1.3554362
##                    ACF1
## Training set 0.01529513
## Test set             NA
print(forecast::accuracy(ets_forecast, AirPassengers[116:144]))
##                     ME      RMSE       MAE       MPE     MAPE      MASE
## Training set  1.375376  8.141851  6.348423 0.4340769 2.755794 0.3014245
## Test set     15.907062 35.807216 29.976427 2.6967550 6.533672 1.4232873
##                   ACF1
## Training set 0.1456818
## Test set            NA
print(forecast::accuracy(theta_forecast, AirPassengers[116:144]))
##                     ME      RMSE       MAE       MPE     MAPE      MASE
## Training set  2.509724  8.244161  6.381718 0.9709265 2.794444 0.3030054
## Test set     18.499435 38.339636 32.160094 3.2578724 6.995654 1.5269682
##                     ACF1
## Training set -0.06759516
## Test set              NA
print(forecast::accuracy(ridge_forecast, AirPassengers[116:144]))
##                         ME     RMSE      MAE         MPE     MAPE      MASE
## Training set -5.507958e-16 13.03215 10.56922 -0.05818072 5.014722 0.5447499
## Test set     -1.868676e+01 41.59669 32.81392 -5.01410649 7.788813 1.5580118
##                  ACF1
## Training set 0.581781
## Test set           NA
print(forecast::accuracy(glmnet_forecast, AirPassengers[116:144]))
##                        ME     RMSE      MAE        MPE     MAPE      MASE
## Training set 2.207596e-15 13.64072 10.86278 -0.7182150 5.219268 0.5598804
## Test set     1.701171e+00 34.42065 28.69509 -0.4970236 6.460795 1.3624492
##                  ACF1
## Training set 0.616725
## Test set           NA
airpass <- cbind(arima_forecast$mean, 
ets_forecast$mean, theta_forecast$mean, 
ridge_forecast$mean, glmnet_forecast$mean,
ts(AirPassengers[116:144], 
start=start(arima_forecast$mean),
frequency=frequency(AirPassengers)))
colnames(airpass) <- c("ARIMA", "ETS", "Theta", "Ridge", "GLMNET", "Actual")
(forecasting_methods <- colnames(airpass)[1:5])
## [1] "ARIMA"  "ETS"    "Theta"  "Ridge"  "GLMNET"
train_obs <- airpass[1:21, 6]
train_pred <- airpass[1:21, 1:5]
test_obs <- airpass[22:29, 6]
test_pred <- airpass[22:29, 1:5]

data <- ForecastComb::foreccomb(train_obs, train_pred, test_obs, test_pred)
start <- proc.time()[3]
obj <- ahead::comb_OLS(data, custom_error=custom_error)
print(proc.time()[3] - start)
## elapsed 
##   0.003
print(obj$Accuracy_Test)
##                ME     RMSE      MAE      MPE     MAPE Custom Error
## Test set 18.10097 23.41915 19.62024 3.496798 3.795867     19.62024
print(obj$Weights)
## [1]  0.5139769  4.6082642 -4.1413190 -0.1354900  0.3797416
# check
print(mean(predict(obj, test_pred) - test_obs))
## [1] -18.10097

2 - Comb Ridge

start <- proc.time()[3]
obj <- ahead::comb_Ridge(data, custom_error=custom_error)
print(proc.time()[3] - start)
## elapsed 
##   0.002
print(class(obj))
## [1] "foreccomb_res" "comb_Ridge"
print(obj$Accuracy_Test)
##                ME     RMSE      MAE      MPE     MAPE Custom Error
## Test set 11.92057 20.50185 16.29153 2.237091 3.109866     16.29153
# check 
print(mean(predict(obj, test_pred) - test_obs))
## [1] -11.92057
plot(obj)