Skip to contents

Adds p-values to tables created by tbl_svysummary() by comparing values across groups.

Usage

# S3 method for class 'tbl_svysummary'
add_p(
  x,
  test = list(all_continuous() ~ "svy.wilcox.test", all_categorical() ~ "svy.chisq.test"),
  pvalue_fun = label_style_pvalue(digits = 1),
  include = everything(),
  test.args = NULL,
  ...
)

Arguments

x

(tbl_svysummary)
table created with tbl_svysummary()

test

(formula-list-selector)
List of formulas specifying statistical tests to perform. Default is list(all_continuous() ~ "svy.wilcox.test", all_categorical() ~ "svy.chisq.test").

See below for details on default tests and ?tests for details on available tests and creating custom tests.

pvalue_fun

(function)
Function to round and format p-values. Default is label_style_pvalue(). The function must have a numeric vector input, and return a string that is the rounded/formatted p-value (e.g. pvalue_fun = label_style_pvalue(digits = 2)).

include

(tidy-select)
Variables to include in output. Default is everything().

test.args

(formula-list-selector)
Containing additional arguments to pass to tests that accept arguments. For example, add an argument for all t-tests, use test.args = all_tests("t.test") ~ list(var.equal = TRUE).

...

These dots are for future extensions and must be empty.

Value

a gtsummary table of class "tbl_svysummary"

Examples

# Example 1 ----------------------------------
# A simple weighted dataset
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |>
  tbl_svysummary(by = Survived, include = c(Sex, Age)) |>
  add_p()
#> The following warnings were returned during `add_p()`:
#> ! For variable `Age` (`Survived`) and "statistic" and "p.value" statistics:
#>   Chi-squared approximation may be incorrect
#> ! For variable `Sex` (`Survived`) and "statistic" and "p.value" statistics:
#>   Chi-squared approximation may be incorrect
Characteristic No
N = 1,490
1
Yes
N = 711
1
p-value2
Sex

0.048
    Male 1,364 (92%) 367 (52%)
    Female 126 (8.5%) 344 (48%)
Age

0.4
    Child 52 (3.5%) 57 (8.0%)
    Adult 1,438 (97%) 654 (92%)
1 n (%)
2 Pearson’s X^2: Rao & Scott adjustment
# A dataset with a complex design data(api, package = "survey") d_clust <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) # Example 2 ---------------------------------- tbl_svysummary(d_clust, by = both, include = c(api00, api99)) |> add_p()
Characteristic No
N = 1,692
1
Yes
N = 4,502
1
p-value2
api00 631 (559, 710) 655 (551, 723) 0.4
api99 632 (550, 701) 613 (499, 687) 0.2
1 Median (Q1, Q3)
2 Design-based KruskalWallis test
# Example 3 ---------------------------------- # change tests to svy t-test and Wald test tbl_svysummary(d_clust, by = both, include = c(api00, api99, stype)) |> add_p( test = list( all_continuous() ~ "svy.t.test", all_categorical() ~ "svy.wald.test" ) )
Characteristic No
N = 1,692
1
Yes
N = 4,502
1
p-value2
api00 631 (559, 710) 655 (551, 723) 0.4
api99 632 (550, 701) 613 (499, 687) 0.2
stype

0.049
    E 1,083 (64%) 3,791 (84%)
    H 237 (14%) 237 (5.3%)
    M 372 (22%) 474 (11%)
1 Median (Q1, Q3); n (%)
2 Design-based t-test; Design-based Wald test of association