How to Perform Tukey HSD Test in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Tukey HSD Test in R, When there are three or more independent groups, we apply a one-way ANOVA to see if there is a significant difference.

The p-value for one-way ANOVA is less than 0.05 indicate that at least one of the treatment groups differs from the others.

One way ANOVA tells us whether the means of the groups are significantly different or not, but we still need to do a post hoc multiple comparison test to dig further.

One of the widely used methods is the Tukey HSD test.

How to do data reshape in R? » Data Reshaping »

What is special with Tukey HSD?

It is a multiple range test similar to the LSD test except that Tukey utilized the honestly significant difference (HSD) test or the w-procedure.

Any two treatments that mean having a difference more than honestly significant difference are said to be significantly different, otherwise not.

Tukey HSD Test in R

The Tukey HSD test allows for all possible pairwise comparisons while keeping the family-wise error rate low.

Step 1: ANOVA Model

For the difference identification, establish a data frame with three independent groups and fit a one-way ANOVA model.

data <- data.frame(group = rep(c("P1", "P2", "P3"), each = 40),
values = c(rnorm(40, 0, 3),rnorm (40, 0, 6),rnorm (40, 1, 5)))

one-way ANOVA model

model <- aov(values~group, data=data)
Df Sum Sq Mean Sq F value   Pr(>F)   
group         2  400.7  200.35     9.9 0.000107 ***
Residuals   117 2367.8   20.24                    
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’

The observed p-value from the ANOVA table is less than 0.05, indicating that there is enough evidence to conclude that the group means are not equal.

How to read a stock chart pattern Chart Reading Technique

Step 2: Perform Tukey HSD Test.

TukeyHSD(model, conf.level=.95)
Tukey multiple comparisons of means
    95% family-wise confidence level
Fit: aov(formula = values ~ group, data = data)
           diff        lwr      upr     p adj
P2-P1 -1.524566 -3.9125188 0.863387 0.2873372
P3-P1  2.882295  0.4943425 5.270248 0.0135957
P3-P2  4.406861  2.0189084 6.794814 0.0000765

P3 vs P1 and P3 vs P2 are significantly different at the 95 percent confidence level, according to the Tukey HSD test.

Step 3: Visualization

TukeyHSD() function allows us to visualize the confidence intervals

plot(TukeyHSD(model, conf.level=.95), las = 2)

Correlation Analysis in R? » Karl Pearson correlation coefficient »

Subscribe to the Newsletter and COMMENT below!

The post How to Perform Tukey HSD Test in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)