Uses the structural specification given in .spec
to aggregate a time
series. A grouped structure is specified using grp1 * grp2
, and a nested
structure is specified via parent / child
. Aggregating the key structure is
commonly used with forecast reconciliation to produce coherent forecasts over
some hierarchy.
aggregate_key(.data, .spec, ...)
.data | A tsibble. |
---|---|
.spec | The specification of aggregation structure. |
... | < The value can be:
|
This function is experimental, and is subject to change in the future.
The way in which the measured variables are aggregated is specified in a
similar way to how [dplyr::summarise()]
is used.
library(tsibble) tourism %>% aggregate_key(Purpose * (State / Region), Trips = sum(Trips)) #> # A tsibble: 34,000 x 5 [1Q] #> # Key: Purpose, State, Region [425] #> Quarter Purpose State Region Trips #> <qtr> <chr*> <chr*> <chr*> <dbl> #> 1 1998 Q1 <aggregated> <aggregated> <aggregated> 23182. #> 2 1998 Q2 <aggregated> <aggregated> <aggregated> 20323. #> 3 1998 Q3 <aggregated> <aggregated> <aggregated> 19827. #> 4 1998 Q4 <aggregated> <aggregated> <aggregated> 20830. #> 5 1999 Q1 <aggregated> <aggregated> <aggregated> 22087. #> 6 1999 Q2 <aggregated> <aggregated> <aggregated> 21458. #> 7 1999 Q3 <aggregated> <aggregated> <aggregated> 19914. #> 8 1999 Q4 <aggregated> <aggregated> <aggregated> 20028. #> 9 2000 Q1 <aggregated> <aggregated> <aggregated> 22339. #> 10 2000 Q2 <aggregated> <aggregated> <aggregated> 19941. #> # … with 33,990 more rows