How to change the order of samples in a Tukey test in R?

Problem : I would like to learn how to change the order of samples for which the Tukey test in R calculates the means and assigns the corresponding letters. The following is a simple example.

I played with aperture data and found that there are differences in Sepal.Length among different species. Here is the box:

enter image description here

I ran an ANOVA test and found that the differences were statistically significant.

> fit <- lm(Sepal.Length ~ Species, data = iris)
> summary(aov(fit))

             Df Sum Sq Mean Sq F value Pr(>F)    
Species       2  63.21  31.606   119.3 <2e-16 ***
Residuals   147  38.96   0.265                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Then I did a Tukey test and got the following:

> library(agricolae)
> HSD.test(fit, "Species", group=T, console=T)

Study: fit ~ "Species"

HSD Test for Sepal.Length 

Mean Square Error:  0.2650082 

Species,  means

           Sepal.Length       std  r Min Max
setosa            5.006 0.3524897 50 4.3 5.8
versicolor        5.936 0.5161711 50 4.9 7.0
virginica         6.588 0.6358796 50 4.9 7.9

alpha: 0.05 ; Df Error: 147 
Critical Value of Studentized Range: 3.348424 

Honestly Significant Difference: 0.2437727 

Means with the same letter are not significantly different.

Groups, Treatments and means
a    virginica       6.588 
b    versicolor      5.936 
c    setosa          5.006

According to the group table, the HSD.test function sorts the funds in descending order, and then assigns the letters. Thus, "virginica" has the highest average value, so it is the first in the table.

: ? , . :

a setosa     5.006
b versicolor 5.936
c virginica  6.588

. multcomp , :

1 - glht Tukey

> an <- aov(fit)
> library(multcomp)
> glht(an, linfct = mcp(Species = "Tukey"))

         General Linear Hypotheses

    Multiple Comparisons of Means: Tukey Contrasts


    Linear Hypotheses:
                                Estimate
    versicolor - setosa == 0       0.930
    virginica - setosa == 0        1.582
    virginica - versicolor == 0    0.652

2 - cld , Species iris$Species

> cld(glht(an, linfct = mcp(Species = "Tukey")))
    setosa versicolor  virginica 
       "a"        "b"        "c" 

, glht , (, std, p-). , HSD.test, cld. HSD.test .

+4
2

. . , - -.

multcompLetters(), , TukeyHSD(). .

, , , HSD.test, , . .

library(agricolae)
reorder<-function(inV){
  collapsed <- paste(inV,sep="",collapse = "")
  u <- unique(strsplit(collapsed,"")[[1]])
  if(length(u)<2){
    return(inV)
  }
  u <- u[order(u)]
  m <- matrix(nrow=NROW(inV),ncol=length(u))
  m[]<-F
  for(i in 1:length(inV)){
    s <- strsplit(inV[i],"")[[1]]
    index <- match(s,u)
    m[i,index] <- T
  }
  for(i in 1:(length(u)-1)){
    firstColT <- match(T,m[,i])[1] #first row with true in current column
    firstT <- match(T,rowSums(m[,i:length(u)] > 0))[1] #first row with true in rest
    if(firstT < firstColT){
      colT <- match(T,m[firstT,i:length(u)])[1]
      colT <- colT + i - 1 #correct index for leftout columns in match
      tmp <- m[,colT]
      m[,colT] <- m[,i]
      m[,i] <- tmp
    }
  }
  res <- vector(mode = "character", length=length(trt))
  for(i in 1:length(inV)){
    l <- u[m[i,]]
    res[i] <- paste(l,sep="",collapse = "")
  }
  return(res)
}

fit <- lm(Sepal.Length ~ Species, data = iris)
a <- HSD.test(fit, "Species", group=T, console=F)$groups
a <- a[rev(rownames(a)),] #order the result the way you want
a$M <- reorder(as.character(a$M))

, .

0

, . , . ,

res <- vector(mode = "character", length=length(trt)), 

res <- vector(mode = "character", length=length("trt")) 
-1

Source: https://habr.com/ru/post/1584556/


All Articles