cascade is similar to summarise, but calculates a summary statistics for the total of a group in addition to each group. The groupings are chosen by "unpeeling" from the end of the groupings, and also expanding out interactions to all terms (eg the interactions of all combinations of subsets of variables as well as each variable on it's own).

cascade(.data, ..., .fill = NA, .fill_level_top = FALSE, .groupings = NULL)

Arguments

.data,

tbl A tbl_svy object

...

Name-value pairs of summary functions

.fill

Value to fill in for group summaries

.fill_level_top

When filling factor variables, whether to put the value `.fill` in the first position (defaults to FALSE, placing it in the bottom).

.groupings

(Experimental) A list of lists of quosures to manually specify the groupings to use, rather than the default.

Examples

library(survey)
data(api)

dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

# Calculates the means by stype and also for the whole
# sample
dstrata %>%
  group_by(stype) %>%
  cascade(api99_mn = survey_mean(api99),
            api00_mn = survey_mean(api00),
            api_diff = survey_mean(api00 - api99))
#> # A tibble: 4 × 7
#>   stype api99_mn api99_mn_se api00_mn api00_mn_se api_diff api_diff_se
#>   <fct>    <dbl>       <dbl>    <dbl>       <dbl>    <dbl>       <dbl>
#> 1 E         636.        13.3     674.       12.5     38.6         2.76
#> 2 H         617.        15.8     626.       15.5      8.46        3.41
#> 3 M         610.        16.8     637.       16.6     26.4         3.05
#> 4 NA        629.        10.1     662.        9.54    32.9         2.08

# Calculates the proportions by the interaction of stype & awards
# as well as by each of those variable's groups alone, and finally
# the total as well
dstrata %>%
  group_by(interact(stype, awards)) %>%
  cascade(prop = survey_mean())
#> # A tibble: 12 × 4
#>    stype awards   prop  prop_se
#>    <fct> <fct>   <dbl>    <dbl>
#>  1 E     No     0.193  3.18e- 2
#>  2 E     Yes    0.521  3.18e- 2
#>  3 E     NA     0.714  1.28e-17
#>  4 H     No     0.0829 8.12e- 3
#>  5 H     Yes    0.0390 8.12e- 3
#>  6 H     NA     0.122  1.35e-17
#>  7 M     No     0.0855 1.17e- 2
#>  8 M     Yes    0.0789 1.17e- 2
#>  9 M     NA     0.164  2.67e-17
#> 10 NA    No     0.361  3.49e- 2
#> 11 NA    Yes    0.639  3.49e- 2
#> 12 NA    NA     1.00   5.15e-32

# Can also specify the .groupings manually, though this interface
# is a little ugly, as it requires passing a list of quosures or
# symbols you've created, rather than the usual syntax
dstrata %>%
  cascade(
    prop = survey_mean(),
    .groupings = list(rlang::quos(stype, awards), rlang::quos(NULL))
  )
#> # A tibble: 7 × 4
#> # Groups:   stype [4]
#>   stype awards  prop  prop_se
#>   <fct> <fct>  <dbl>    <dbl>
#> 1 E     No     0.27  4.46e- 2
#> 2 E     Yes    0.730 4.46e- 2
#> 3 H     No     0.68  6.66e- 2
#> 4 H     Yes    0.32  6.66e- 2
#> 5 M     No     0.52  7.14e- 2
#> 6 M     Yes    0.48  7.14e- 2
#> 7 NA    NA     1.00  5.15e-32