I've just run an ANOVA (aov) in r between 3 groups. Group 1,2,3.
After running TukeyHSD for my model, my comparisons are compared in the order of groups:
2-1, 3-1, 3-2
can this be changed so is as follows: 1-2, 1-3, 2-3
Thanks
I've just run an ANOVA (aov) in r between 3 groups. Group 1,2,3.
After running TukeyHSD for my model, my comparisons are compared in the order of groups:
2-1, 3-1, 3-2
can this be changed so is as follows: 1-2, 1-3, 2-3
Thanks
Using relevel
will not work since you don't want to change the level order, just the labels. First we need some reproducible data:
data(iris)
SL <- iris$Sepal.Length
Sp <- as.factor(as.numeric(iris$Species))
iris.aov <- aov(SL~Sp)
iris.mc <- TukeyHSD(iris.aov)
iris.mc
# Tukey multiple comparisons of means
# 95% family-wise confidence level
#
# Fit: aov(formula = SL ~ Sp)
#
# $Sp
# diff lwr upr p adj
# 2-1 0.930 0.6862273 1.1737727 0
# 3-1 1.582 1.3382273 1.8257727 0
# 3-2 0.652 0.4082273 0.8957727 0
Now to switch the labels we use expand.grid
to create ones with the first and second group id switched:
ngroups <- 3
Grps <- expand.grid(seq(ngroups), seq(ngroups))
Grps <- Grps[Grps$Var1 < Grps$Var2,] # Unique groups
newlbls <- unname(apply(Grps, 1, paste0, collapse="-"))
dimnames(iris.mc$Sp)[[1]] <- newlbls
iris.mc
# Tukey multiple comparisons of means
# 95% family-wise confidence level
#
# Fit: aov(formula = SL ~ Sp)
#
# $Sp
# diff lwr upr p adj
# 1-2 0.930 0.6862273 1.1737727 0
# 1-3 1.582 1.3382273 1.8257727 0
# 2-3 0.652 0.4082273 0.8957727 0