Avoid failure of confint.merMod on weighted models in lme4 when data object modified in calling frame

Viewed 487

I'm facing a problem when using lme4 glmer function with weights, where if the data object passed to glmer is modified, some functions such as confint no longer work on the model. Here is an example:

library(lme4)

set.seed(1)
n <- 1000
df <- data.frame(
  y=rbinom(n,1,.5),
  w=runif(n,0,1)*.1+.95,
  g=as.integer(round(runif(n,0,4)))
)
m <- glmer(cbind(y,1-y)~(1|g),data=df,weights=w,family=binomial())
confint(m)
df$w <- df$w*2
confint(m)

The 2nd call to confint gives this error:

Computing profile confidence intervals ...
Error in profile.merMod(object, which = parm, signames = oldNames, ...) : 
  Profiling over both the residual variance and
fixed effects is not numerically consistent with
profiling over the fixed effects only

It seems this has something to do with the profile function, as that function doesn't work after modifying the data frame.

The following seems to work to remove the dependency on the data object, but I am a bit uneasy not knowing if there might ever be bad side effects:

glmer2 <- function(...){
  cl <- match.call()
  df <- eval.parent(cl$data)
  cl[1] <- call("glmer")
  cl$data <- as.name("df")
  eval(cl)
}
m <- glmer2(cbind(y,1-y)~(1|g),data=df,weights=w,family=binomial())
confint(m)
df$w <- df$w*2
confint(m)

(results of confint don't change)

The reason I need something like this is that I am creating a series of models, and need to re-compute the weights between each one, and it would be quite messy to keep all of the data objects.

Why do model functions seem to rely on the data object still being present and unchanged in the calling environment? And is there a better way to solve this issue?

(R version 3.6.3 (2020-02-29), x86_64-redhat-linux-gnu, lme4_1.1-21)

0 Answers
Related