# Modelling longitudinal data with crossed random effects

by Dave   Last Updated October 18, 2019 18:19 PM - source

Let's say I have 40 participants. They are each measured three times. On each visit they see ten stimuli, which can be one of two types. 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.

Thanks!

``````require(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 <- as.factor(data$$participant)
data$$session <- as.factor(data$$session)
data$$item <- as.factor(data$$item)
data$$type <- as.factor(data$$type)

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