Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in TukeyHSD.aov(my_data) : no factors in the fitted model

Tags:

r

tukey

I have successfully uploaded data, run shapiro, bartletts and one way anova on my data set however no matter what I try I can't seem to run TukeyHSD without getting some error message such as the one above this is what I have inputted is there something I am missing??

> 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')"

Any help would be great!!

like image 798
Flo27 Avatar asked Feb 16 '18 18:02

Flo27


1 Answers

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)

like image 90
mhovd Avatar answered Oct 18 '22 07:10

mhovd