Provides a pipe-friendly framework to performs Tukey post-hoc
tests. Wrapper around the function TukeyHSD(). It is
essentially a t-test that corrects for multiple testing.
Can handle different inputs formats: aov, lm, formula.
tukey_hsd(x, ...)
# Default S3 method
tukey_hsd(x, ...)
# S3 method for class 'lm'
tukey_hsd(x, ...)
# S3 method for class 'data.frame'
tukey_hsd(x, formula, ...)an object of class aov, lm or data.frame
containing the variables used in the formula.
other arguments passed to the function
TukeyHSD(). These include:
which: A character vector listing terms in the fitted model for which the intervals should be calculated. Defaults to all the terms.
ordered: A logical value indicating if the levels of the factor should be ordered according to increasing average in the sample before taking differences. If ordered is true then the calculated differences in the means will all be positive. The significant differences will be those for which the lwr end point is positive.
a formula of the form x ~ group where x is a
numeric variable giving the data values and group is a factor with
one or multiple levels giving the corresponding groups. For example,
formula = TP53 ~ cancer_group.
a data.frame containing the variables in the formula.
a tibble data frame containing the results of the different comparisons.
# Data preparation
df <- ToothGrowth
df$dose <- as.factor(df$dose)
# Tukey HSD from ANOVA results
aov(len ~ dose, data = df) %>% tukey_hsd()
#> # A tibble: 3 × 9
#> term group1 group2 null.value estimate conf.low conf.high p.adj
#> * <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 dose 0.5 1 0 9.13 5.90 12.4 2 e- 8
#> 2 dose 0.5 2 0 15.5 12.3 18.7 1.12e-11
#> 3 dose 1 2 0 6.36 3.14 9.59 4.25e- 5
#> # ℹ 1 more variable: p.adj.signif <chr>
# two-way anova with interaction
aov(len ~ dose*supp, data = df) %>% tukey_hsd()
#> # A tibble: 19 × 9
#> term group1 group2 null.value estimate conf.low conf.high p.adj
#> * <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 dose 0.5 1 0 9.13 6.36 11.9 3.55e-10
#> 2 dose 0.5 2 0 15.5 12.7 18.3 4.38e-13
#> 3 dose 1 2 0 6.36 3.60 9.13 2.71e- 6
#> 4 supp OJ VC 0 -3.70 -5.58 -1.82 2.31e- 4
#> 5 dose:supp 0.5:OJ 1:OJ 0 9.47 4.67 14.3 4.61e- 6
#> 6 dose:supp 0.5:OJ 2:OJ 0 12.8 8.03 17.6 2.13e- 9
#> 7 dose:supp 0.5:OJ 0.5:VC 0 -5.25 -10.0 -0.452 2.43e- 2
#> 8 dose:supp 0.5:OJ 1:VC 0 3.54 -1.26 8.34 2.64e- 1
#> 9 dose:supp 0.5:OJ 2:VC 0 12.9 8.11 17.7 1.77e- 9
#> 10 dose:supp 1:OJ 2:OJ 0 3.36 -1.44 8.16 3.19e- 1
#> 11 dose:supp 1:OJ 0.5:VC 0 -14.7 -19.5 -9.92 2.99e-11
#> 12 dose:supp 1:OJ 1:VC 0 -5.93 -10.7 -1.13 7.39e- 3
#> 13 dose:supp 1:OJ 2:VC 0 3.44 -1.36 8.24 2.94e- 1
#> 14 dose:supp 2:OJ 0.5:VC 0 -18.1 -22.9 -13.3 4.86e-13
#> 15 dose:supp 2:OJ 1:VC 0 -9.29 -14.1 -4.49 6.91e- 6
#> 16 dose:supp 2:OJ 2:VC 0 0.0800 -4.72 4.88 1 e+ 0
#> 17 dose:supp 0.5:VC 1:VC 0 8.79 3.99 13.6 2.10e- 5
#> 18 dose:supp 0.5:VC 2:VC 0 18.2 13.4 23.0 4.82e-13
#> 19 dose:supp 1:VC 2:VC 0 9.37 4.57 14.2 5.77e- 6
#> # ℹ 1 more variable: p.adj.signif <chr>
# Tukey HSD from lm() results
lm(len ~ dose, data = df) %>% tukey_hsd()
#> # A tibble: 3 × 9
#> term group1 group2 null.value estimate conf.low conf.high p.adj
#> * <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 dose 0.5 1 0 9.13 5.90 12.4 2 e- 8
#> 2 dose 0.5 2 0 15.5 12.3 18.7 1.12e-11
#> 3 dose 1 2 0 6.36 3.14 9.59 4.25e- 5
#> # ℹ 1 more variable: p.adj.signif <chr>
# Tukey HSD from data frame and formula
tukey_hsd(df, len ~ dose)
#> # A tibble: 3 × 9
#> term group1 group2 null.value estimate conf.low conf.high p.adj
#> * <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 dose 0.5 1 0 9.13 5.90 12.4 2 e- 8
#> 2 dose 0.5 2 0 15.5 12.3 18.7 1.12e-11
#> 3 dose 1 2 0 6.36 3.14 9.59 4.25e- 5
#> # ℹ 1 more variable: p.adj.signif <chr>
# Tukey HSD using grouped data
df %>%
group_by(supp) %>%
tukey_hsd(len ~ dose)
#> # A tibble: 6 × 10
#> supp term group1 group2 null.value estimate conf.low conf.high p.adj
#> * <fct> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 OJ dose 0.5 1 0 9.47 5.31 13.6 1.58e- 5
#> 2 OJ dose 0.5 2 0 12.8 8.67 17.0 9.39e- 8
#> 3 OJ dose 1 2 0 3.36 -0.800 7.52 1.31e- 1
#> 4 VC dose 0.5 1 0 8.79 4.90 12.7 1.75e- 5
#> 5 VC dose 0.5 2 0 18.2 14.3 22.0 1.66e-11
#> 6 VC dose 1 2 0 9.37 5.48 13.3 6.61e- 6
#> # ℹ 1 more variable: p.adj.signif <chr>