Skip to contents

Add a column of event-rate differences to a table created with tbl_hierarchical() or tbl_ard_hierarchical(). For every node in the hierarchy (e.g. each system organ class and each preferred term) the event rate of a second by group is subtracted from the rate of a first by group. This is a wrapper around cards::diff_ard_hierarchical().

The table must be stratified by a single by variable and its statistics must include the rate (p) statistic (the default for tbl_hierarchical()).

Usage

# S3 method for class 'tbl_hierarchical'
add_difference(
  x,
  levels = NULL,
  statistic = "{estimate}%",
  estimate_fun = label_style_number(digits = 1, scale = 100),
  ...
)

# S3 method for class 'tbl_ard_hierarchical'
add_difference(
  x,
  levels = NULL,
  statistic = "{estimate}%",
  estimate_fun = NULL,
  ...
)

Arguments

x

(tbl_hierarchical/tbl_ard_hierarchical)
table created with tbl_hierarchical() or tbl_ard_hierarchical().

levels

(vector)
a length-two vector of the by variable levels to compare. The difference is calculated as levels[1] minus levels[2]. This argument is required when the by variable has more than two levels, and when by has exactly two levels it is optional and can be used to flip the direction of the difference. Default is NULL.

statistic

(string)
a single glue string defining the difference statistic to display. The only available element is {estimate} (the rate difference). Default is "{estimate}%".

estimate_fun

(function)
a function to round and format the rate difference. For add_difference.tbl_hierarchical() the default is label_style_number(digits = 1, scale = 100). For add_difference.tbl_ard_hierarchical() the default is NULL, meaning the formatting function carried in the source ARD is used.

...

These dots are for future extensions and must be empty.

Value

a gtsummary table of the same class as x

Examples

# Example 1 ----------------------------------
# rate difference between two treatment arms
ADAE_subset <- cards::ADAE |>
  dplyr::filter(AESOC %in% unique(cards::ADAE$AESOC)[1:5]) |>
  dplyr::filter(.by = AESOC, AEDECOD %in% unique(cards::ADAE$AEDECOD)[1:5])

tbl_hierarchical(
  data = ADAE_subset,
  variables = c(AESOC, AEDECOD),
  by = TRTA,
  denominator = cards::ADSL,
  id = USUBJID
) |>
  add_difference(levels = c("Xanomeline High Dose", "Placebo"))
Primary System Organ Class
    Dictionary-Derived Term
Placebo
N = 86
1
Xanomeline High Dose
N = 84
1
Xanomeline Low Dose
N = 84
1
Rate Difference2
CARDIAC DISORDERS 2 (2.3%) 3 (3.6%) 0 (0%) 1.2%
    ATRIOVENTRICULAR BLOCK SECOND DEGREE 2 (2.3%) 3 (3.6%) 0 (0%) 1.2%
GASTROINTESTINAL DISORDERS 9 (10%) 4 (4.8%) 5 (6.0%) -5.7%
    DIARRHOEA 9 (10%) 4 (4.8%) 5 (6.0%) -5.7%
GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS 8 (9.3%) 25 (30%) 24 (29%) 20.5%
    APPLICATION SITE ERYTHEMA 3 (3.5%) 15 (18%) 12 (14%) 14.4%
    APPLICATION SITE PRURITUS 6 (7.0%) 22 (26%) 22 (26%) 19.2%
SKIN AND SUBCUTANEOUS TISSUE DISORDERS 9 (10%) 14 (17%) 15 (18%) 6.2%
    ERYTHEMA 9 (10%) 14 (17%) 15 (18%) 6.2%
1 n (%)
2 Difference: Xanomeline High Dose - Placebo