Skip to content
Prev 2427 / 20628 Next

Fixed effects only model with lme4

A quick hack to compute a likelihood profile, for changing
random-effects variances.  A slight special case is to compute the
likelihood of a model with variance of the random effect set to zero.
This works only for a single-random-effect model, and is pulled from
code in Doug Bates's vignette.  In the past this would have dangerously
modified your original model, but I think that's fixed now.

   Other glmm hacks are available at
http://glmm.wikidot.com/local--files/reef-fish/glmmfuns.R

  I would be happy to hear about extensions or corrections.

  cheers
    Ben Bolker

## extract likelihood based on zero variance for a single random
##  effect
zerodev <- function(mm) {
  varprof(mm,0,0,1)[["ML"]]
}

varprof <- function(mm,lower=0,upper=20,n=101) {
  sg <- seq(lower, upper, len = n)
  orig.sd <- attr(VarCorr(mm)[[1]],"stddev")
  dev <- mm at deviance
  nc <- length(dev)
  nms <- names(dev)
  vals <- matrix(0, nrow = length(sg), ncol = nc, dimnames = list(NULL,
nms))
  update_dev <- function(sd) {
    .Call("mer_ST_setPars", mm, sd, PACKAGE = "lme4")
    .Call("mer_update_L", mm, PACKAGE = "lme4")
    res <- try(.Call("mer_update_RX", mm, PACKAGE = "lme4"), silent = TRUE)
    if (inherits(res, "try-error")) {
      val <- NA
    } else {
      .Call("mer_update_ranef", mm, PACKAGE = "lme4")
      .Call("mer_update_dev", mm, PACKAGE = "lme4") ## added for glmmML
      val <- mm at deviance
    }
    val
  }
  for (i in seq(along = sg)) {
      vals[i,] <- update_dev(sg[i])
    }
  update_dev(orig.sd) ## hack! to restore original sd
  data.frame(sd=sg,vals)
}
Emmanuel Charpentier wrote: