@MYaseen208 在评论中已经回答了这个问题,但为了完整起见,这里给出了一些详细的答案。
虽然文档中没有指定,但TukeyHSD()
需要aov
创建对象时使用组作为因子(或字符,它们被强制为因子)。您可以使用 eg 进一步检查aov
对象是否具有正确的类型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
由reprex 包于 2022-02-28 创建(v2.0.1)