Question

Je souhaite effectuer une ANOVA entre des objets stockés dans deux listes distinctes, mais plutôt que de les exécuter un par un comme celui-ci.

> 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

Je voudrais utiliser une boucle pour effectuer une ANOVA entre les objets de chaque liste.J'ai essayé d'utiliser la fonction mapply, mais le résultat n'a pas produit les résultats que j'attendais.

> 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

Des suggestions sur la façon dont je peux procéder ?

Merci

Modifier:Exemple reproductible

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) 
Était-ce utile?

La solution

Utilisez le SIMPLIFY paramètre:

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

Autres conseils

Vous pouvez utiliser sapply

MyRes <- sapply(1:length(output.02), function(x) {
    anova(output.02[[x]], output.03[[x]])})

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top