[英]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.