# Modelling longitudinal data with crossed random effects

Let's say I have 40 participants. They are each measured three times (session). On each visit they see ten stimuli (item), which can be one of two types (type). They then get some score for each stimuli. How could I model the effect of stimulus type, session number, and their interaction, with lme4?

I've included some example data, and my best guess at the model with the full random effects structure below.

library("lme4")

participant <- rep(1:40, each = 30)
session <- rep(rep(1:3, each = 10), times = 40)
item <- rep(1:10, times = 120)
type <- rep(1:2, times = 600)
score <- rnorm(1200)

data <- cbind(participant, session, item, type, score)
data <- as.data.frame(data)

data$$participant <- factor(data$$participant)
data$$session <- factor(data$$session)
data$$item <- factor(data$$item)
data$$type <- factor(data$$type)

m <- lmer(score ~ type * session + (1 + type | participant / session) +
(1 | item / session), data = data)

• Why is item included as a random effect? Why is session included as both fixed and random? Oct 23, 2019 at 6:34

First, note that the simulated data above results in a singular model fit because there is no variation in the response among any of the random factors. This can be overcome with a simple modification:

library(lme4)

set.seed(15)
participant <- rep(1:40, each = 30)
session <- rep(rep(1:3, each = 10), times = 40)
item <- rep(1:10, times = 120)
type <- rep(1:2, times = 600)
# score <- rnorm(1200)                        ##### This line removed
score <- participant + item + rnorm(1200)     ##### This line added

data <- cbind(participant, session, item, type, score)
data <- as.data.frame(data)

data$$participant <- factor(data$$participant)
data$$session <- factor(data$$session)
data$$item <- factor(data$$item)
data$$type <- factor(data$$type)

m <- lmer(score ~ type * session + (1 + type | participant / session) +
(1 | item / session), data = data)


Second, now note that the model m above does not converge. This is because the fixed effect session is also included as a random grouping factor, which does not make any sense. A more sensible alternative is:

m0 <- lmer(score ~ type * session + (1 | participant) + (1 | item), data = data)


where I have also removed the random slope/coefficient for type by participant which also does not make sense given that the data are simple random draws - that is, there is no systematic association of score and type for each participant.

> summary(m0)
Linear mixed model fit by REML ['lmerMod']
Formula: score ~ type * session + (1 | participant) + (1 | item)
Data: data

REML criterion at convergence: 3828.3

Scaled residuals:
Min      1Q  Median      3Q     Max
-4.2095 -0.6430  0.0437  0.6908  3.1569

Random effects:
Groups      Name        Variance Std.Dev.
participant (Intercept) 136.623  11.689
item        (Intercept)   9.856   3.139
Residual                  1.024   1.012
Number of obs: 1200, groups:  participant, 40; item, 10

Fixed effects:
Estimate Std. Error t value
(Intercept)    25.557744   2.322048  11.007
type2           0.988240   1.988126   0.497
session2       -0.025759   0.101185  -0.255
session3       -0.042207   0.101185  -0.417
type2:session2 -0.028411   0.143098  -0.199
type2:session3 -0.002558   0.143098  -0.018

Correlation of Fixed Effects:
(Intr) type2  sessn2 sessn3 typ2:2
type2       -0.428
session2    -0.022  0.025
session3    -0.022  0.025  0.500
type2:sssn2  0.015 -0.036 -0.707 -0.354
type2:sssn3  0.015 -0.036 -0.354 -0.707  0.500


This model estimates the fixed effect of stimulus type, session number, and their interaction, while controlling for crossed random effects for participant and item, as requested.

Random slopes for type, session and their interaction could potentially be included, provided that the data supports such a structure (the simulated data do not) and provided these are indicated by the underlying theory of the data generation process. It is not generally a good idea to begin with a maximal random effects structure.

• Hi Robert, thanks for the answer! Can you explain why having a subject slope for Type does not make sense? Doesn't it make sense to allow for the fact that the effect of Type might be different for each participant? Perhaps this was an issue with the simulated data, but imagine a study in which participants respond to either red or blue circles (making Type a colour factor), woudl it not make sense to allow for the effect of colour to vary by person? Could you also elaborate on why nesting session in participant doesn't make sense?
– Dave
Oct 25, 2019 at 22:28
• @Dave, that's right, it is simply that in this (simulated) case it does not make sense to fit random slopes fro Type. With real data it very well might. As for nesting in Participant, I assume you are referring to session, which is a fixed effect, not random. Oct 26, 2019 at 6:36