【问题标题】:Post-hoc tests for one-way ANOVA with Welch's correction in R在 R 中使用 Welch 校正的单向 ANOVA 的事后检验
【发布时间】:2015-04-19 16:24:08
【问题描述】:

我在 R 中使用 oneway.test() 进行了单向 ANOVA 检验和韦尔奇校正,因为我的数据违反了等方差假设(转换没有解决问题)。

一个简单的数据示例:

> dput(df)
structure(list(Count = c(13, 14, 14, 12, 11, 13, 14, 15, 13, 
12, 20, 15, 9, 5, 13, 14, 7, 17, 18, 14, 12, 12, 13, 14, 11, 
10, 15, 14, 14, 13), Group = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a", "b", "c"
), class = "factor")), .Names = c("Count", "Group"), row.names = c(NA, 
-30L), class = "data.frame")

library(car) 
grp = as.factor(c(rep(1, 10), rep(2, 10),rep(3, 10)))
leveneTest(df$Count,grp) #unequal variances

#one-way ANOVA with welch's correction
oneway.test(Count ~ Group, data=df, na.action=na.omit, var.equal=FALSE)

我有多个组,所以我现在想运行成对的事后测试。无论如何,是否可以使用 oneway.test() 函数中的对象来执行此操作?如果不是,如何对方差不等的组进行成对测试?我一直无法在网上找到这个问题的答案。任何建议将不胜感激。

【问题讨论】:

  • 你能发一个df的例子吗?
  • 是的,抱歉 - 我提供了另一种格式的数据
  • 在本例中,您希望对仅包括(例如)ab 组的df 子集执行oneway.test

标签: r anova posthoc


【解决方案1】:

补充一点,尽管时机不好,而且我自己一直在寻找类似的东西,但也可以选择执行 Games-Howell 测试。 正如stackexchange_post 中介绍的那样,这甚至已包含在“userfriendlyscience”R 包中的“posthoc.tgh”函数下。它代表了对不等方差的 Tukey-Kramer 检验的扩展。 posthocTGH {userfriendlyscience}

原始出版物(甚至在我出生之前): Paul A. Games 和 John F. Howell。 具有不等 N 和/或方差的成对多重比较过程:蒙特卡洛研究。 教育与行为统计杂志,第 1 卷,第 2 期,1976 年,第 113-125 页。 doi: 10.3102/10769986001002113

【讨论】:

  • 感谢您提供的踏脚石,感谢布鲁塞尔的@Matherion 编辑并分发了该函数。
【解决方案2】:

这里有两种方法:

数据

library(car) 
df <- structure(list(Count = c(13, 14, 14, 12, 11, 13, 14, 15, 13, 12, 20, 15, 9, 5, 13, 14, 7, 17, 18, 14, 12, 12, 13, 14, 11, 10, 15, 14, 14, 13),
                     Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a", "b", "c" ), class = "factor")),
                .Names = c("Count", "Group"),
                row.names = c(NA, -30L), class = "data.frame")

基础 R

首先,Group 因子的唯一对集合:

allPairs <- expand.grid(levels(df$Group), levels(df$Group))
## http://stackoverflow.com/questions/28574006/unique-combination-of-two-columns-in-r/28574136#28574136
allPairs <- unique(t(apply(allPairs, 1, sort)))
allPairs <- allPairs[ allPairs[,1] != allPairs[,2], ]
allPairs
##      [,1] [,2]
## [1,] "a"  "b" 
## [2,] "a"  "c" 
## [3,] "b"  "c" 

现在分析:

allResults <- apply(allPairs, 1, function(p) {
    dat <- df[ df$Group %in% p, ]
    ret <- oneway.test(Count ~ Group, data = dat, na.action = na.omit, var.equal = FALSE)
    ret$groups <- p
    ret
})
length(allResults)
## [1] 3
allResults[[1]]
##  One-way analysis of means (not assuming equal variances)
## data:  Count and Group
## F = 0.004, num df = 1.000, denom df = 10.093, p-value = 0.9508

如果你想要这是一个矩阵,也许是这样:

mm <- diag(length(levels(df$Group)))
dimnames(mm) <- list(levels(df$Group), levels(df$Group))
pMatrix <- lapply(allResults, function(res) {
    ## not fond of out-of-scope assignment ...
    mm[res$groups[1], res$groups[2]] <<- mm[res$groups[2], res$groups[1]] <<- res$p.value
})
mm
##           a         b         c
## a 1.0000000 0.9507513 0.6342116
## b 0.9507513 1.0000000 0.8084057
## c 0.6342116 0.8084057 1.0000000

(对于 F 统计量,这可以很容易地完成。)

使用dplyr

首先,Group 因子的唯一对集合:

library(dplyr)
## http://stackoverflow.com/questions/28574006/unique-combination-of-two-columns-in-r/28574136#28574136
allPairs <- expand.grid(levels(df$Group), levels(df$Group), stringsAsFactors = FALSE)  %>%
    filter(Var1 != Var2) %>%
    mutate(key = paste0(pmin(Var1, Var2), pmax(Var1, Var2), sep='')) %>%
    distinct(key) %>%
    select(-key)
allPairs
##   Var1 Var2
## 1    b    a
## 2    c    a
## 3    c    b

如果顺序真的很重要,您可以尽早将dplyr::arrange(Var1, Var2) 添加到此管道中,也许在调用expand.grid 之后。

现在分析:

ret <- allPairs %>%
    rowwise() %>%
    do({
        data.frame(.,
                   oneway.test(Count ~ Group, filter(df, Group %in% c(.$Var1, .$Var2)),
                               na.action = na.omit, var.equal = FALSE)[c('statistic', 'p.value')],
                   stringsAsFactors = FALSE)
    })

ret
## Source: local data frame [3 x 4]
## Groups: <by row>
##   Var1 Var2   statistic   p.value
## 1    b    a 0.004008909 0.9507513
## 2    c    a 0.234782609 0.6342116
## 3    c    b 0.061749571 0.8084057

(我没有对其中任何一个的性能做出任何声明;通常一个会像这个例子那样用很少的数据表现出色,但另一个会以更大的数据集领先。它们似乎都执行相同的统计对具有相同结果的明智比较。交给你!)

【讨论】:

  • 太棒了!谢谢你。真的很有帮助。我最初一直在寻找一种单向 ANOVA 'tukey's post-hoc' 类型,它等效于具有不等方差的数据,但我从你的回答中猜测,没有简单的函数可以对带有 welch 校正的 ANOVA 执行此操作?成对运行 oneway.tests 是一个很好的解决方案。再次感谢。
  • 我刚刚注意到您的代码的“ret”部分似乎不起作用(错误:找不到函数“%>%”)。谢谢。
  • %&gt;%dplyr 中。最后一个带有ret 的块依赖于前一个块的完成,它不是独立的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多