r - How to pass the 'object' argument dynamically to anova() function -
i struggling write script allows more flexible approach compare different linear mixed-effect models making use of lme4
or nlme
package. not want adjust script each , every model add or drop, looking dynamic approach. doing have adjust 1 variable contains character strings of model formulas.
this works fine unless anova()
comes in. anova()
not accept list of elements containing appropriate classes:
###### here problem # comparing models means of anova anova(lme.lst) #### --> not work anova(lme.lst[[1]], lme.lst[[2]], lme.lst[[3]]) #### work kills dynamic approach ######
i did not figure neat way decompose list , pass multiple arguments anova()
function. tried unlist()
without success.
here minimal example (adapted lme4 manual, p. 8):
require(lme4) require(aiccmodavg) # variable containing of strings in order describe fixed effect terms # (wihout response/dependen variable) ### should orderd callmodel <- c("angle ~ recipe + temp + (1|recipe:replicate)", # model1 ### small "angle ~ recipe + temperature + (1|recipe:replicate)", # model2 ### "angle ~ recipe * temperature + (1|recipe:replicate)") # model3 ### big # convert string array 'callfevar' list of formulas callmodel <- sapply(callmodel, as.formula) # create empty list safing results of fitted model lme.lst <- list() # model fitting in loop , change list names (i in 1 : length(callmodel)) { lmetmp <- lmer(callmodel[[i]], cake, reml= false) lme.lst[i] <- list(lmetmp) names(lme.lst)[i] <- deparse(callmodel[[i]]) } # remove temporary variable rm(lmetmp) # summary of models lapply(lme.lst, summary) ###### here problem # comparing models means of anova anova(lme.lst) #### --> not work anova(lme.lst[[1]], lme.lst[[2]], lme.lst[[3]]) #### work kills dynamic approach ###### # comparing models means of aicc aictab(lme.lst) #### accepts list
do.call
calls function arguments supplied in list.
do.call(anova, lme.lst)
Comments
Post a Comment