data(Orthodont,package="nlme")
fm1 <- lmer(distance ~ age + (age|Subject), data=Orthodont)
## standardized residuals versus fitted values by gender
plot(fm1, resid(., scaled=TRUE) ~ fitted(.) | Sex, abline = 0)
## box-plots of residuals by Subject
plot(fm1, Subject ~ resid(., scaled=TRUE))
## observed versus fitted values by Subject
plot(fm1, distance ~ fitted(.) | Subject, abline = c(0,1))
## residuals by age, separated by Subject
plot(fm1, resid(., scaled=TRUE) ~ age | Sex, abline = 0)
## scale-location plot, with red smoothed line
scale_loc_plot <- function(m, line.col = "red", line.lty = 1,
line.lwd = 2) {
plot(fm1, sqrt(abs(resid(.))) ~ fitted(.),
type = c("p", "smooth"),
par.settings = list(plot.line =
list(alpha=1, col = line.col,
lty = line.lty, lwd = line.lwd)))
}
scale_loc_plot(fm1)
## Q-Q plot
lattice::qqmath(fm1, id=0.05)
ggp.there <- "package:ggplot2" %in% search()
if (ggp.there || require("ggplot2")) {
## we can create the same plots using ggplot2 and the fortify() function
fm1F <- fortify.merMod(fm1)
ggplot(fm1F, aes(.fitted, .resid)) + geom_point(colour="blue") +
facet_grid(. ~ Sex) + geom_hline(yintercept=0)
## note: Subjects are ordered by mean distance
ggplot(fm1F, aes(Subject,.resid)) + geom_boxplot() + coord_flip()
ggplot(fm1F, aes(.fitted,distance)) + geom_point(colour="blue") +
facet_wrap(~Subject) +geom_abline(intercept=0,slope=1)
ggplot(fm1F, aes(age,.resid)) + geom_point(colour="blue") + facet_grid(.~Sex) +
geom_hline(yintercept=0)+ geom_line(aes(group=Subject),alpha=0.4) +
geom_smooth(method="loess")
## (warnings about loess are due to having only 4 unique x values)
if(!ggp.there) detach("package:ggplot2")
}
Run the code above in your browser using DataLab