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, ...)A tsibble.
The specification of aggregation structure.
<data-masking> Name-value pairs of
summary functions. The name will be the name of the variable in the result.
The value can be:
A vector of length 1, e.g. min(x), n(), or sum(is.na(y)).
A data frame with 1 row, to add multiple columns from a single expression.
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]
#> Loading required namespace: crayon
#> 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.
#> # ℹ 33,990 more rows