7
$\begingroup$

Whenever we include a random effect in a mixed model, since the estimates are shrunk, isn't it normal at the fundamental level that the residual plot will show a positive correlation between fitted values and residuals?

Here's a super simple model with no fixed effect and, as random effect, 500 subjects observed 4 times:

set.seed(1)
N = 500
R = 4

subjects = rep(factor(1:N),each = R)
mu = rep(runif(N),each = R)
y = mu + 0.2*rnorm(N*R)
df = data.frame(subjects,mu,y)

mdl = lme4::lmer(y ~ 1 + (1|subjects),df)
performance::check_model(mdl)

residual plot

It feels intuitive to me that estimates being shrunk, subjects with higher (lower) $\mu$ will show a positive (negative) residual.

Is it a problem? How can we diagnose (the linearity of) our models then?

Note: the trend exists also with a normal distribution of $\mu$s.

$\endgroup$

2 Answers 2

5
$\begingroup$

The random effect is assumed to follow a normal distribution, so if you simulate it as uniformly distributed, it's hard to pinpoint why you observe something unexpected. Nevertheless, as you indicated, the positive slope persists if you use a normal distribution, which I will use here to suggest a solution:

library("lme4")
set.seed(1)
N <- 500
R <- 4
subjects <- rep(factor(1:N),each = R)
mu <- rep(rnorm(N), each = R)
y <- mu + 0.2 * rnorm(N * R)
df <- data.frame(subjects, mu, y)
mdl <- lme4::lmer(y ~ 1 + (1|subjects), df)
plot(resid(mdl) ~ fitted(mdl), col = "steelblue", pch = 16)
abline(coef(lm(resid(mdl) ~ fitted(mdl))), col = 2, lwd = 2)

residuals vs fitted

One solution, provided by the DHARMa package, is to use simulated residuals conditional only on the fixed effects:

library("DHARMa")
simres <- simulateResiduals(mdl, re.form = ~ 0)

DHARMa1

Compare this to simulated residuals conditional on the fixed and random effects:

simres2 <- simulateResiduals(mdl, re.form = ~ 1 | subjects)

DHARMa2

$\endgroup$
4
  • $\begingroup$ I am unsure how this alleviates the issue of correlation between random effects and residuals, which are assumed to be independent in a random effects model. $\endgroup$
    – larmor
    Commented Aug 2 at 18:47
  • $\begingroup$ @larmor The plot in the question shows the residuals against the fitted values, which are not only comprised of the fixed effects, but also the random effects. In other words, the correlation you observe is between $\hat{\mathbf{\epsilon}}$ and $\mathbf{X\hat\beta} + \mathbf{Z \hat \upsilon}$. If you draw any conclusions about the fixed effects, the random effects and $\epsilon$ are assumed zero. The plot shown here only compares $\hat\epsilon$ to $\mathbf{X\hat\beta}$ (using a simulation-based approach) and is therefore a better way to assess issues with the fixed effects, like non-linearity. $\endgroup$ Commented Aug 2 at 19:01
  • $\begingroup$ I agree with all your statements, but I'm also worried about the assumption of independence between random effects and residuals. The plot in my question is displaying a correlation between them (since there is no fixed effect to correlate with). $\endgroup$
    – larmor
    Commented Aug 3 at 10:59
  • $\begingroup$ After a few days, my understanding is that while it is assumed in a random effect model that μs and ϵs are independent variables, it is not the case that conditional modes (i.e. individual estimates of random effects) and residuals will be uncorrelated. $\endgroup$
    – larmor
    Commented Aug 9 at 13:21
4
$\begingroup$

In regards to your question, and also to clarify the answer provided by Frans, I want to point out that a res ~ fitted plot has 2 axis, a) the x axis where you have the model predictions, and b) the y axis, where you have the residuals. For both axis, you can condition on the REs or not.

  1. When you condition on the REs when calculating the predictions on x, you can/will indeed get get the pattern that you highlight, in particular when the residuals are unconditional (but I believe there is a light pattern as well if you calculate conditional residuals). I show this here https://github.com/florianhartig/DHARMa/issues/43. For this reason, predicted values (on x) in DHARMa are always marginal predictions (= unconditional, not including the REs), and I believe it is good advice to NOT use res ~ FullPredictions, and rather use res ~ MarginalPredictions for mixed models.

  2. The re.form argument that Frans mentions determines in DHARMa how the residuals are calculated (i.e. the values on y, not on x). As shown in the GH issue above, this affects the correlation against conditions predictions, but I think this point is secondary to your question.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.