# How to Use the HSD.test Function in R

one-way ANOVA is used to determine whether or not there is a statistically significant difference between the means of three or more independent groups.

If the overall p-value from the ANOVA table is less than some significance level, then we have sufficient evidence to say that at least one of the means of the groups is different from the others.

In order to find out exactly which groups are different from each other, we must conduct a post hoc test.

One of the most commonly used post hoc tests is Tukey’s Test, which allows us to make pairwise comparisons between the means of each group while controlling for the family-wise error rate.

One of the easiest ways to perform Tukey’s Test in R is to use the HSD.test function from the agricolae function.

This function uses the following basic syntax:

HSD.test(y, trt, …)

where:

• y: The result of an ANOVA model fit using aov() in R
• trt: Vector treatment applied to each experimental unit

The following example shows how to use this function in practice.

## Example: How to Use the HSD.test Function in R

Suppose that we would like to perform a one-way ANOVA to determine if three different fertilizers lead to different levels of mean plant growth.

We will call these fertilizers A, B and C to make things simple.

We can use the following code to generate a dataset that contains the plant growth for 30 random plants that used each fertilizer:

```#make this example reproducible
set.seed(0)

#create data
data <- data.frame(group = rep(c("A", "B", "C"), each = 30),
values = c(runif(30, 0, 3),
runif(30, 0, 5),
runif(30, 1, 7)))

#view first six rows of data
head(data)

group     values
1     A  2.6900916
2     A  0.7965260
3     A  1.1163717
4     A  1.7185601
5     A  2.7246234
6     A  0.6050458
```

Next, we can use the aov() function to fit a one-way ANOVA model to this data:

```#fit one-way ANOVA model
model <- aov(values~group, data=data)

#view the model output
summary(model)

Df Sum Sq Mean Sq F value   Pr(>F)
group        2  98.93   49.46   30.83 7.55e-11 ***
Residuals   87 139.57    1.60
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1```

The overall p-value from the ANOVA table is 7.55e-11.

Since this is less than .05, we have sufficient evidence to say that the mean values across each group are not equal.

This means we can proceed to perform Tukey’s Test to determine exactly which group means are different.

The following code shows how to use the HSD.test() function to perform Tukey’s Test:

```library(agricolae)

#perform Tukey's Test
HSD.test(model, "group", console=TRUE)

Study: model ~ "group"

HSD Test for values

Mean Square Error:  1.604223

group,  means

values       std  r      Min      Max
A 1.584292 0.9050638 30 0.040171 2.975718
B 2.562033 1.2385145 30 0.116656 4.306047
C 4.129694 1.5683145 30 1.505481 6.763708

Alpha: 0.05 ; DF Error: 87
Critical Value of Studentized Range: 3.372163

Minimun Significant Difference: 0.7797948

Treatments with the same letter are not significantly different.

values groups
C 4.129694      a
B 2.562033      b
A 1.584292      c
```

The output at the very end tells us that Treatments with the same letter are not significantly different.

Notice that none of the treatments have the same letter, which tells us that there is a statistically significant difference between the mean plant growth resulting from each fertilizer at the 0.05 significance level.

## Additional Resources

The following tutorials explain how to perform other common tasks in R: