update() inside a function only searches the global environment?

前端 未结 2 2140
半阙折子戏
半阙折子戏 2020-12-16 21:09

I tried to write a wrapper function to do likelihood ratio tests in batches. I tried to include update() to update the initial model. However, it seems that instead of looki

2条回答
  •  醉酒成梦
    2020-12-16 21:29

    Although I really like @Hadley's answer (and will likely use that function myself), you can also specify a data argument in the update function. (Here, I assumed you wanted to pass temp to your models.)

    model1a <- update(model1, ~.-factor1:factor2, data = temp)
    

    EDIT

    If you're looking to compare models with anova, update will mung up the name of the data argument and "trick" anova into believing that the two models were fit using two different datasets. Updating only the formula and creating a new model will avoid this:

    foo <- function(){
                      temp <- fake
                      model1 <- lmer(data~factor1*factor2 + (1 |subj), data=temp)
                      newForm <- update.formula(formula(model1), ~.-factor1:factor2)
                      model1a <- lmer(newForm, data=temp)
                      anova(model1, model1a)
                }
    

提交回复
热议问题