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.
item
included as a random effect? Why issession
included as both fixed and random? $\endgroup$