Please read up on the source code.

gts example

Data

These are infant mortality counts. This data set is an example of gts, where the total infant mortality count in Australia can be first disaggregated by sex then by state, or vice versa.

library(hts)
## Loading required package: forecast
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
str(infantgts)
## List of 3
##  $ bts   : Time-Series [1:71, 1:16] from 1933 to 2003: 738 886 760 908 851 807 833 873 1000 912 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:16] "NSW female" "VIC female" "QLD female" "SA female" ...
##  $ groups: 'gmatrix' int [1:4, 1:16] 1 1 1 1 1 1 2 2 1 1 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:4] "Total" "Sex" "State" "Bottom"
##   .. ..$ : chr [1:16] "NSW female" "VIC female" "QLD female" "SA female" ...
##  $ labels:List of 2
##   ..$ Sex  : chr [1:2] "Sex/female" "Sex/male"
##   ..$ State: chr [1:8] "State/NSW" "State/VIC" "State/QLD" "State/SA" ...
##  - attr(*, "class")= chr "gts"
?infantgts

table(infantgts$labels[][1])
## Sex
## Sex/female   Sex/male 
##          1          1
table(infantgts$labels[][2])
## State
## State/ACT State/NSW  State/NT State/QLD  State/SA State/TAS State/VIC  State/WA 
##         1         1         1         1         1         1         1         1

Visualize the Data

plot(infantgts, levels = 1)

plot(infantgts, levels = 2)

plot(infantgts, levels = 3, cex = .1)

Model

?forecast
## Help on topic 'forecast' was found in the following packages:
## 
##   Package               Library
##   generics              /Library/Frameworks/R.framework/Versions/4.2/Resources/library
##   forecast              /Users/arvindsharma/Library/R/x86_64/4.2/library
## 
## 
## Using the first match ...
fcasts3.comb <- forecast(object = infantgts, # time series model to produce the forecasts
                         h = 4,              # The forecast horison 
                         method = "comb", 
                         fmethod = "ets"
                         )
summary(fcasts3.comb)
## Grouped Time Series 
## 4 Levels 
## Number of groups at each level: 1 2 8 16 
## Total number of series: 27 
## Number of observations in each historical series: 71 
## Number of forecasts per series: 4 
## Top level series of forecasts: 
## Time Series:
## Start = 2004 
## End = 2007 
## Frequency = 1 
## [1] 1194.008 1168.812 1143.615 1118.418
## 
## Method: Optimal combination forecasts 
## Forecast method: ETS
plot(fcasts3.comb)

Forecast

Use the aggts function in hts package - The time series from selected levels of a hierarchical/grouped time series or a forecasted hierarchical/grouped time series are returned as a multivariate time series.

Actual Data

?aggts

agg_gts2 <- aggts(y = fcasts3.comb, 
                  levels = 1, 
                  forecasts = FALSE)

plot(agg_gts2)

Predictions (into future)

agg_gts1 <- aggts(y = fcasts3.comb, # n object of class {gts}.
                  levels = 1, 
                  forecasts = TRUE # defualt value
                  )
plot(agg_gts1)