R 中 Welch 校正的单向方差分析的事后检验 [英] Post-hoc tests for one-way ANOVA with Welch's correction in R

查看:128
本文介绍了R 中 Welch 校正的单向方差分析的事后检验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中使用 oneway.test() 运行了带有韦尔奇校正的单向方差分析测试,因为我的数据违反了等方差假设(转换没有解决问题).

I have run a one-way ANOVA test with welch's correction using oneway.test() in R, as I have data that violate the assumption of equal variance (transformations did not solve the problem).

一个简单的数据示例:

> 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() 函数中的对象来做到这一点?如果不是,如何对方差不等的组进行成对测试?我无法在网上找到这个问题的答案.任何建议将不胜感激.

I have multiple groups so I would now like to run pairwise post-hoc tests. Is there anyway to do this with an object from the oneway.test() function? If not, how would one go about running pair-wise tests on groups with unequal variances? I have not been able to find an answer to this question online. Any advice would be appreciated.

推荐答案

这里有两种方法:

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

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

If you want this is a matrix, perhaps this:

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 统计量,这可以很容易地完成.)

(This can be done just as easily for the F-statistic.)

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

First, the set of unique pairs of the Group factor:

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 之后.

If the order really matters, you can add dplyr::arrange(Var1, Var2) early into this pipeline, perhaps after the call to 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

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

(I'm making no claims as to the performance of either of these; often one will shine with few data like this example, but the other will come out ahead with larger sets. They both appear to perform the same statistical pair-wise comparisons with the same results. Over to you!)

这篇关于R 中 Welch 校正的单向方差分析的事后检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆