Aureon
Aureon

Reputation: 141

hts method for creating hierarchical time series

I am trying to convert a time series into a hierarchical one using the hts package and my structure is as follows:

enter image description here

My data is already stored as a ts object in R with a weekly frequency and all columns are labeled:

enter image description here

Then I try to make the hts object but I get this error message, probably because I do not fully understand how to split the columns in the argument "characters".

x <- hts(abc, c(1,1,2,2))
Since argument characters are not specified, the default labelling system is used.
Error: Argument nodes must be a list.

Thanks in advance

Upvotes: 1

Views: 355

Answers (1)

Rob Hyndman
Rob Hyndman

Reputation: 31800

First you need to use the argument characters. It is not the second argument of hts(). Second, you need to specify characters according to the names of your columns. In this case, the first character is the first level of disaggregation (A, B, C, D, E), while the next two characters specify the next level of disaggregation. So your argument should be characters = c(1,2).

Here is an example with synthetic data, but with the same structure as yours.

library(hts)
abc <- matrix(sample(1:100, 32*140, replace=TRUE), ncol=32)
colnames(abc) <- c(
  paste0("A0",1:5), 
  paste0("B0",1:9),"B10",
  paste0("C0",1:8),
  paste0("D0",1:5),
  paste0("E0",1:4)
)
abc <- ts(abc, start=2019, frequency=365.25/7)
x <- hts(abc, characters = c(1,2))
x
#> Hierarchical Time Series 
#> 3 Levels 
#> Number of nodes at each level: 1 5 32 
#> Total number of series: 38 
#> Number of observations per series: 140 
#> Top level series: 
#> Time Series:
#> Start = 2019 
#> End = 2021.66392881588 
#> Frequency = 52.1785714285714 
#>   [1] 1735 1645 1472 1638 1594 1722 1525 1761 1500 1746 1331 1567 1853 1652 1587
#>  [16] 1540 1453 1989 1629 1587 1596 1474 1320 1599 1762 1419 1931 1447 2102 1608
#>  [31] 1439 1909 1331 1742 1428 1677 1534 1657 1741 1612 1574 1954 1542 2067 1512
#>  [46] 1850 1650 1666 1321 1332 1924 1786 1496 1695 1363 1437 1740 1448 1260 1371
#>  [61] 1661 1726 1786 1641 1463 1616 1641 1895 1503 1430 1972 1705 1722 1447 1515
#>  [76] 1636 1544 1727 1960 1647 1682 1569 1616 1628 1706 1837 1738 1659 1574 1716
#>  [91] 1409 1428 1411 1708 1606 1501 1413 1707 1552 1567 1693 1748 2034 1557 1402
#> [106] 1649 1637 1653 1857 1401 1519 1600 1844 1585 1796 1612 1456 1626 1390 1368
#> [121] 1492 1765 1644 1773 1302 2027 1810 1652 1819 1628 1574 1655 1650 1817 1605
#> [136] 1422 1793 1999 1489 1667

Created on 2021-10-18 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions