This has been answered in the comments by @MYaseen208, but for the sake of completeness here is an answer with some detail.
While it is not specified in the documentation, TukeyHSD()
requires the aov
object to have been created with groups as factors (or characters, which are coerced to factors). You can further check if the aov
object has the correct types with e.g. str(data.aov$model)
.
# Generate some example data across three levels of `temp`
my_data = data.frame(yield = rnorm(n = 60), temp = c(100, 200, 300))
# Perform the ANOVA
data.aov = aov(yield ~ temp, data = my_data)
# Summary of AOV will output valid results
summary(data.aov)
#> Df Sum Sq Mean Sq F value Pr(>F)
#> temp 1 0.87 0.8700 1.107 0.297
#> Residuals 58 45.59 0.7861
# TukeyHSD requires categories as factors, note the error
TukeyHSD(data.aov)
#> Warning in replications(paste("~", xx), data = mf): non-factors ignored: temp
#> Error in TukeyHSD.aov(data.aov): no factors in the fitted model
# As shown in the comments, this can be done in-line
data.aov.factor = aov(yield ~ factor(temp), data = my_data)
# Same results as AOV without factors
summary(data.aov.factor)
#> Df Sum Sq Mean Sq F value Pr(>F)
#> factor(temp) 2 1.09 0.5440 0.683 0.509
#> Residuals 57 45.38 0.7961
# And with factors TukeyHSD will work as expected
TukeyHSD(data.aov.factor)
#> Tukey multiple comparisons of means
#> 95% family-wise confidence level
#>
#> Fit: aov(formula = yield ~ factor(temp), data = my_data)
#>
#> $`factor(temp)`
#> diff lwr upr p adj
#> 200-100 -0.27537565 -0.9543331 0.4035818 0.5948950
#> 300-100 -0.29495353 -0.9739110 0.3840039 0.5516274
#> 300-200 -0.01957788 -0.6985354 0.6593796 0.9973491
# Also note that type character will be used as factors, and will not throw an error if used
data.aov.char = aov(yield ~ as.character(temp), data = my_data)
TukeyHSD(data.aov.char)
#> Tukey multiple comparisons of means
#> 95% family-wise confidence level
#>
#> Fit: aov(formula = yield ~ as.character(temp), data = my_data)
#>
#> $`as.character(temp)`
#> diff lwr upr p adj
#> 200-100 -0.27537565 -0.9543331 0.4035818 0.5948950
#> 300-100 -0.29495353 -0.9739110 0.3840039 0.5516274
#> 300-200 -0.01957788 -0.6985354 0.6593796 0.9973491
Created on 2022-02-28 by the reprex package (v2.0.1)
temp
is not a factor. First converttemp
like thismy_data <- aov(yield~as.factor(temp), data=Pectin)
. – Caryloncaryn