11

我已成功上传数据,在我的数据集上运行 shapiro、bartletts 和单向方差分析,但是无论我尝试什么,我似乎都无法运行 TukeyHSD 而不收到一些错误消息,例如上面的错误消息,这是我输入的内容我错过了什么?

> my_data <- aov(yield~temp, data=Pectin)
> summary.aov(my_data)
            Df Sum Sq Mean Sq F value   Pr(>F)    
temp         1  63.90   63.90   24.67 0.000327 ***
Residuals   12  31.09    2.59                     

> TukeyHSD(Pectin)
Error in UseMethod("TukeyHSD") : 
  no applicable method for 'TukeyHSD' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
> TukeyHSD(my_data)
Error in TukeyHSD.aov(my_data) : no factors in the fitted model
In addition: Warning message:
In replications(paste("~", xx), data = mf) : non-factors ignored: temp
> TukeyHSD(summary.aov(my_data))
Error in UseMethod("TukeyHSD") : 
  no applicable method for 'TukeyHSD' applied to an object of class "c('summary.aov', 'listof')"

任何帮助都会很棒!

4

1 回答 1

1

@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)

于 2022-02-28T15:24:26.793 回答