General function for calculating ARD results within subgroups.

While the examples below show use with other functions from the cards package, this function would primarily be used with the statistical functions in the cardx functions.

ard_strata(.data, .by = NULL, .strata = NULL, .f, ...)

Arguments

.data

(data.frame)
a data frame

.by, .strata

(tidy-select)
columns to tabulate by/stratify by for calculation. Arguments are similar, but with an important distinction:

.by: results are tabulated by all combinations of the columns specified, including unobserved combinations and unobserved factor levels.

.strata: results are tabulated by all observed combinations of the columns specified.

These argument should not include any columns that appear in the .f argument.

.f

(function, formula)
a function or a formula that can be coerced to a function with rlang::as_function() (similar to purrr::map(.f))

...

Additional arguments passed on to the .f function.

Value

an ARD data frame of class 'card'

Examples

# Example 1 ----------------------------------
ard_strata(
  ADSL,
  .by = ARM,
  .f = ~ ard_summary(.x, variables = AGE)
)
#> # An ARD data frame: 24 × 10
#>    group1 group1_level       variable context stat_name stat_label  stat fmt_fun
#>    <chr>  <list>             <chr>    <chr>   <chr>     <chr>      <lis>  <list>
#>  1 ARM    Placebo            AGE      summary N         N          86          0
#>  2 ARM    Placebo            AGE      summary mean      Mean       75.2        1
#>  3 ARM    Placebo            AGE      summary sd        SD          8.59       1
#>  4 ARM    Placebo            AGE      summary median    Median     76          1
#>  5 ARM    Placebo            AGE      summary p25       Q1         69          1
#>  6 ARM    Placebo            AGE      summary p75       Q3         82          1
#>  7 ARM    Placebo            AGE      summary min       Min        52          1
#>  8 ARM    Placebo            AGE      summary max       Max        89          1
#>  9 ARM    Xanomeline High D… AGE      summary N         N          84          0
#> 10 ARM    Xanomeline High D… AGE      summary mean      Mean       74.4        1
#> # ℹ 14 more rows
#> # ℹ 2 more variables: warning <list>, error <list>

# Example 2 ----------------------------------
df <- data.frame(
  USUBJID = 1:12,
  PARAMCD = rep(c("PARAM1", "PARAM2"), each = 6),
  AVALC = c(
    "Yes", "No", "Yes", # PARAM1
    "Yes", "Yes", "No", # PARAM1
    "Low", "Medium", "High", # PARAM2
    "Low", "Low", "Medium" # PARAM2
  )
)

ard_strata(
  df,
  .strata = PARAMCD,
  .f = \(.x) {
    lvls <-
      switch(.x[["PARAMCD"]][1],
        "PARAM1" = c("Yes", "No"),
        "PARAM2" = c("Zero", "Low", "Medium", "High")
      )

    .x |>
      dplyr::mutate(AVALC = factor(AVALC, levels = lvls)) |>
      ard_tabulate(variables = AVALC)
  }
)
#> # An ARD data frame: 18 × 11
#>    group1  group1_level variable variable_level context  stat_name   stat
#>    <chr>   <list>       <chr>    <list>         <chr>    <chr>     <list>
#>  1 PARAMCD PARAM1       AVALC    Yes            tabulate n          4    
#>  2 PARAMCD PARAM1       AVALC    Yes            tabulate N          6    
#>  3 PARAMCD PARAM1       AVALC    Yes            tabulate p          0.667
#>  4 PARAMCD PARAM1       AVALC    No             tabulate n          2    
#>  5 PARAMCD PARAM1       AVALC    No             tabulate N          6    
#>  6 PARAMCD PARAM1       AVALC    No             tabulate p          0.333
#>  7 PARAMCD PARAM2       AVALC    Zero           tabulate n          0    
#>  8 PARAMCD PARAM2       AVALC    Zero           tabulate N          6    
#>  9 PARAMCD PARAM2       AVALC    Zero           tabulate p          0    
#> 10 PARAMCD PARAM2       AVALC    Low            tabulate n          3    
#> 11 PARAMCD PARAM2       AVALC    Low            tabulate N          6    
#> 12 PARAMCD PARAM2       AVALC    Low            tabulate p          0.5  
#> 13 PARAMCD PARAM2       AVALC    Medium         tabulate n          2    
#> 14 PARAMCD PARAM2       AVALC    Medium         tabulate N          6    
#> 15 PARAMCD PARAM2       AVALC    Medium         tabulate p          0.333
#> 16 PARAMCD PARAM2       AVALC    High           tabulate n          1    
#> 17 PARAMCD PARAM2       AVALC    High           tabulate N          6    
#> 18 PARAMCD PARAM2       AVALC    High           tabulate p          0.167
#> # ℹ 4 more variables: stat_label <chr>, fmt_fun <list>, warning <list>,
#> #   error <list>