user53330 - 11 months ago 107

R Question

I would like to reproduce lmer diagnostic plots in ggplot2. Particularly, I know that for a lmer model

`DV ~ Factor1 * Factor2 + (1|SubjID)`

`plot(model, resid(.)~fitted(.)|Factor1+Factor2)`

I would like to generate the same plot, but using ggplot2. I tried using

`qplot(resid(model), fitted(model))`

I'd appreciate any advise to achieve this, thanks!

EDIT

The core of my question: given any lmer model, how can I create a data-frame including fitted and residual values AND the Factor information for each value? something like:

`Factor1 Factor2 Fitted Resid`

0 0 987 654

0 0 123 456

(...)

I could not figure that out from lmer documentation on the

`resid()`

`fitted()`

Answer Source

Here is an example of what we want, **a reproducible example**

```
data(Orthodont, package="nlme")
Orthodont$age <- as.factor(Orthodont$age)
model <- lmer(distance ~ age * Sex + (1|Subject), Orthodont)
plot(model, resid(.) ~ fitted(.) | age + Sex )
```

answer

```
ggplot(model, aes(.fitted, .resid)) + geom_point() +
facet_wrap(~ Sex + age, ncol = 4) # (edited) I noticed fortify(model) isn't necessary.
```