# How to perform tukey HSD in base R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

First thing you must remember while moving on to post hoc analysis is the null hypothesis of the analysis of variance must be rejected, so that we can claim there exists a difference in the group means. Now, once we achieve that the tukey HSD can be performed simply by using TukeyHSD function in base R.

## Example

Consider the below data frame −

Live Demo

x1<-rep(LETTERS[1:4],5)
y1<-rep(c(5,2000,30,99),5)
df1<-data.frame(x1,y1)
df1

## Output

  x1 y1
1  A 5
2  B 2000
3  C 30
4  D 99
5  A 5
6  B 2000
7  C 30
8  D 99
9  A 5
10 B 2000
11 C 30
12 D 99
13 A 5
14 B 2000
15 C 30
16 D 99
17 A 5
18 B 2000
19 C 30
20 D 99

## Example

Performing analysis of variance −

ANOVA<-aov(y1~x1,data=df1)
summary(ANOVA)

## Output

Df Sum Sq Mean Sq F value Pr(>F)
x1 3 14361185 4787062 1.07e+32 <2e-16 ***
Residuals 16 0 0
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

## Example

Performing tukey HSD −

TukeyHSD(ANOVA)

## Output

Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = y1 ~ x1, data = df1)
$group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ... ## Example Live Demo head(PlantGrowth,20) ## Output  weight group 1 4.17 ctrl 2 5.58 ctrl 3 5.18 ctrl 4 6.11 ctrl 5 4.50 ctrl 6 4.61 ctrl 7 5.17 ctrl 8 4.53 ctrl 9 5.33 ctrl 10 5.14 ctrl 11 4.81 trt1 12 4.17 trt1 13 4.41 trt1 14 3.59 trt1 15 5.87 trt1 16 3.83 trt1 17 6.03 trt1 18 4.89 trt1 19 4.32 trt1 20 4.69 trt1 Performing analysis of variance − ## Example Live Demo ANOVA<-aov(weight~group,data=PlantGrowth) summary(ANOVA) ## Output Df Sum Sq Mean Sq F value Pr(>F) group 2 3.766 1.8832 4.846 0.0159 * Residuals 27 10.492 0.3886 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 ## Example Performing tukey HSD − TukeyHSD(ANOVA) ## Output Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = weight ~ group, data = PlantGrowth)$group
trt2-trt1 0.865 0.1737839 1.5562161 0.0120064