Skip to contents

Adds rows below each variable of a table created with tbl_summary(by =) comparing every other level of the by variable against the reference level: the difference in means for continuous variables, the difference in proportions for dichotomous variables, and the standardized mean difference for categorical variables. Each comparison subsets the data to the two levels involved, so the estimate is the reference level minus the compared level, shown in the compared level's column.

Usage

add_difference_row(
  x,
  reference,
  statistic = everything() ~ "{estimate}",
  test = NULL,
  group = NULL,
  header = NULL,
  adj.vars = NULL,
  test.args = NULL,
  conf.level = 0.95,
  include = everything(),
  pvalue_fun = label_style_pvalue(digits = 1),
  estimate_fun = list(c(all_continuous(), all_categorical(FALSE)) ~ label_style_sigfig(),
    all_dichotomous() ~ label_style_sigfig(scale = 100, suffix = "%"), all_tests("smd")
    ~ label_style_sigfig()),
  ...
)

Arguments

x

(tbl_summary)
A table created with tbl_summary() with a by variable.

reference

(scalar)
The level of the by variable the other levels are compared against, as a character string.

statistic

(formula-list)
The statistics shown, one row per element, as "{...}" templates per variable. Default is everything() ~ "{estimate}". Available statistics are those the method returns: estimate, std.error, parameter, statistic, conf.low, conf.high and p.value, e.g. statistic = everything() ~ c("{estimate}", "{conf.low}, {conf.high}", "{p.value}").

test

(formula-list)
The method for each variable, e.g. list(all_continuous() ~ "t.test", all_dichotomous() ~ "prop.test", all_categorical(FALSE) ~ "smd"). See the section below for the defaults and ?tests for the available methods and how to write your own.

group

(selector)
Column identifying the pairs or groups of correlated observations, used by the paired tests. Default is NULL. See tests for the methods that use it.

header

(string)
Text of an unindented label row placed above each variable's difference rows. Default is NULL, no row.

adj.vars

(selector)
Variables to adjust for in an ANCOVA. With adj.vars, the default method for continuous variables is "ancova" and the estimate is headed "Adjusted Difference". Default is NULL.

test.args

(formula-list)
Additional arguments passed to the tests, as a named list per variable. To assume equal variances in every t-test, use test.args = all_tests("t.test") ~ list(var.equal = TRUE).

conf.level

(scalar numeric)
Confidence level of the interval. Default is 0.95.

include

(selector)
Variables to compute a p-value for. Default is everything().

pvalue_fun

(function)
Function that rounds and formats the p-values. Default is label_style_pvalue(). The function takes a numeric vector and returns a character vector, e.g. pvalue_fun = label_style_pvalue(digits = 2).

estimate_fun

(formula-list)
Functions that format the statistics other than the p-value, per variable. The default formats differences in proportions as percentages and everything else with label_style_sigfig().

...

Not used.

Value

A table of class c("tbl_summary", "ltsummary") with row_type = "difference_row" rows under each included variable. The reference level's column shows an em dash on those rows.

test argument

The methods are those of add_difference(), with the same defaults: "t.test" for continuous variables ("ancova" when adj.vars is given), "prop.test" for dichotomous variables and "smd" for categorical variables. Each comparison runs on the subset of the data holding the reference level and the compared level, with the reference level first. See ?tests for the methods and how to write your own.

See also

add_difference() for a difference column instead of rows.

Other add statistics: add_ci(), add_difference(), add_n(), add_overall(), add_p(), add_p_continuous, add_q(), add_stat(), add_stat_label(), tests

Examples

# Example 1 ----------------------------------
trial |>
  tbl_summary(
    by = grade,
    include = c(age, response),
    statistic = all_continuous() ~ "{mean} ({sd})",
    missing = "no"
  ) |>
  add_difference_row(
    reference = "I",
    statistic = everything() ~ c("{estimate}", "{conf.low}, {conf.high}", "{p.value}"),
    header = "Difference vs. Grade I"
  )