我想在存储在两个单独列表中的对象之间执行方差分析,而不是像这样一次执行一个
> anova(output.02[[1]], output.03[[1]])
Model df AIC BIC logLik Test L.Ratio p-value
output.02[[1]] 1 9 11221.77 11279.72 -5601.884
output.03[[1]] 2 13 11222.90 11306.60 -5598.450 1 vs 2 6.868822 0.143
> anova(output.02[[2]], output.03[[2]])
Model df AIC BIC logLik Test L.Ratio p-value
output.02[[2]] 1 9 10976.36 11034.31 -5479.182
output.03[[2]] 2 13 10974.90 11058.60 -5474.449 1 vs 2 9.465378 0.0505
我想使用循环在每个列表中的对象之间执行方差分析。我尝试使用 mapply 函数,但是输出没有产生我期望的结果。
> mapply(anova, output.02, output.03)
zimmrec zdelrec zdigiback zspotword zsdmt zglobcog zmmse
call factor,2 factor,2 factor,2 factor,2 factor,2 factor,2 factor,2
Model Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
df Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
AIC Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
BIC Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
logLik Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
Test factor,2 factor,2 factor,2 factor,2 factor,2 factor,2 factor,2
L.Ratio Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
p-value Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
关于如何做到这一点的任何建议?
谢谢
编辑:可重现的例子
attach(Orthodont)
set.seed(1234)
#example response variables
Orthodont$v1 <- rnorm(n=108, mean=20, sd=1)
Orthodont$v2 <- rnorm(n=108, mean=31, sd=2.8)
Orthodont$v3 <- rnorm(n=108, mean=15, sd=1.5)
head(Orthodont)
#function to loop the response variables through a lme function
#produces first batch of models
myfunc <- function(X){
lapply(X, function(.col){
y <- .col
out <- with(Orthodont, lme(y ~ age, random = ~ age | Subject, method = "ML", na.action = na.exclude, control = lmeControl(opt = "optim")))
out
})
}
output.02 <- myfunc(Orthodont[5:7]) #first list of models
myfunc2 <- function(X){
lapply(X, function(.col){
y <- .col
out <- with(Orthodont, lme(y ~ age + Sex, random = ~ age | Subject, method = "ML", na.action = na.exclude, control = lmeControl(opt = "optim")))
out
})
}
output.03 <- myfunc2(Orthodont[5:7])# second list of models
#anova for each pair of models
anova(output.02[[1]], output.03[[1]])
anova(output.02[[2]], output.03[[2]])
anova(output.02[[3]], output.03[[3]])
#mapply function
mapply(anova, output.02, output.03)