fabletools
fabletools copied to clipboard
aggregate_key() produces aggregations of single leaf nodes
This is not necessarily a bug, just something to be aware of. An option to produce unbalanced hierarchies for redundant aggregations may be useful.
library(fabletools)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
tsibble::tourism %>%
aggregate_key(Purpose * (State / Region), Trips = sum(Trips)) %>%
filter(Purpose == "Business", State == "ACT")
#> # A tsibble: 160 x 5 [1Q]
#> # Key: Purpose, State, Region [2]
#> Quarter Purpose State Region Trips
#> <qtr> <chr> <chr> <chr> <dbl>
#> 1 1998 Q1 Business ACT <aggregated> 150.
#> 2 1998 Q2 Business ACT <aggregated> 99.9
#> 3 1998 Q3 Business ACT <aggregated> 130.
#> 4 1998 Q4 Business ACT <aggregated> 102.
#> 5 1999 Q1 Business ACT <aggregated> 95.5
#> 6 1999 Q2 Business ACT <aggregated> 229.
#> 7 1999 Q3 Business ACT <aggregated> 109.
#> 8 1999 Q4 Business ACT <aggregated> 159.
#> 9 2000 Q1 Business ACT <aggregated> 105.
#> 10 2000 Q2 Business ACT <aggregated> 202.
#> # … with 150 more rows
Created on 2020-06-19 by the reprex package (v0.3.0)
This may need an option, such as drop
or trim
which removes structurally redundant nodes in the hierarchy.
MRE:
library(fabletools)
library(tsibble)
tibble::tribble(
~ grp_a, ~ grp_b, ~ time, ~ y,
"a", NA, 1, 1,
"b", "x", 1, 2,
"b", "y", 1, 3
) %>%
as_tsibble(c(grp_a, grp_b), index = time) %>%
aggregate_key(grp_a/grp_b, y = sum(y))
#> # A tsibble: 6 x 4 [?]
#> # Key: grp_a, grp_b [6]
#> time grp_a grp_b y
#> <dbl> <chr> <chr> <dbl>
#> 1 1 <aggregated> <aggregated> 6
#> 2 1 a <aggregated> 1
#> 3 1 b <aggregated> 5
#> 4 1 a NA 1
#> 5 1 b x 2
#> 6 1 b y 3
Created on 2020-08-11 by the reprex package (v0.3.0)