How to perform tukey HSD in base R?

R ProgrammingServer Side ProgrammingProgramming

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)
$x1
diff lwr upr p adj
B-A 1995 1995 1995 0
C-A 25 25 25 0
D-A 94 94 94 0
C-B -1970 -1970 -1970 0
D-B -1901 -1901 -1901 0
D-C 69 69 69 0

Example

Consider the PlantGrowth data in base R −

 Live Demo

str(PlantGrowth)

Output

'data.frame': 30 obs. of 2 variables:
$ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
$ 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
diff lwr upr p adj
trt1-ctrl -0.371 -1.0622161 0.3202161 0.3908711
trt2-ctrl 0.494 -0.1972161 1.1852161 0.1979960
trt2-trt1 0.865 0.1737839 1.5562161 0.0120064
raja
Published on 08-Dec-2020 07:11:32
Advertisements