1

我想在存储在两个单独列表中的对象之间执行方差分析,而不是像这样一次执行一个

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

2 回答 2

3

使用SIMPLIFY参数:

mapply(anova, output.02, output.03, SIMPLIFY=FALSE) 
#$v1
#                 Model df      AIC      BIC    logLik   Test   L.Ratio p-value
#dots[[1L]][[1L]]     1  6 324.4204 340.5132 -156.2102                         
#dots[[2L]][[1L]]     2  7 326.2229 344.9978 -156.1115 1 vs 2 0.1974693  0.6568
#
#$v2
#                 Model df      AIC      BIC    logLik   Test  L.Ratio p-value
#dots[[1L]][[2L]]     1  6 524.0956 540.1884 -256.0478                        
#dots[[2L]][[2L]]     2  7 525.7577 544.5326 -255.8788 1 vs 2 0.337934   0.561
#
#$v3
#                 Model df      AIC      BIC    logLik   Test  L.Ratio p-value
#dots[[1L]][[3L]]     1  6 387.4002 403.4930 -187.7001                        
#dots[[2L]][[3L]]     2  7 389.1333 407.9082 -187.5667 1 vs 2 0.266947  0.6054
于 2014-07-29T12:16:57.013 回答
0

你可以使用sapply

MyRes <- sapply(1:length(output.02), function(x) {
    anova(output.02[[x]], output.03[[x]])})
于 2014-07-29T09:49:19.257 回答