hts with regressors

October 19, 2014
By

(This article was first published on Hyndsight » R, and kindly contributed to R-bloggers)

The hts package for R allows for forecasting hierarchical and grouped time series data. The idea is to generate forecasts for all series at all levels of aggregation without imposing the aggregation constraints, and then to reconcile the forecasts so they satisfy the aggregation constraints. (An introduction to reconciling hierarchical and grouped time series is available in this Foresight paper.)

The base forecasts can be generated using any method, with ETS models and ARIMA models provided as options in the forecast.gts() function. As ETS models do not allow for regressors, you will need to choose ARIMA models if you want to include regressors.

Suppose x is a matrix of historical regressors (with each column containing one regressor and with the number of rows equal to the number of time periods of historical data), and f is the corresponding matrix of future regressors (with the number of rows equal to the forecast horizon). Then if y is an hts or gts object, the following code can be used for forecasting:

fc = forecast(y, fmethod="arima", xreg="x", newxreg="f")

That will fit a regression with ARIMA errors to each of the original series (at all levels of aggregation), produce forecasts from each model, and then reconcile the forecasts so they satisfy the aggregation constraints.

Infant deaths

I will illustrate using infant death numbers for Australia. These are disaggregated by state and sex as shown below. This graph is produced using plot(infantgts).

Rplot02

[Click for a larger version of the graph]

A potential forecasting method is to use a regression model on the log scale with a constant to 1970 and a decreasing trend thereafter. The figure below shows the model for the most aggregated data.

Rplot01

To apply this model to all series, and allow for ARMA errors, we can use the following code.

y = window(infantgts, start=1944)
z = pmax(time(y$bts) - 1970, 0)
fz = max(z) + 1:10
fc = forecast(y, h=10, fmethod="arima", xreg=z, newxreg=fz, lambda=0)
plot(fc)

Rplot03

[Click for a larger version of the graph]

I started the series at 1944 as there were a few zero observations before that, and taking logs caused problems. The argument lambda=0 means the models are fitted to the logged data (although reconciliation must occur on the original scale).

To leave a comment for the author, please follow the link and comment on their blog: Hyndsight » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)