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 withtbl_svysummary()
- test
(
formula-list-selector
)
List of formulas specifying statistical tests to perform. Default islist(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 islabel_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 iseverything()
.- test.args
(
formula-list-selector
)
Containing additional arguments to pass to tests that accept arguments. For example, add an argument for all t-tests, usetest.args = all_tests("t.test") ~ list(var.equal = TRUE)
.- ...
These dots are for future extensions and must be empty.
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,4901
Yes
N = 7111
p-value2
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,6921
Yes
N = 4,5021
p-value2
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,6921
Yes
N = 4,5021
p-value2
1 Median (Q1, Q3); n (%)
2 Design-based t-test; Design-based Wald test of association