Post-hoc test in R

A significant one-way ANOVA means that at least one of the group means is different from the other ones. But which ones are different?.

To find this out, you need to run what is called a post-hoc test, which basically compares all possible pair-wise comparisons to find out, which ones are different.

In R, we can run a post-hoc test with the R function: tukey_hsd() [from the packgae rstatix].

Lets try:

library(tidyverse)
library(ggpubr)
library(rstatix) # we load this libraries, which we use to sumamrise the results

#We start by putting the data in a dataframe, which two columns. one for the variety type, and the other for the number of tomatoes 
Data=data.frame(Variety=c(rep("VarA",3),rep("VarB",3),rep("VarC",3)), NumTomatoes=c(1,2,3, 3,4,5, 5,6,7))

pwc <- Data %>% tukey_hsd(NumTomatoes ~ Variety) #PairWise Comparisons
pwc
## # 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 Variety VarA   VarB            0        2   -0.505      4.51 0.109  
## 2 Variety VarA   VarC            0        4    1.49       6.51 0.00649
## 3 Variety VarB   VarC            0        2   -0.505      4.51 0.109  
## # ℹ 1 more variable: p.adj.signif <chr>

From that comparison, you can tell that there are significant differences between Variety A and Variety C only. Cool, ah?.

By looking at the plot, one could have concluded that they are all different. But if you look at the actual data, you will see that variety B, have data in common to varieties A and C, which given the small sample size, led to those pairs to be similar.