Adds difference to tables created by tbl_summary().
The difference between two groups (typically mean or rate difference) is added
to the table along with the difference's confidence interval and a p-value (when applicable).
Usage
# S3 method for class 'tbl_summary'
add_difference(
x,
test = NULL,
group = NULL,
adj.vars = NULL,
test.args = NULL,
conf.level = 0.95,
levels = NULL,
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)
table created withtbl_summary()- test
(
formula-list-selector)
Specifies the tests/methods to perform for each variable, e.g.list(all_continuous() ~ "t.test", all_dichotomous() ~ "prop.test", all_categorical(FALSE) ~ "smd").See below for details on default tests and ?tests for details on available tests and creating custom tests.
- group
(
tidy-select)
Variable name of an ID or grouping variable. The column can be used to calculate p-values with correlated data. Default isNULL. See tests for methods that utilize thegroupargument.- adj.vars
(
tidy-select)
Variables to include in adjusted calculations (e.g. in ANCOVA models). Default isNULL.- test.args
(
formula-list-selector)
Containing additional arguments to pass to tests that accept arguments. For example, add an argument for all t-tests, usetest.args = all_tests("t.test") ~ list(var.equal = TRUE).- conf.level
(
numeric)
a scalar in the interval(0, 1)indicating the confidence level. Default is 0.95- levels
(
vector)
a length-two vector of thetbl_summary(by=)levels to compare. The difference is calculated aslevels[1]minuslevels[2]. This argument is required when thebyvariable has more than two levels, and allows the user to select which two groups to compare. Whenbyhas exactly two levels, this argument is optional and can be used to flip the direction of the difference (e.g.levels[2]minuslevels[1]). Default isNULL.- include
(
tidy-select)
Variables to include in output. Default iseverything().- pvalue_fun
(
function)
Function to round and format p-values. Default islabel_style_pvalue(). The function must have a numeric vector input, and return a string that is the rounded/formatted p-value (e.g.pvalue_fun = label_style_pvalue(digits = 2)).- estimate_fun
(
formula-list-selector)
List of formulas specifying the functions to round and format differences and confidence limits.- ...
These dots are for future extensions and must be empty.
Examples
# Example 1 ----------------------------------
trial |>
select(trt, age, marker, response, death) %>%
tbl_summary(
by = trt,
statistic =
list(
all_continuous() ~ "{mean} ({sd})",
all_dichotomous() ~ "{p}%"
),
missing = "no"
) |>
add_n() |>
add_difference()
Characteristic
N
Drug A
N = 981
Drug B
N = 1021
Difference2
95% CI2
p-value2
1 Mean (SD); %
2 Welch Two Sample t-test; 2-sample test for equality of proportions with continuity correction
Abbreviation: CI = Confidence Interval
# Example 2 ----------------------------------
# ANCOVA adjusted for grade and stage
trial |>
select(trt, age, marker, grade, stage) %>%
tbl_summary(
by = trt,
statistic = list(all_continuous() ~ "{mean} ({sd})"),
missing = "no",
include = c(age, marker, trt)
) |>
add_n() |>
add_difference(adj.vars = c(grade, stage))
Characteristic
N
Drug A
N = 981
Drug B
N = 1021
Adjusted Difference2
95% CI2
p-value2
1 Mean (SD)
2 ANCOVA
Abbreviation: CI = Confidence Interval
# Example 3 ----------------------------------
# Select two groups to compare when `by=` has 3+ levels
trial |>
tbl_summary(
by = grade,
statistic = all_continuous() ~ "{mean} ({sd})",
include = c(age, marker),
missing = "no"
) |>
add_difference(levels = c("I", "III"))
Characteristic
I
N = 681
II
N = 681
III
N = 641
Difference2,3
95% CI2,3
p-value2,3
1 Mean (SD)
2 Welch Two Sample t-test
3 Difference: I - III
Abbreviation: CI = Confidence Interval
