2015-04-25 144 views
1

我正在努力编写一个脚本,该脚本允许使用lme4nlme软件包比较不同的线性混合效果模型。由于我不想调整每个添加或删除模型的脚本,因此我正在寻找一种动态的方法。这样做我只需要调整一个包含模型公式的字符串的变量。如何将“对象”参数动态传递给anova()函数

这工作得很好,除非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 

回答

5

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

do.call(anova, lme.lst) 
+0

对于最小的示例,这种方式不起作用。我得到: '错误在anova.merMod(''角度〜食谱+温度+(1 |食谱:复制)“= ,: 参数”对象“丢失,没有默认' – ToJo

+0

我只是弄明白以下几点:当使用'do.call'时,'anova'不喜欢列表的名称参数,因此,将列表包装在'unname'中会有这样的把戏:'do.call (anova,unname(lme.lst))' 现在,通过将'model.names'参数设置为'names(me.lst)'也是很酷的任何想法?但是,后者只会是一个“不错的”功能。 – ToJo

相关问题