The "forecastHybrid" package provides functions to build composite models using multiple individual component models from the "forecast" package. These hybridModel
objects can then be manipulated with many of the familiar functions from the "forecast" and "stats" packages including forecast()
, plot()
, accuracy()
, residuals()
, and fitted()
.
The stable release of the package is hosted on CRAN and can be installed as usual.
install.packages("forecastHybrid")
The latest development version can be installed using the "devtools" package.
devtools::install_github("ellisp/forecastHybrid/pkg")
Version updates to CRAN will be published frequently after new features are implemented, so the development version is not recommended unless you plan to modify the code.
First load the package.
library(forecastHybrid)
If you don't have time to read the whole guide and want to get started immediately with sane default settings to forecast the USAccDeaths
timeseries, run the following:
quickModel <- hybridModel(USAccDeaths)
## Fitting the auto.arima model
## Fitting the ets model
## Fitting the thetam model
## Fitting the nnetar model
## Fitting the stlm model
## Fitting the tbats model
forecast(quickModel)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 1979 8354.580 7924.712 8968.899 7706.957 9272.235
## Feb 1979 7543.318 6864.957 8184.977 6542.338 8468.428
## Mar 1979 8241.755 7223.731 8886.679 6888.559 9146.115
## Apr 1979 8531.595 7606.103 9194.629 7249.674 9500.477
## May 1979 9336.334 8105.778 10112.349 7734.506 10442.376
## Jun 1979 9776.752 8519.977 10525.745 8226.434 10878.297
## Jul 1979 10683.548 9158.635 11613.448 8784.188 11987.171
## Aug 1979 9986.536 8979.890 10830.331 8697.308 11224.086
## Sep 1979 9001.395 8281.314 9944.791 7892.801 10357.609
## Oct 1979 9256.502 8309.414 10198.510 8056.898 10629.548
## Nov 1979 8781.928 8033.333 9732.246 7589.183 10180.765
## Dec 1979 9112.847 8255.912 10255.628 7816.759 10720.971
## Jan 1980 8382.947 7485.300 9496.297 7033.420 10011.749
## Feb 1980 7600.342 6687.306 8749.398 6141.503 9295.201
## Mar 1980 8249.630 7033.489 9586.522 6637.427 10161.075
## Apr 1980 8542.046 7338.446 9940.466 7010.225 10542.396
## May 1980 9333.203 7749.995 10861.976 7355.042 11490.092
## Jun 1980 9762.515 8118.797 11280.306 7714.097 11933.559
## Jul 1980 10654.632 8600.469 12373.743 8150.610 13051.201
## Aug 1980 9963.104 8598.231 11596.927 8248.308 12297.755
## Sep 1980 8988.101 7949.455 10718.099 7261.423 11441.542
## Oct 1980 9250.330 8063.717 10978.824 7417.369 11724.196
## Nov 1980 8788.215 7623.215 10519.779 6856.540 11286.453
## Dec 1980 9106.311 7938.375 11050.531 7288.262 11837.932
plot(forecast(quickModel), main = "Forecast from auto.arima, ets, thetam, nnetar, stlm, and tbats model")
The workhorse function of the package is hybridModel()
, a function that combines several component models from the "forecast" package. At a minimum, the user must supply a ts
or numeric
vector for y
. In this case, the ensemble will include all six component models: auto.arima()
, ets()
, thetam()
, nnetar()
, stlm()
, and tbats()
. To instead use only a subset of these models, pass a character string to the models
argument with the first letter of each model to include. For example, to build an ensemble model on a simulated dataset with auto.arima()
, ets()
, and tbats()
components, run
# Build a hybrid forecast on a simulated dataset using auto.arima, ets, and tbats models.
# Each model is given equal weight
set.seed(12345)
series <- ts(rnorm(18), f = 2)
hm1 <- hybridModel(y = series, models = "aet", weights = "equal")
## Fitting the auto.arima model
## Fitting the ets model
## Fitting the tbats model
The individual component models are stored inside the hybridModel
objects and can viewed in their respective slots, and all the regular methods from the "forecast" package could be applied to these individual component models.
# View the individual models
hm1$auto.arima
## Series: y
## ARIMA(0,0,0) with zero mean
##
## sigma^2 estimated as 0.6659: log likelihood=-21.88
## AIC=45.76 AICc=46.01 BIC=46.65
# See forecasts from the auto.arima model
plot(forecast(hm1$auto.arima))
The hybridModel()
function produces an S3 object of class forecastHybrid
.
class(hm1)
## [1] "hybridModel"
is.hybridModel(hm1)
## [1] TRUE
The print()
and summary()
methods print information about the ensemble model including the weights assigned to each individual component model.
print(hm1)
## Hybrid forecast model comprised of the following models: auto.arima, ets, tbats
## ############
## auto.arima with weight 0.333
## ############
## ets with weight 0.333
## ############
## tbats with weight 0.333
summary(hm1)
## Length Class Mode
## auto.arima 18 forecast_ARIMA list
## ets 19 ets list
## tbats 21 bats list
## weights 3 -none- numeric
## frequency 1 -none- numeric
## x 18 ts numeric
## xreg 1 -none- list
## models 3 -none- character
## fitted 18 -none- numeric
## residuals 18 ts numeric
Two types of plots can be created for the created ensemble model: either a plot showing the actual and fitted value of each component model on the data or individual plots of the component models as created by their regular S3 plot()
methods. Note that a plot()
method does not exist in the "forecast" package for objects generated with stlm()
, so this component model will be ignored when type = "models"
, but the other component models will be plotted regardless.
plot(quickModel, type = "fit")
plot(quickModel, type = "models")
Since version 0.4.0, ggplot
graphs are available. Note, however, that the nnetar
, and tbats
models do not have ggplot::autoplot()
methods, so these are not plotted.
plot(quickModel, type = "fit", ggplot = TRUE)
## Warning: Removed 12 row(s) containing missing values (geom_path).