Hierarchical time series forecast using hts library with fourier as xreg

Hello,

I don't see many posts here related to the library hts, but I'm hoping someone here is experienced in using the library for hierarchical forecasting. I am trying to apply the following code, which works with a normal ts object, using an hts object instead:

#Time series with period of 101 means fourier transform is the best way to deal with seasonality
data=ts(MyData,frequency=101)
fit <- auto.arima(data, xreg = fourier(data, K = 11),
                    seasonal = FALSE, lambda = 1)

When I do the same with an hts object, it won't accept the fourier transform as xreg, either applying fourier to the ts object:

> fcast<-forecast(data.hts, h=101, fmethod='arima', newxreg=fourier(data,K=11))

Or when applying it to the hts$bts:

> fcast<-forecast(data.hts, h=101, fmethod='arima', newxreg=fourier(data.hts$bts,K=11))

I get the same error:

Error in forecast.forecast_ARIMA(models, h = h, xreg = newxreg) :
Number of regressors does not match fitted model

If anyone has insight into this I would be very appreciative!

Thank you

forecast.hts needs two xreg matrices, one for the historical period and one for the future period.

Here is some code which does what you want.

library(hts)
#> Loading required package: forecast

MyData <- matrix(rnorm(303 * 3), ncol = 3) %>% ts(frequency = 101)
data.hts <- MyData %>% hts()

future <- matrix(numeric(101 * 3), ncol = 3) %>%
  ts(frequency = 101, start = tsp(MyData)[2] + 1 / 101)

fcast <- forecast(data.hts,
  h = 101, fmethod = "arima",
  seasonal = FALSE,
  xreg = fourier(MyData, K = 11),
  newxreg = fourier(future, K = 11)
)

Created on 2019-12-06 by the reprex package (v0.3.0)

MyData contains the bottom level series, while data.hts is the hierarchical time series object. future is just an empty matrix with the same attributes as what the future of MyData would look like. It is created only in order for fourier() to know how to construct the right matrix for the future periods.

The syntax is a little cumbersome. The new fable package is much better at handling this, but the documentation for hierarchical time series is still sparse (we are working on it).

4 Likes

Oh I see, I missed the distinction between xreg and newxreg! I had found a workaround doing the individual ARIMA's and then using combinef but this will allow me to test other methods as well. Thanks!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.