# Forecasting hierarchical time series R package

I have to forecast a large set of (hierarchical) time series and since the R package hts allows for confidence intervals for their ensemble, I'd like to use it. I haven't found an example of how to use it, yet. How should I forecast the following series with it: The time series have a simple form. The sum of $n_3$ level-$3$-series make up a level-$2$-series and the sum of $n_2$ level-$2$-series make up a level-$1$-series and the sum of $n_1$ level-$1$-series make up the top-most level-$0$-series (where sums are taken over series in one point in time, $n_j$ natural numbers for $j=0,1,2,3$).

-
There are examples in the help files. Please take the time to read the documentation. –  Rob Hyndman Jul 1 '12 at 9:36
Well, the most difficult part when reading the paper Optimal combination forecasts for hierarchical time series was picturing how to implement the relations of the time series. At the bottom of documentation p. 7 there's an example of how to create a hierarchical time series. While I do not understand the second parameter of 'gts(abc, rbind(c(1,1,2,2), c(1,2,1,2)))', the call 'hts(abc, c(1,1,2,2))' is straight forward. Sadly, calls to 'hts(., c(1,...,n))' not necessarily yield exactly 4 levels, so clarification on 'gts' would be nice. –  Konsta Jul 2 '12 at 7:43

Here is one example taken from the documentation in the package:

abc <- ts(5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 100))
x <- hts(abc, c(1,1,2,2))
y <- gts(abc, rbind(c(1,1,2,2), c(1,2,1,2)))


The first line generates a matrix of four time series, each of 100 observations. The second line interprets the four time series as hierarchical, with the first two series in one group and the second two series in a second group:

      Total
|
|-------|
A       B
|       |
-----   -----
|   |   |   |
A1  A2  B1  B2


This gives seven series in all including the aggregations.

> x
Hierarchical Time Series
3 Levels
Number of series at each level: 1 2 4
Total number of series: 7
Number of observations per series: 100


The third line of the example interprets the four series as grouped rather than hierarchical, with one grouping variable treating the first two series in one group and the remaining two series in the second group. The other grouping variable takes the first and third series in one group and the second and fourth in the other group. So in this interpretation, the four series can be grouped as in the hierarchy above, or as in the hierarchy below:

      Total
|
|-------|
X       Y
|       |
-----   -----
|   |   |   |
A1  B1  A2  B2


So there are nine series in all under this structure, due to the additional grouping variable.

> y
Grouped Time Series
4 Levels
Number of groups at each level: 1 2 2 4
Total number of series: 9
Number of observations per series: 100

-
Thank you for the clarification, Rob. Thus, my example seems to be an hts. Playing around with hts yielded either 2 or 3 levels. While I could drop a level in-between in my example, I wonder if it is possible to have, say, 4 levels? Further, hts() calls gts(). 'gts' reveals its code and examining it shows that the necessary g can be hand-crafted g <- rbind(c(1,1,1,1),c(1,2,2,2),c(1,2,2,3),c(1,2,3,4)) and then be used when calling 'hts'. Hm, I cannot find confidence intervals and accuracy.gts yields Error in window.default(x, ...) : 'start' cannot be after 'end' –  Konsta Jul 2 '12 at 11:36
Since forecast.hts() calls forecast package's forecast() with '\$mean' I think one needs to tailor an appropriate copy of forecast.hts to retrieve confidence intervals. –  Konsta Jul 2 '12 at 11:56
hts prediction intervals are not yet available. –  Rob Hyndman Jul 2 '12 at 12:16