Skip to content
Prev 9677 / 20628 Next

lmer update environment: Bug?

James Forester <jdforest at ...> writes:
It turns out that this stuff is really, really, really, hard to get right.
I've been able to get it to work reliably (in almost all permutations)
in the development version of lme4, but I can't easily come up with
a better workaround/hack than the one you have.

  The problem is that the update method is looking in its parent
environment for the objects in the call.  'dat1' doesn't live in
the frame of testlmer, it lives in the global environment ...

   If you're willing/able to install the development version from
github

library("devtools")
install_github("lme4",user="lme4")

(you'll need to have installed RcppEigen etc.)
that should work.  However, we're still having some fragility
issues with GLMMs.  However, if you're using LMMs (lmer) only,
as far as we know the development version will do everything the
stable version can do ... (and more)

  Ben Bolker
###########################
## Test function
###########################

testlmer<-function(dat1, test.update=TRUE, save.global=FALSE){

    if(save.global){
        dat1<<-dat1
    }

    ##This model borrowed from the examples in ?lmer
    basemod=glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
    family = binomial, data = dat1)

    if(test.update){
        ##This is where the problem seems to be
        update.mod=eval.parent(update(basemod,.~. + rand.dat))
        return(update.mod)
    } else return(basemod)
}

###########################
## Test script
###########################
library(lme4.0)

##add a spurious column of data
cbpp$rand.dat = rnorm(nrow(cbpp))

## this works: a glmer model is fit within the function
##   and is then updated in the global environment
testmod = testlmer(cbpp, test.update=FALSE, save.global=FALSE)
testmod = update(testmod,.~. + rand.dat,data=cbpp)

## this fails: attempting to update the base model,
##   the function fails with "object 'dat1' not found"
##   In this case 'dat1' is only within the function environment
testmod = testlmer(cbpp, test.update=TRUE, save.global=FALSE)

## this works: here the data are saved to the global environment
##    before the model update
testmod = testlmer(cbpp, test.update=TRUE, save.global=TRUE)
rm("dat1")