A pipe-friendly function to add an adjusted p-value column into a data frame. Supports grouped data.
Arguments
- data
a data frame containing a p-value column
- p.col
column name containing p-values
- output.col
the output column name to hold the adjusted p-values
- method
method for adjusting p values (see
p.adjust). Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none".
Details
For grouped data (and, equivalently, when a test is run on
data grouped with dplyr::group_by() using an in-test
p.adjust.method), the p-value adjustment is computed within
each group separately, not across all groups. If you instead want a single
family of comparisons adjusted across all groups, run the test without
adjustment (p.adjust.method = "none") and then call
adjust_pvalue() on the combined result (see the grouped example
below).
Examples
# Perform pairwise comparisons and adjust p-values
ToothGrowth %>%
t_test(len ~ dose) %>%
adjust_pvalue()
#> # A tibble: 3 × 10
#> .y. group1 group2 n1 n2 statistic df p p.adj p.adj.signif
#> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 len 0.5 1 20 20 -6.48 38.0 1.27e- 7 2.54e- 7 ****
#> 2 len 0.5 2 20 20 -11.8 36.9 4.40e-14 1.32e-13 ****
#> 3 len 1 2 20 20 -4.90 37.1 1.91e- 5 1.91e- 5 ****
# Grouped data: adjustment within vs across groups
# Per-group adjustment (within each supp level):
ToothGrowth %>%
group_by(supp) %>%
t_test(len ~ dose) # in-test holm, adjusted within each group
#> # A tibble: 6 × 11
#> supp .y. group1 group2 n1 n2 statistic df p p.adj
#> * <fct> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 OJ len 0.5 1 10 10 -5.05 17.7 0.0000878 0.000176
#> 2 OJ len 0.5 2 10 10 -7.82 14.7 0.00000132 0.00000397
#> 3 OJ len 1 2 10 10 -2.25 15.8 0.0392 0.0392
#> 4 VC len 0.5 1 10 10 -7.46 17.9 0.000000681 0.00000136
#> 5 VC len 0.5 2 10 10 -10.4 14.3 0.0000000468 0.000000140
#> 6 VC len 1 2 10 10 -5.47 13.6 0.0000916 0.0000916
#> # ℹ 1 more variable: p.adj.signif <chr>
# One family across ALL comparisons (all groups together):
ToothGrowth %>%
group_by(supp) %>%
t_test(len ~ dose, p.adjust.method = "none") %>%
adjust_pvalue(method = "holm")
#> # A tibble: 6 × 11
#> supp .y. group1 group2 n1 n2 statistic df p p.adj
#> <fct> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 OJ len 0.5 1 10 10 -5.05 17.7 0.0000878 0.000264
#> 2 OJ len 0.5 2 10 10 -7.82 14.7 0.00000132 0.00000530
#> 3 OJ len 1 2 10 10 -2.25 15.8 0.0392 0.0392
#> 4 VC len 0.5 1 10 10 -7.46 17.9 0.000000681 0.00000341
#> 5 VC len 0.5 2 10 10 -10.4 14.3 0.0000000468 0.000000281
#> 6 VC len 1 2 10 10 -5.47 13.6 0.0000916 0.000264
#> # ℹ 1 more variable: p.adj.signif <chr>