[R-sig-ME] bug/problem with update for merMod

Ben Bolker bbolker at gmail.com
Fri Feb 7 16:21:57 CET 2014


Alexandra Kuznetsova <alku at ...> writes:

> 
> Dear all,
> 
> I have an issue with update.merMod function.  
>I would like to call it within a function and to send some extra
> parameters to it.
> The code below fails with an error:
> 
> updateModel <- function(model, reml, l)
> {
> 
>   nfit <- update(object=model, formula.=as.formula(paste(".~."))
>                  , REML=reml ,contrasts=l, evaluate=FALSE)
>   env <- environment(formula(model))
>   nfit <- eval(nfit, envir = env)
>   #nfit <- eval.parent(nfit)
>   #nfit <- eval(nfit, parent.frame())
> }
> 
> m <- lmer(Reaction ~ Days +(1|Subject), data=sleepstudy)
> updateModel(m, FALSE, "contr.SAS")
> 
> Error in model.matrix.default(fixedform, fr, contrasts) : object 'l' not found
> 
> eval.parent(nfit) fails as well.
> 

  This works (for me) if I copy the new arguments into the environment
of the formula:

updateModel <- function(model, reml, l)
{

  nfit <- update(object=model, formula.=as.formula(paste(".~."))
                 , REML=reml ,contrasts=l, evaluate=FALSE)
  env <- environment(formula(model))
  assign("l",l,envir=env)
  assign("reml",reml,envir=env)
  eval(nfit, envir = env)
}

library(lme4)
m <- lmer(Reaction ~ Days +(1|Subject), data=sleepstudy)
updateModel(m, FALSE, "contr.SAS")



More information about the R-sig-mixed-models mailing list