Add rate differences to a hierarchical table
Source:R/add_difference_hierarchical.R
add_difference_hierarchical.Rdadd_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.
Arguments
x(tbl_hierarchical)
A table created withtbl_hierarchical()and abyvariable.levels(vector)
The twobylevels to compare, the difference beinglevels[1]minuslevels[2]. Required whenbyhas more than two levels; with two levels it can flip the direction. Default isNULL, 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 islabel_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))