Search code examples
rpackageanovatukey

R : TUKEY following one-way ANOVA


I conducted a one-way ANOVA in R, but I keep getting error messages when I attempt to do a Tukey post-hoc to see which treatments differ from each other. (I would like the results to be ranked (a, ab, b, bcd...etc.)

DATA details:

data = "abh2"

x = 6 treatments : "treatment"

y= moisture readings "moist" (n=63 per treatment, total=378)

I ran a one-way ANOVA:

anov <- anova(lm(moist~treatment, data=abh2))

.# RESULTS indicate I can move to a post hoc (p<0.05):

Analysis of Variance Table

Response: moist
           Df Sum Sq Mean Sq F value    Pr(>F)    
treatment   5 1706.3  341.27  25.911 < 2.2e-16 ***

I chose Tukey HSD and tried to run it with 2 methods, but get error messages for both:

Built-in R function:

TukeyHSD(anov)
# ERROR : no applicable method for 'TukeyHSD' applied to an object of class "c('anova', 'data.frame')"

Agricolae package:

    HSD.test(anov, "treatment", group=TRUE, console=TRUE)
    # ERROR : Error in HSD.test(anov, "treatment", group = TRUE, console = TRUE) :
argument "MSerror" is missing, with no default

I found the MSerror was

1) An "# Old version HSD.test()" (But I've just updated the agricolae package)

2) MSerror<-deviance(model)/df

So I tried:

HSD.test(anov, "treatment", MSerror=deviance(moist)/5, group=TRUE, console=TRUE)
 *but still* # ERROR: $ operator is invalid for atomic vectors

Could anyone help me move ahead from here? It seems like a pretty simple problem but I've spent hours on this!

Many thanks :)


Solution

  • Thanks for the feed back Annie-Claude, it got me on the right track that R wasn't recognizing the data as it should.

    I solved the problem using this code though:

    library(agricolae)
    
    model<-aov(moist~treatment, data=abh2)
    
    out <- HSD.test(model,"treatment", group=TRUE,console=TRUE)
    

    It appears that the ANOVA command that I was initially using (from R base package), was simply not understood by the Tukey test I was trying to perform afterwards (with the agricolae package).

    Take-home message for me: Try to conduct a related string of analyses in the same package!

    p.s. To obtain the p-values:

    summary(model)
    

    they can be converted to a dataframe like so:

    as.data.frame( summary(model)[[1]] )