2
$\begingroup$

I have a database composed of n = 1000 observations of a variable to explain (y), an explanatory variable (x). The observations are grouped according to a variable "group", and I have only few observations per "group" (around 2), while the number of different "groups" is high.

I fitted a linear mixed model (with R lmer function, from package lme4) explaining y as a function of x, using "group" as a random effect. I then checked if the residuals were normal and independant of the fitted values and of the random effects.

**At this stage I got a problem:

  • the residuals show an upward-trend in relation to the fitted values
  • the residuals show an upward-trend in relation to the random effect prediction, which contradicts the assumption 1 in Pinheiro and Bates (2000, p174) : "the within group errors are [...] independent of the random effect"**

Here, the residuals are computed as "observed value - fixed effect - random effect" and the fitted are computed as "fixed effect + random effect".

I suspect these strange patterns are because the number of observations per "group" is low. Indeed, they nearly disappear when I simulate data with more observations per group.

I include this test code below to illustrate the problem:

# generate data 
library(nlme)
library(ggplot2)

nb.group <- 500
list.group <- as.character(seq(1,nb.group, 1)) # anmes of the different groups
nb.obs <- 1000

alpha <- 0.3 # fixed effect
gamma <- 1.5 # sd of random effect
sigma <- 1 # sd of individual erro

db.RE <- data.frame( # generate value for random effect
  group = list.group,
  A = rnorm(nb.group, 0, gamma)
)

db <- data.frame(
  x = rgamma(nb.obs, 10, 5), # explicative variable
  group = sample(list.group, size = nb.obs, replace = TRUE), # vector of group
  E = rnorm(nb.obs, 0, sigma) # individual error
)

db <- merge(db, db.RE, by = "group") # attribute the value of random effect corresponding to each group

db$y <- alpha*db$x + db$A + db$E # y : independant variable


# calibrate with RE

model <- lme(fixed = as.formula(y ~ x), random = ~ 1|group, data = db)

# diagnosis 

summary(model)

db_pred <- cbind(predict(model, level = c(0:1)), res = residuals(model, level = c(0:1))) # get fitted values and residuals
db_ranef <- data.frame(group = rownames(ranef(model)), ranef(model)) # get random effect prediction
names(db_ranef) <- c("group", "ranef")
db_pred <- merge(db_pred, db_ranef, by = "group")

# plot residuals as a function of fitted (at the population level)
graph.1 <- ggplot(data = db_pred, aes(x = cut(predict.fixed,
                                   breaks = seq(min(predict.fixed), max(predict.fixed), length.out = 10)), y = res.group)) +
  geom_boxplot() +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  ggtitle("residuals as a function of fitted (at the population level)")

# plot residuals as a function of fitted (at the individual level)
graph.2 <- ggplot(data = db_pred, aes(x = cut(predict.group,
                                   breaks = seq(min(predict.group), max(predict.group), length.out = 10)), y = res.group)) +
  geom_boxplot() +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  ggtitle("residuals as a function of fitted (at the individual level)")

# qq plot of residuals 
graph.3 <- ggplot(data = db_pred, aes(sample = res.group)) +
  stat_qq() + stat_qq_line() +
  theme_bw() +
  ggtitle("qq plot of residuals")

# qq plot of random effect 
graph.4 <- ggplot(data = db_pred, aes(sample = ranef)) +
  stat_qq() + stat_qq_line() +
  theme_bw() +
  ggtitle("qq plot of random effect")

# plot residuals as a function of random effect prediction
graph.5 <- ggplot(data = db_pred, aes(x = cut(ranef,
                                   breaks = seq(min(ranef), max(ranef), length.out = 10)), y = res.group)) +
  geom_boxplot() +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  ggtitle("residuals as a function of random effect prediction")


cowplot::plot_grid(plotlist = list(graph.1, graph.2, graph.3, graph.4, graph.5))


The corresponding diagnosis graphs are here : diagnosis graphs We observed two problems : (i) correlation between residuals and fitted value (including random effect) and (ii) correlation between residuals and random effect.

Is this correlation between residuals and fitted values (or random effects) expected when we have few observations per group, and could you explain to me why it should be the case?

$\endgroup$
6
  • $\begingroup$ I’m not much of an r user; however, is your package using a frequentist or Bayesian design? If Bayesian, it’s acceptable if your subgroups have different means (fixed effects) and variances. The random effects would be the coefficients for each $X_{group, individual}$. So greater residuals for one subgroup is acceptable; it just means that this subgroup has more variance relative to other subgroups. $\endgroup$
    – jbuddy_13
    Commented Dec 1, 2023 at 10:53
  • 1
    $\begingroup$ See github.com/florianhartig/DHARMa/issues/43 $\endgroup$ Commented Dec 1, 2023 at 13:18
  • $\begingroup$ @FlorianHartig thanks a lot for this very useful link! To be sure that I understand it well: (i) in the case that I do no resimulations (ie I only use predicted random-effect), should I also plot unconditional residuals against unconditional prediction? (ii) is it relevant in my case not to resimulate RE (as a reminder, my case is a simple linear mixed model)? (iii) how would you check the assumption in Pinheiro and Bates (p174) that "the within group errors are [...] independent of the random effect" $\endgroup$
    – MatthieuC
    Commented Dec 4, 2023 at 9:55
  • $\begingroup$ @jbuddy_13 thanks a lot for your comment. The nlme package use a frequentist design, as far as I understand. My issue is not really that residuals for some groups have a higher variance, it is rather that for groups with high random effect prediction, residuals are always positive and high, and for groups with low random effect prediction, residuals are always negative and high. $\endgroup$
    – MatthieuC
    Commented Dec 4, 2023 at 10:00
  • $\begingroup$ Mathieu, you should in general not plot against REs because they have a shrinkage and this creates the diagonal pattern. About the within-group error - I understand this as a check for heteroskedasticity wrt to the RE, but I don't have the reference at hand so maybe there is more context. $\endgroup$ Commented Dec 11, 2023 at 9:14

0

Your Answer

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

Browse other questions tagged or ask your own question.