5

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?

Henrik
  • 65,555
  • 14
  • 143
  • 159
Kika Tarsi
  • 47
  • 1
  • 1
  • 2
  • I think you're assuming since you can run `plot` on a `lm` object, you can do that for a `lmer` object too. Note that there is a `plot.lm` but not a `plot.lmer` function. – Señor O Oct 08 '13 at 20:10
  • @SeñorO They are assuming correctly: `?plot.merMod`. – joran Oct 08 '13 at 20:24
  • @joran I don't have `plot.merMod` when I load `lme4`. Additionally, an `lmer` model goes into `plot.default` on my machine. – Señor O Oct 08 '13 at 20:29
  • @SeñorO I didn't either, until I upgrade **lme4** to the most current version on CRAN. – joran Oct 08 '13 at 20:30
  • 1
    @joran I would imagine that's OP's problem then - I get the same error (which comes from `xy.coords`) – Señor O Oct 08 '13 at 20:31
  • Yes, wish I had a plot.lmer function. I updated my lme4 package and tried working with plot.merMod, but I'm still getting the same "cannot coerce S4 to vector" error message. Grrr... – Kika Tarsi Oct 08 '13 at 21:18
  • Perhaps it's platform specific. I'm on OS X and as of lmer 1.0-4 I don't have a `plot.merMod`. – John Oct 08 '13 at 21:23
  • 1
    OK, I need a reproducible example. With a fresh install of `lme4` 1.0-4 from CRAN, this works for me: `library(lme4); fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy); plot(fm1)`. `plot.merMod` is not exported but you should be able to see it via `lme4:::plot.merMod`. abbrv. `sessionInfo()`: r-devel r63889, 32-bit linux, lme4_1.0-4 ; Matrix_1.0-14 ; lattice_0.20-23 ; compiler_3.1.0 ; grid_3.1.0 ; MASS_7.3-29 ; minqa_1.2.1 ; nlme_3.1-111 ; splines_3.1.0 ; tools_3.1.0 – Ben Bolker Oct 08 '13 at 21:25
  • I'll say it again in case it got lost in the wordy command above: `plot.merMod` is not exported but you should be able to see it via `lme4:::plot.merMod`. Does `lme4:::plot.merMod(best)` work? – Ben Bolker Oct 08 '13 at 21:29
  • Just checked, this also works for me on 64-bit linux, R 2.15.3, with a fresh CRAN install – Ben Bolker Oct 08 '13 at 21:42
  • @KikaTarsi: if you just updated, what version do you have now? – Ben Bolker Oct 09 '13 at 00:55
  • Ah haaa, you were right, had to update. Just got 1.0.4. Using plot.merMod(best) didn't work--it couldn't find the function. But now plot(best) gives me my residuals, which is a start – Kika Tarsi Oct 10 '13 at 00:26

1 Answers1

8

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).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453