具有不同因变量的 R 的方差分析
ANOVA on R with different dependent variables
到目前为止我知道如何在 R 上进行 运行 方差分析,但我总是必须将代码复制到 运行 de ANOVA 以获得另一个变量,我想知道我是否可以通过某种方式传递它到循环中的 aov() 变量的名称并将方差分析的结果存储在变量中,这样我就不必通过复制代码块来手动更改它们。
例如:
我要测试的变量:Z、Y、X
分类变量:治疗
VectorVariables = c(Z, Y, X)
for (i in Vector) {
AnovaZ <- aov(Z ~ Treatment) #then
AnovaY <- aov(Y ~ Treatment) # and so on..
}
某种程度上是可能的??
如果你想做一个循环,诀窍是使用as.formula(paste())
。
创建一个列表(我们称之为 result
)来存储每个 aov
输出。然后循环存储在 Vector
:
中的因变量名称
n <- length(Vector)
result <- vector(mode="list", length=n)
for(i in 1:n) {
result[[i]] <- aov(as.formula(paste(Vector[i], "~ Treament")))
}
不需要for
循环!您可以简单地将 cbind
个不同的响应变量放在一起。
这是一个例子:
由于您没有提供样本数据集,我根据 npk
数据集生成了一些样本数据,其中我添加了第二个响应变量 yield2
这是与 yield
相同,但添加了一些白噪声。
set.seed(2018)
df <- npk
df$yield2 <- df$yield + rnorm(nrow(df), mean = 0, sd = 5)
根据两个响应变量 yield
和 yield2
执行方差分析
res <- aov(cbind(yield, yield2) ~ block, df)
#Call:
# aov(formula = cbind(yield, yield2) ~ block, data = df)
#
#Terms:
# block Residuals
#resp 1 343.295 533.070
#resp 2 905.0327 847.2597
#Deg. of Freedom 5 18
#
#Residual standard errors: 5.441967 6.860757
#Estimated effects may be unbalanced
resp 1
和 resp 2
分别给出 运行 aov(yield ~ block, df)
和 aov(yield2 ~ block, df)
的平方和。
因此在您的情况下,命令类似于
res <- aov(cbind(Y, Z) ~ Treatment)
或者,如果您想 运行 并存储来自单独方差分析的结果,请将响应变量存储在 list
中并使用 lapply
:
lapply(list(Y = "Y", Z = "Z"), function(x)
aov(as.formula(sprintf("%s ~ Treatment", x)), df))
这会产生 list
个方差分析结果,其中每个 list
元素对应一个响应变量。
另一个解决方案是使用列表列和purrr::map。这在处理许多模型时很有用(例如,参见 http://r4ds.had.co.nz/many-models.html)。
library(tidyverse)
aov_f <- function(df) {aov(value ~ carb, data = df)}
mtcars_n <- gather(mtcars, obs, value, mpg:gear) %>%
group_by(obs) %>%
nest() %>%
mutate(aov = map(data, aov_f))
到目前为止我知道如何在 R 上进行 运行 方差分析,但我总是必须将代码复制到 运行 de ANOVA 以获得另一个变量,我想知道我是否可以通过某种方式传递它到循环中的 aov() 变量的名称并将方差分析的结果存储在变量中,这样我就不必通过复制代码块来手动更改它们。
例如:
我要测试的变量:Z、Y、X
分类变量:治疗
VectorVariables = c(Z, Y, X)
for (i in Vector) {
AnovaZ <- aov(Z ~ Treatment) #then
AnovaY <- aov(Y ~ Treatment) # and so on..
}
某种程度上是可能的??
如果你想做一个循环,诀窍是使用as.formula(paste())
。
创建一个列表(我们称之为 result
)来存储每个 aov
输出。然后循环存储在 Vector
:
n <- length(Vector)
result <- vector(mode="list", length=n)
for(i in 1:n) {
result[[i]] <- aov(as.formula(paste(Vector[i], "~ Treament")))
}
不需要for
循环!您可以简单地将 cbind
个不同的响应变量放在一起。
这是一个例子:
由于您没有提供样本数据集,我根据
npk
数据集生成了一些样本数据,其中我添加了第二个响应变量yield2
这是与yield
相同,但添加了一些白噪声。set.seed(2018) df <- npk df$yield2 <- df$yield + rnorm(nrow(df), mean = 0, sd = 5)
根据两个响应变量
执行方差分析yield
和yield2
res <- aov(cbind(yield, yield2) ~ block, df) #Call: # aov(formula = cbind(yield, yield2) ~ block, data = df) # #Terms: # block Residuals #resp 1 343.295 533.070 #resp 2 905.0327 847.2597 #Deg. of Freedom 5 18 # #Residual standard errors: 5.441967 6.860757 #Estimated effects may be unbalanced
resp 1
和resp 2
分别给出 运行aov(yield ~ block, df)
和aov(yield2 ~ block, df)
的平方和。
因此在您的情况下,命令类似于
res <- aov(cbind(Y, Z) ~ Treatment)
或者,如果您想 运行 并存储来自单独方差分析的结果,请将响应变量存储在 list
中并使用 lapply
:
lapply(list(Y = "Y", Z = "Z"), function(x)
aov(as.formula(sprintf("%s ~ Treatment", x)), df))
这会产生 list
个方差分析结果,其中每个 list
元素对应一个响应变量。
另一个解决方案是使用列表列和purrr::map。这在处理许多模型时很有用(例如,参见 http://r4ds.had.co.nz/many-models.html)。
library(tidyverse)
aov_f <- function(df) {aov(value ~ carb, data = df)}
mtcars_n <- gather(mtcars, obs, value, mpg:gear) %>%
group_by(obs) %>%
nest() %>%
mutate(aov = map(data, aov_f))