ranovaposthoctukeyfactoring

Tukey HSD for categorical and continuous variables in R


I want to do a post-hoc test for a significant ANOVA I've done successfully.

I have 5 conditions (target_onset) across which I want to compare reaction times (key_resp.rt) in a df called data_clean. target_onset and key_resp.rt are columns.

This is how I did the ANOVA, which worked fine:

cond.aov <- aov(data_clean$target_onset ~ data_clean$key_resp.rt)
summary(cond.aov)

Next, I want to see what a post-hoc test says to find out which differences between the 5 conditions are significant.

I know that TukeyHSD only takes factors. So I factorized my columns of interest:

data_clean$target_onset <- factor(data_clean$target_onset)
data_clean$key_resp.rt <- factor(data_clean$key_resp.rt)

TukeyHSD(aov(data_clean$target_onset ~ data_clean$key_resp.rt))

However, when I run this code, I get the following error:

Error in class(y) <- oldClass(x) : adding class "factor" to an invalid object In addition: Warning messages: 1: In model.response(mf, "numeric") : using type = "numeric" with a factor response will be ignored 2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors

Any suggestions would be helpful. Thanks in advance.


Solution

  • EDIT first time through I missed the fact you had the formula backwards as well!

    You need to make target_onset a factor before issuing the aov function. You do not want to make key_resp.rt a factor at all.

    So the sequence should be...

    data_clean$target_onset <- factor(data_clean$target_onset)
    
    cond.aov <- aov(key_resp.rt ~ target_onset, data = data_clean)
    
    summary(cond.aov)
    
    TukeyHSD(cond.aov)
    

    The dependent variable (the response time goes on the left of the tilde and the independent grouping variable to the right.

    If you don't make the condition/grouping variable a factor aov which actually do an lm using the numbers you have in the grouping column you can see that reflected in the degrees of freedom for the cond.aov.

    As long as you already have an aov object might as well make the call to TukeyHSD as simple as possible