How do I run diagnostic plots for lmer in R?

Kika Tarsi picture Kika Tarsi · Oct 8, 2013 · Viewed 13.4k times · Source

I am trying to run diagnostic plots on an lmer model but keep hitting a wall. I'm not sure how much information I need to provide here, but here goes:

The model is simple:

best <- lmer(MSV_mm ~ Size_treat + (1|Rep) + (1|Patch) + (1|Trap), data= early_nopine). 

MSV_mm is numeric (snout-vent lengths) and Size_treat is a factor with 4 levels: Continuous, Large, Medium and Small. Rep, Patch and Trap are random effects.

When I run plot(best), I get the following error message:

"Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'"

I assume this is related to the lmer function. I have trolled the web and have not yet found an answer to this problem. Is it an lmer thing?

Answer

Ben Bolker picture Ben Bolker · Oct 9, 2013

I can reproduce this error with lme4.0, which is equivalent to earlier (pre-1.0.0) versions of lme4 from CRAN. If you use an up-to-date lme4 from CRAN (version 1.0-4 as of October 2013), this is what should happen:

library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
plot(fm1)

enter image description here

Although plot.merMod is not exported, it is documented (?plot.merMod), and you can see it via lme4:::plot.merMod (or getAnywhere("plot.merMod")).

If you want to reproduce this plot with an earlier version of lme4, you can do:

augData <- data.frame(sleepstudy,fitted=fitted(fm1),resid=residuals(fm1))
library(lattice)
xyplot(fitted~resid,data=augData)

You should think about whether you want deviance residuals (the default from residuals()) or Pearson residuals (the default for plot.merMod).