首页 文章

如何在R中的Tukey测试中更改样本的顺序?

提问于
浏览
4

Problem :我想学习如何更改Tukey在R中计算的样本的样本顺序并指定相应的字母 . 非常简单的例子如下 .

我玩虹膜数据,发现不同物种之间的Sepal.Length存在差异 . 这是盒子图:

enter image description here

我进行了ANOVA测试,发现差异具有统计学意义 .

> 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

然后我进行了Tukey的测试并获得了以下内容:

> 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

根据组表,HSD.test函数按降序排序,然后分配字母 . 因此,“virginica”具有最大的平均值,因此它是表中的第一个 .

Questions :有没有办法更改默认排序和分配字母?我可以按平均值的升序对样本进行排序,然后分配字母 . 预期的输出如下:

a setosa     5.006
b versicolor 5.936
c virginica  6.588

Possible solution :在包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 可以根据因素 iris$Species 为我提供分配给 Species 的字母

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

不幸的是, glht 函数没有显示另一个数据,这些数据对于创建条形图(平均值,标准差,p值)非常有用 . 当然,我可以使用其他特殊功能单独执行,或者只使用 HSD.testcld . 但我更倾向于解决 HSD.test 函数中的均值排序问题并仅使用此问题 .

2 回答

  • 0

    我注意到回答这个问题有点晚了 . 但是我遇到了完全相同的问题,喜欢分享我的解决方案作为未来的参考 . 希望有一天能有所帮助 .

    第一个选项

    例如,可以使用 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))
    

    例如,这有点过分,但它也适用于更复杂的情况 .

  • -1

    首先,感谢功能 . 这就是我在寻找的东西 . 但我认为有一个错误

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

    它应该是

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

相关问题