Tukey's Test not accounting for factor levels?

Hello everyone,

I am new to R 3.5.1 and I am trying to run a Tukey's HSD post-hoc test on a significant 2x2 ANOVA. However, the Tukey's test is not performing the pairwise comparisons. Instead, it is showing the main effects like the ANOVA. I tried setting the level of my factors in a few different ways, and I have verified that R is recognizing them. I'm not quite sure what I am doing wrong here. I thank anyone for their advice!

mpxd = my data set
Sex = factor with two levels
Con = factor with two levels
Hypo1R = DV

#set levels of factors
Sex <- factor(Sex)
levels(Sex) <- c("Male", "Female")
Con <- factor(Con)
levels(Con) <- c("Saline", "LPS")

#2x2 ANOVA, compare dependant variable (DV ~ IV1 + IV2, create variable = data)
ANOVA_Hypo <- aov(Hypo1R ~ Sex + Con, data = mpxd)

#display results
summary(ANOVA_Hypo)

#Tukeys test
TukeyHSD(ANOVA_Hypo)

I am getting this for my output:

> str(Sex)
> #set levels of factors
> Sex <- factor(Sex)
> levels(Sex) <- c("Male", "Female")
> Con <- factor(Con)
> levels(Con) <- c("Saline", "LPS")
 Factor w/ 2 levels "Male","Female": 2 2 2 2 2 2 2 2 2 2 ..
> str(Con)
 Factor w/ 2 levels "Saline","LPS": 2 2 2 2 2 2 2 2 2 2 ...

> summary(ANOVA_Hypo)
            Df Sum Sq Mean Sq F value Pr(>F)  
Sex          1  0.344   0.344   1.068 0.3080  
Con          1  2.276   2.276   7.069 0.0115 *
Residuals   37 11.912   0.322       

> TukeyHSD(ANOVA_Hypo)
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = Hypo1R ~ Sex + Con, data = mpxd)

$`Sex`
                diff        lwr       upr     p adj
Male-Female 0.185473 -0.1780868 0.5490329 0.3079957

$Con
                diff       lwr     upr     p adj
Saline-LPS 0.4770601 0.1135002 0.84062 0.0115189

I tried the pairwise.t.test option as well, but I am getting an error:

> pairwise.t.test(mpxd$Sex, mpxd$Con, p.adj = 'none')

	Pairwise comparisons using t tests with pooled SD 

data:  mpxd$Sex and mpxd$Con 

       LPS
Saline -  

P value adjustment method: none 
Warning messages:
1: In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA
2: In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA
3: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
  NAs introduced by coercion
4: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
  NAs introduced by coercion

I don't have much experience with Tukey's HSD test specifically, but here's what I think is happening: In your model, there's no interaction between Con and Sex. As a result, the predicted effect of "Saline" vs. "LSP" (the two levels of Con) is the same (because the model requires it to be the same) for each level of Sex and there is therefore nothing to test.

If you fit a model where Con and Sex interact (aov(Hypo1R ~ Sex*Con, data = mpxd); note * instead of +), then the predicted effect of Con will (in general) be different for each level of Sex and then it would make sense to test for a difference by Sex. In this case, TukeyHSD will return tests for Con and Sex separately, and also for each combination of Con and Sex (the interaction).

1 Like

Good thinking! It worked.

Thanks a lot.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.