Skip to contents

Adds a column of p-values to a table created by tbl_summary(), comparing each variable across the levels of the by variable.

Usage

add_p(
  x,
  test = NULL,
  pvalue_fun = label_style_pvalue(digits = 1),
  group = NULL,
  include = everything(),
  test.args = NULL,
  adj.vars = NULL,
  ...
)

Arguments

x

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

test

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

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

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.

include

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

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

adj.vars

(selector)
Variables to adjust for in an ANCOVA: with adj.vars, the default test for a continuous variable when by has two levels is "ancova", lm(variable ~ by + adj.vars). Default is NULL.

...

Not used.

Value

A table of class c("tbl_summary", "ltsummary") with a p.value column. The full test results (statistic, parameter, estimate, conf.low, conf.high) are stored as hidden columns.

test argument

See ?tests for the available tests, pseudo-code showing exactly how each p-value is computed, and the interface for custom test functions.

The default test depends on the summary type of the variable and on the number of levels of the by variable:

  • "wilcox.test" for continuous variables when by has two levels.

  • "kruskal.test" for continuous variables when by has more than two levels.

  • "chisq.test.no.correct" for categorical and dichotomous variables when every expected cell count is 5 or more, and "fisher.test" when any expected cell count is below 5.

  • "ancova" for continuous variables when adj.vars is given and by has two levels.

There is no default for paired data, nor for categorical variables with adj.vars. When group is specified, name the test for every variable, e.g. test = everything() ~ "paired.t.test".

The test used for each variable is recorded in the test_name column of x$table_body, and the method names appear in the footnote of the p-value column.

See also

tests for the test registry and custom tests, bold_p() to highlight small p-values, and all_tests() to select variables by test.

add_p_cross for the arguments of add_p() on a cross table.

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

Examples

# Example 1 ----------------------------------
# the default tests: Wilcoxon rank sum for age, chi-squared or Fisher for grade
trial |>
  tbl_summary(by = trt, include = c(age, grade)) |>
  add_p()
# Example 2 ---------------------------------- # choose the tests and pass arguments to them trial |> tbl_summary(by = trt, include = c(age, marker), missing = "no") |> add_p( # perform t-test for all variables test = everything() ~ "t.test", # assume equal variance in the t-test test.args = all_tests("t.test") ~ list(var.equal = TRUE), pvalue_fun = label_style_pvalue(digits = 2) )
# Example 3 ---------------------------------- # more than two groups: Kruskal-Wallis and chi-squared by default trial |> tbl_summary(by = grade, include = c(age, response), missing = "no") |> add_p()
# Example 4 ---------------------------------- # a custom test: a function returning a list with a p-value and a method welch_anova <- function(data, variable, by, ...) { res <- oneway.test(data[[variable]] ~ as.factor(data[[by]])) list(p.value = res$p.value, method = "Welch's ANOVA") } trial |> tbl_summary(by = grade, include = c(age, marker), missing = "no") |> add_p(test = all_continuous() ~ welch_anova)