Wrong Tukey-letter ordering in R multcompView package

1.2k views Asked by At

I do not understand letter ordering in the function multcompletters from multcompView. According to documentation it should be according to mean of the group. In the following example, the middle group got c (from abc) and should have got b. Is this a bug?

require(multcompView)
# Data 
datacol <- c(21.1,20.2,21.8,20.9,23.3,21.1,20.2,21.8,20.9,23.3,19.8,16.4,
16.9,16.0,17.6,17.5,16.9,13.3,18.0,17.6,13.5,12.2,15.2,15.1,15.2,14.0)

# Group 
faccol <- c(rep(c(1,2),each=10),rep(3,6))

# Combined Dataframe 
tukeyset <- data.frame(datacol,as.factor(faccol))
colnames(tukeyset)[2] <- "faccol"

# Tukeytest 
tukeyres <- TukeyHSD(x=aov(lm(datacol~faccol,data=tukeyset)))
Tlevels <- tukeyres$faccol[,4]
multcompLetters(Tlevels) # WRONG ORDER, even reversed 
# Boxplot 
boxplot(tukeyset$datacol~tukeyset$faccol)
# adding the labels 
text(x=c(1,2,3),y=c(aggregate(data=tukeyset,datacol~faccol,mean)$datacol),
     labels=as.character(multcompLetters(Tlevels,reversed=TRUE)$Letters)[order(names(multcompLetters(Tlevels,reversed=TRUE)['Letters']$Letters))])
1

There are 1 answers

0
Wang qiqi On

Oh no just encountered this problem! Spent 2 hours finally sorting it out!

You cannot call multcompLetters. It will give you the completely wrong order.

You have to use multcompLetters2, multcompLetters3 or multcompLetters4.

Another very important point is you have to convert your input dataset into a dataframe, but not tibble! Tibble doesn't work for this.