View source: R/add_difference_row.R
add_difference_row.tbl_summary | R Documentation |
Adds difference to tables created by tbl_summary()
as additional rows.
This function is often useful when there are more than two groups to compare.
Pairwise differences are calculated relative to the specified
by
variable's specified reference level.
## S3 method for class 'tbl_summary'
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()),
...
)
x |
( |
reference |
(scalar) |
statistic |
( |
test |
( See below for details on default tests and ?tests for details on available tests and creating custom tests. |
group |
( |
header |
( |
adj.vars |
( |
test.args |
( |
conf.level |
( |
include |
( |
pvalue_fun |
( |
estimate_fun |
( |
... |
These dots are for future extensions and must be empty. |
The default labels for the statistic rows will often not be what you need
to display. In cases like this, use modify_table_body()
to directly
update the label rows. Use show_header_names()
to print the underlying
column names to identify the columns to target when changing the label,
which in this case will always be the 'label'
column.
See Example 2.
a gtsummary table of class "tbl_summary"
# Example 1 ----------------------------------
trial |>
tbl_summary(
by = grade,
include = c(age, response),
missing = "no",
statistic = all_continuous() ~ "{mean} ({sd})"
) |>
add_stat_label() |>
add_difference_row(
reference = "I",
statistic = everything() ~ c("{estimate}", "{conf.low}, {conf.high}", "{p.value}")
)
# Example 2 ----------------------------------
# Function to build age-adjusted logistic regression and put results in ARD format
ard_odds_ratio <- \(data, variable, by, ...) {
cardx::construct_model(
data = data,
formula = reformulate(response = variable, termlabels = c(by, "age")), # adjusting model for age
method = "glm",
method.args = list(family = binomial)
) |>
cardx::ard_regression_basic(exponentiate = TRUE) |>
dplyr::filter(.data$variable == .env$by)
}
trial |>
tbl_summary(by = trt, include = response, missing = "no") |>
add_stat_label() |>
add_difference_row(
reference = "Drug A",
statistic = everything() ~ c("{estimate}", "{conf.low}, {conf.high}", "{p.value}"),
test = everything() ~ ard_odds_ratio,
estimate_fun = everything() ~ label_style_ratio()
) |>
# change the default label for the 'Odds Ratio'
modify_table_body(
~ .x |>
dplyr::mutate(
label = ifelse(label == "Coefficient", "Odds Ratio", label)
)
) |>
# add footnote about logistic regression
modify_footnote_body(
footnote = "Age-adjusted logistic regression model",
column = "label",
rows = variable == "response-row_difference"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.