如何传递'对象'动态参数anova()函数

时间:2015-04-25 00:43:18

标签: r anova lme4 nlme

我正在努力编写一个脚本,该脚本允许更灵活的方法来比较使用lme4nlme包的不同线性混合效果模型。因为我不想为我添加或删除的每个模型调整脚本,所以我正在寻找动态方法。这样做我只需要调整一个包含模型公式的字符串的变量。

除非anova()出现,否则此工作正常。anova()不接受包含相应类的列表元素:

###### Here is my problem
# comparing models by means of ANOVA
anova(lme.lst)                                  #### --> does not work
anova(lme.lst[[1]], lme.lst[[2]], lme.lst[[3]]) #### would work but kills the dynamic approach
######

我没有想出一个简洁的方法来分解列表并将多个参数传递给anova()函数。我试了unlist()但没有成功。

这是一个最小的例子(改编自lme4 manual, p. 8):

require(lme4)
require(AICcmodavg)

# Variable containing of strings in order to describe the fixed effect terms 
# (wihout response/dependen variable)                                            ### should be orderd from
callModel <- c("angle ~ recipe + temp        + (1|recipe:replicate)",  # model1  ### small
               "angle ~ recipe + temperature + (1|recipe:replicate)",  # model2  ### too                
               "angle ~ recipe * temperature + (1|recipe:replicate)")  # model3  ### BIG

# convert string array 'callFeVar' into a list of formulas
callModel <- sapply(callModel, as.formula)

# create an empty list for safing the results of fitted model 
lme.lst <- list()
# do model fitting in a loop and change list names
for (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 is my problem
# comparing models by means of ANOVA
anova(lme.lst)                                  #### --> does not work
anova(lme.lst[[1]], lme.lst[[2]], lme.lst[[3]]) #### would work but kills the dynamic approach
######

# comparing models by means of AICc
aictab(lme.lst)                                 #### accepts list

1 个答案:

答案 0 :(得分:5)

do.call使用列表中提供的参数调用函数。

do.call(anova, lme.lst)