简体   繁体   English

使用Welch在R中校正的单向ANOVA的事后检验

[英]Post-hoc tests for one-way ANOVA with Welch's correction in R

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). 我使用了R中的oneway.test()进行了单向ANOVA测试和welch校正,因为我的数据违反了等方差的假设(转换没有解决问题)。

A simple data example: 一个简单的数据示例:

> 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)

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? 无论如何使用oneway.test()函数中的对象来执行此操作? 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. 任何意见,将不胜感激。

Just to add, despite the bad timing and given than I have been seeking for something similar myself, there is also the option to perform a Games-Howell test. 只是补充一点,尽管时机不好而且比我自己一直在寻找类似的东西,但也可以选择进行Games-Howell测试。 This has even been incorporated under the 'posthoc.tgh' function in the 'userfriendlyscience' R package as introduced in this stackexchange_post . 这甚至被所述“posthoc.tgh”的作用下结合在“userfriendlyscience” R封装在此引入stackexchange_post It represents an extension of the Tukey‐Kramer test for unequal variances. 它代表了Tukey-Kramer测试对不等方差的扩展。 posthocTGH {userfriendlyscience} posthocTGH {userfriendlyscience}

Original publication (even from before I was born): Paul A. Games and John F. Howell. 原始出版物(甚至在我出生之前):Paul A. Games和John F. Howell。 Pairwise Multiple Comparison Procedures with Unequal N's and/or Variances: A Monte Carlo Study . 具有不等N和/或方差的成对多重比较程序:蒙特卡罗研究 Journal of Educational & Behavioral Statistics, Vol.1, No. 2, 1976, pp. 113-125. Journal of Educational&Behavioral Statistics,Vol.1,No。2,1976,pp.113-125。 doi: 10.3102/10769986001002113 doi:10.3102 / 10769986001002113

Here are two methods: 这有两种方法:

The Data 数据

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")

Base R 基地R.

First, the set of unique pairs of the Group factor: 首先, 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" 

Now the analysis: 现在分析:

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

(This can be done just as easily for the F-statistic.) (对于F统计量,这可以很容易地完成。)

Using dplyr 使用dplyr

First, the set of unique pairs of the Group factor: 首先, 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

If the order really matters, you can add dplyr::arrange(Var1, Var2) early into this pipeline, perhaps after the call to expand.grid . 如果订单真的很重要,你可以在调用expand.grid之后尽早将dplyr::arrange(Var1, Var2)添加到此管道中。

Now the analysis: 现在分析:

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!) (我没有对这两者中的任何一个的表现提出任何要求;通常一个人会发现像这个例子的数据很少,但另一个将提前出现更大的集合。他们似乎都执行相同的统计成对比较结果相同。给你!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 使用 post-hoc 运行多个独立的单向方差分析 - R running several independent one-way ANOVA with post-hoc 使用rpsychi(R)和随后的事后成对比较来进行单向方差分析 - Perfom a one-way ANOVA using rpsychi (R) and the subsequent post-hoc pair-wise comparisons 方差分析的一种方法:事后比较“在图形中添加字母” - One way ANOVA: post-hoc comparison “adding letters to graph” R:对混合线性 model 进行 ANOVA 后的事后测试给出不同的结果,为什么以及如何进行? - R: Post-hoc tests after ANOVA on mixed linear model give different results, why and how to proceed? R中重复测量的bw单向方差分析和单向方差分析有什么区别 - what‘s the difference bw One-way ANOVA and One-way ANOVA for repeated measures In R 具有重复测量的方差分析和 R 中的 TukeyHSD 事后检验 - ANOVA with repeated measures and TukeyHSD post-hoc test in R 韦尔奇方差分析后进行特定成对比较的事后 - post hoc for specific pairwise comparisons after welch's anova 如何使用 R 中的正确(在受试者内)错误项对双向混合方差分析中的显着交互进行事后测试? - How can I run post-hoc tests for a significant interaction in Two-Way Mixed ANOVA with the correct (within subjects) error term in R? R:数字列的单向Anova和成对事后检验(土耳其,舍菲或其他) - R: One Way Anova and pairwise post hoc tests (Turkey, Scheffe or other) for numerical columns R:TUKEY遵循单因素方差分析 - R : TUKEY following one-way ANOVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM