Adds p-values to tables created by tbl_summary by comparing values across groups.

# S3 method for tbl_summary
add_p(
  x,
  test = NULL,
  pvalue_fun = NULL,
  group = NULL,
  include = everything(),
  exclude = NULL,
  ...
)

Arguments

x

Object with class tbl_summary from the tbl_summary function

test

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,

  • "aov" for a one-way ANOVA 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,

  • "chisq.test.no.correct" for a chi-squared test of independence without continuity correction,

  • "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.

pvalue_fun

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)).

group

Column name (unquoted or quoted) of an ID or grouping variable. The column can be used to 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.

include

Variables to include in output. Input may be a vector of quoted variable names, unquoted variable names, or tidyselect select helper functions. Default is everything().

exclude

DEPRECATED

...

Not used

Value

A tbl_summary object

Setting Defaults

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 start-up 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().

Example Output

Example 1

Example 2

See also

Author

Emily C. Zabor, Daniel D. Sjoberg

Examples

# Example 1 ---------------------------------- add_p_ex1 <- trial[c("age", "grade", "trt")] %>% tbl_summary(by = trt) %>% add_p() # Example 2 ---------------------------------- # Conduct a custom McNemar test for response, # Function must return a named list of the p-value and the # test name: list(p = 0.123, test = "McNemar's test") # The '...' must be included as input # This feature is experimental, and the API may change in the future my_mcnemar <- function(data, variable, by, ...) { result <- list() result$p <- stats::mcnemar.test(data[[variable]], data[[by]])$p.value result$test <- "McNemar's test" result } add_p_ex2 <- trial[c("response", "trt")] %>% tbl_summary(by = trt) %>% add_p(test = response ~ "my_mcnemar")