[R-sig-ME] lmer update environment: Bug?

Ben Bolker bbolker at gmail.com
Sat Feb 23 20:57:50 CET 2013


James Forester <jdforest at ...> writes:

> While attempting to re-run some previously working code this morning, I
> seem to have stumbled upon a bug in how lmer() works with update(). For
> some reason, when I try to update a glmer or lmer model within a function,
> lmer() or update() can only see a dataframe if it is in the global
> environment. I have included an example below (a similar script written for
> glm works fine). Any ideas on how to correctly specify the environment so I
> can get around this?
> 
> Thanks for your help
> 
> James Forester

  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




> 
> Platform: x86_64-pc-linux-gnu
> R version: 2.15.2 (2012-10-26)
> lme4 version: 0.999999-0
> 
###########################
## 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")



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