0

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

ts22
  • 9
  • 1
  • https://stackoverflow.com/questions/3872070/how-to-force-r-to-use-a-specified-factor-level-as-reference-in-a-regression This might have been answered in this post. – Jan Brederecke May 20 '21 at 14:14

1 Answers1

0

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
dcarlson
  • 10,936
  • 2
  • 15
  • 18