简体   繁体   English

R 使用 post-hoc 运行多个独立的单向方差分析

[英]R running several independent one-way ANOVA with post-hoc

I have the following sample data:我有以下示例数据:

structure(list(Class = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L), .Label = c("A", "B", "C ", "D ", "E"), class = "factor"), 
    a = c(0.10881116, 0.526242737, 0.982902999, 0.320738663, 
    0.652972541, 0.039061302, 0.866235756, 0.319948863, 0.49249116, 
    0.387460274), b = c(0.962253789, 0.504883561, 0.958827249, 
    0.112715995, 0.481341694, 0.022454068, 0.365585675, 0.243682534, 
    0.540064663, 0.79933528), c = c(0.68491864, 0.170941001, 
    0.067239671, 0.350063079, 0.303616697, 0.811791432, 0.986189818, 
    0.261161444, 0.366817736, 0.393204464), d = c(0.171410187, 
    0.795272464, 0.127037962, 0.729957086, 0.783967392, 0.836820247, 
    0.39774571, 0.727385402, 0.191486044, 0.316815623), e = c(0.018072241, 
    0.360542881, 0.435783461, 0.557028064, 0.645997614, 0.631136435, 
    0.316623636, 0.871827327, 0.615828269, 0.956653665), f = c(0.152489388, 
    0.500431046, 0.249617685, 0.855327742, 0.578962117, 0.510960229, 
    0.910920471, 0.8616062, 0.301616817, 0.691359783), g = c(0.016796537, 
    0.597620997, 0.169782711, 0.190080222, 0.781218649, 0.323382447, 
    0.968615432, 0.287030348, 0.754648917, 0.720887331)), class = "data.frame", row.names = c(NA, 
-10L))

I'm looking to run several one-way ANOVAs.我正在寻找运行几个单向方差分析。 I want to run ANOVAs between "Class" (A, B, C, D, etc.) for each column independently (ie one anova for "a", another for "b", another for "c", etc. for a total of 7 ANOVAs).我想独立地为每一列在“类”(A、B、C、D 等)之间运行方差分析(即一个方差分析用于“a”,另一个用于“b”,另一个用于“c”,等等。总共 7 个方差分析)。 For each, I want to run a Scheffe post-hoc test.对于每个,我想运行 Scheffe 事后测试。

So, for instance, for one ANOVA, the code would be因此,例如,对于一个方差分析,代码将是

res.aov <- aov(a ~ Class, data = df)
library(DescTools)
ScheffeTest(res.aov)

Is there a way to run all ANOVAs at once?有没有办法一次运行所有方差分析?

We can do this in a loop我们可以在循环中做到这一点

library(DescTools)
out <- lapply(names(df)[-1], function(nm) 
       ScheffeTest(aov(reformulate('Class', nm), data = df)))
names(out) <- names(df)[-1]

In the dput output, find some leading/lagging spaces for 'Class'dput输出中,找到“Class”的一些前导/滞后空格

df$Class <- trimws(df$Class)

Just in case, the formula can also be constructed with paste以防万一,公式也可以用paste构造

out <- lapply(names(df)[-1], function(nm) 
       ScheffeTest(aov(as.formula(paste(nm, "~", "Class")), data = df)))
names(out) <- names(df)[-1]

Or with sprintf或者用sprintf

out <- lapply(names(df)[-1], function(nm) 
       ScheffeTest(aov(as.formula(sprintf("%s ~ Class", nm)), data = df)))
names(out) <- names(df)[-1]

Or if we decide to do this in tidyverse或者如果我们决定在tidyverse这样做

library(purrr)    
library(dplyr)
map(names(df)[-1], ~ aov(reformulate('Class', .x), data = df) %>%
                  ScheffeTest)

暂无
暂无

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

相关问题 使用rpsychi(R)和随后的事后成对比较来进行单向方差分析 - Perfom a one-way ANOVA using rpsychi (R) and the subsequent post-hoc pair-wise comparisons 使用Welch在R中校正的单向ANOVA的事后检验 - Post-hoc tests for one-way ANOVA with Welch's correction in R 方差分析的一种方法:事后比较“在图形中添加字母” - One way ANOVA: post-hoc comparison “adding letters to graph” 具有重复测量的方差分析和 R 中的 TukeyHSD 事后检验 - ANOVA with repeated measures and TukeyHSD post-hoc test in R R:TUKEY遵循单因素方差分析 - R : TUKEY following one-way ANOVA R中分层样本的单向ANOVA - One-way ANOVA for stratified samples in R R:如何“提取” Dunnetts 检验的 p 值(方差分析后的事后) - R: How to “extract” the p-values of a Dunnetts-test (Post-Hoc after ANOVA) R中重复测量的bw单向方差分析和单向方差分析有什么区别 - what‘s the difference bw One-way ANOVA and One-way ANOVA for repeated measures In R R:对混合线性 model 进行 ANOVA 后的事后测试给出不同的结果,为什么以及如何进行? - R: Post-hoc tests after ANOVA on mixed linear model give different results, why and how to proceed? 如何使用 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM