Skip to contents

add_difference() on a tbl_hierarchical() table adds a column with the difference between the event rates of two by levels on every row of the hierarchy: levels[1] minus levels[2], formatted by estimate_fun and placed in the statistic pattern. The column is headed "Rate Difference", a footnote names the pair, and the overall row of overall_row = TRUE stays blank. gtsummary computes the differences on its cards ARD; ltsummary subtracts the rates stored when the table was built, so a table sorted with sort_hierarchical() or filtered with filter_hierarchical() works too.

The table must be stratified by a by variable, and there is no method for tbl_hierarchical_count() tables, as in gtsummary.

Value

The table with an estimate column.

Arguments

  • x (tbl_hierarchical)
    A table created with tbl_hierarchical() and a by variable.

  • levels (vector)
    The two by levels to compare, the difference being levels[1] minus levels[2]. Required when by has more than two levels; with two levels it can flip the direction. Default is NULL, the two levels in their order.

  • statistic (string)
    The pattern of the new column, where {estimate} is the only element. Default is "{estimate}%".

  • estimate_fun (function)
    Formats the difference. Default is label_style_number(digits = 1, scale = 100).

  • ... Not used.

See also

add_difference() for the differences of a summary table.

Other hierarchical tables: filter_hierarchical(), sort_hierarchical(), tbl_hierarchical()

Examples

ae <- data.frame(
  id = c(1L, 1L, 1L, 2L, 2L, 3L, 5L, 5L, 6L),
  trt = c("A", "A", "A", "A", "A", "A", "B", "B", "B"),
  soc = c("Nervous", "Nervous", "Gastro", "Nervous", "Gastro",
          "Gastro", "Nervous", "Gastro", "Gastro"),
  term = c("Headache", "Headache", "Nausea", "Dizziness", "Nausea",
           "Vomiting", "Headache", "Nausea", "Nausea")
)
enrolled <- data.frame(id = 1:8, trt = rep(c("A", "B"), each = 4))
tbl <- tbl_hierarchical(ae, variables = c(soc, term), by = trt,
                        denominator = enrolled, id = id)

# Example 1 ----------------------------------
add_difference(tbl)
# Example 2 ---------------------------------- # the pair in the other order, shown as a proportion add_difference(tbl, levels = c("B", "A"), statistic = "{estimate}", estimate_fun = label_style_sigfig(digits = 2))