Calculate and add a p-value comparing the two variables in the cross table. Missing values are included in p-value calculations.

# S3 method for tbl_cross
add_p(
  x,
  test = NULL,
  pvalue_fun = NULL,
  source_note = NULL,
  test.args = NULL,
  ...
)

Arguments

x

Object with class tbl_cross from the tbl_cross function

test

A string specifying statistical test to perform. Default is "chisq.test" when expected cell counts >=5 and "fisher.test" when expected cell counts <5.

pvalue_fun

Function to round and format p-value. Default is style_pvalue, except when source_note = TRUE when the default is style_pvalue(x, prepend_p = TRUE)

source_note

Logical value indicating whether to show p-value in the {gt} table source notes rather than a column.

test.args

Named list containing additional arguments to pass to the test (if it accepts additional arguments). For example, add an argument for a chi-squared test with test.args = list(correct = TRUE)

...

Not used

Example Output

Example 1

image of rendered example table

Example 2

image of rendered example table

See also

Other tbl_cross tools: inline_text.tbl_cross(), tbl_cross()

Author

Karissa Whiting

Examples

# \donttest{
# Example 1 ----------------------------------
add_p_cross_ex1 <-
  trial %>%
  tbl_cross(row = stage, col = trt) %>%
  add_p()
#> Warning: There was 1 warning in `mutate()`.
#>  In argument: `stage = (structure(function (..., .x = ..1, .y = ..2, . = ..1)
#>   ...`.
#> Caused by warning:
#> ! `fct_explicit_na()` was deprecated in forcats 1.0.0.
#>  Please use `fct_na_value_to_level()` instead.
#>  The deprecated feature was likely used in the dplyr package.
#>   Please report the issue at <https://github.com/tidyverse/dplyr/issues>.

# Example 2 ----------------------------------
add_p_cross_ex2 <-
  trial %>%
  tbl_cross(row = stage, col = trt) %>%
  add_p(source_note = TRUE)
# }