Adds p-values to tables created by tbl_summary
by comparing values across groups.
add_p(x, test = NULL, pvalue_fun = NULL, group = NULL,
include = NULL, exclude = NULL)
Object with class tbl_summary
from the tbl_summary function
List of formulas specifying statistical tests to perform,
e.g. list(all_continuous() ~ "t.test", all_categorical() ~ "fisher.test")
.
Options include
"t.test"
for a t-test,
"wilcox.test"
for a Wilcoxon rank-sum test,
"kruskal.test"
for a Kruskal-Wallis rank-sum test,
"chisq.test"
for a Chi-squared test of independence,
"fisher.test"
for a Fisher's exact test,
"lme4"
for a random intercept logistic regression model to account for
clustered data, lme4::glmer(by ~ variable + (1 | group), family = binomial)
.
The by
argument must be binary for this option.
Tests default to "kruskal.test"
for continuous variables, "chisq.test"
for
categorical variables with all expected cell counts >=5, and "fisher.test"
for categorical variables with any expected cell count <5.
A custom test function can be added for all or some variables. See below for
an example.
Function to round and format p-values.
Default is style_pvalue.
The function must have a numeric vector input (the numeric, exact p-value),
and return a string that is the rounded/formatted p-value (e.g.
pvalue_fun = function(x) style_pvalue(x, digits = 2)
or equivalently,
purrr::partial(style_pvalue, digits = 2)
).
Column name of an ID or grouping variable. The column can be
used calculate p-values with correlated data (e.g. when the test argument
is "lme4"
). Default is NULL
. If specified,
the row associated with this variable is omitted from the summary table.
Names of variables to include in output.
Names of variables to exclude from output.
A tbl_summary
object
If you like to consistently use a different function to format p-values or
estimates, you can set options in the script or in the user- or
project-level startup file, '.Rprofile'. The default confidence level can
also be set. Please note the default option for the estimate is the same
as it is for tbl_regression()
.
options(gtsummary.pvalue_fun = new_function)
Example 1
Example 2
See tbl_summary vignette for detailed examples
Other tbl_summary tools: add_n
,
add_overall
,
add_q.tbl_summary
,
add_stat_label
,
bold_italicize_labels_levels
,
bold_p.tbl_summary
,
inline_text.tbl_summary
,
modify_header
,
sort_p.tbl_summary
,
tbl_summary
# NOT RUN {
add_p_ex1 <-
trial %>%
dplyr::select(age, grade, response, trt) %>%
tbl_summary(by = trt) %>%
add_p()
# Conduct a custom McNemar test for response,
# The custom function must return a single p-value, or NA
add_p_test.mcnemar <- function(data, variable, by, ...) {
stats::mcnemar.test(data[[variable]], data[[by]])$p.value
}
add_p_ex2 <-
trial %>%
dplyr::select(response, trt) %>%
tbl_summary(by = trt) %>%
add_p(test = vars(response) ~ "mcnemar")
# }
Run the code above in your browser using DataLab