R lme4绘制lmer残差〜由ggplot

时间:2016-10-03 13:33:03

标签: r ggplot2 lme4

我想在ggplot2中重现lmer诊断图。特别是,我知道对于lmer模型DV ~ Factor1 * Factor2 + (1|SubjID)我可以简单地调用plot(model, resid(.)~fitted(.)|Factor1+Factor2)来生成基于格的Residuals Vs.适合每个Factor1 + Factor 2组合的图形。

我想生成相同的情节,但使用ggplot2。我尝试使用qplot(resid(model), fitted(model))以及使用不同参数的不同变体,但有关分面所需因素的信息并未通过此(和类似)调用。

我很感激任何建议,谢谢!

EDIT 我的问题的核心:给定任何lmer模型,如何创建包含拟合值和残差值的数据框以及每个值的因子信息?类似的东西:

Factor1 Factor2 Fitted Resid 0 0 987 654 0 0 123 456 (...) 我无法从resid()fitted()函数

上的lmer文档中找到答案

1 个答案:

答案 0 :(得分:0)

以下是我们想要的示例,可重现的示例

data(Orthodont, package="nlme")
Orthodont$age <- as.factor(Orthodont$age)

model <- lmer(distance ~ age * Sex + (1|Subject), Orthodont)
plot(model, resid(.) ~ fitted(.) | age + Sex )

答案

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