Skip to content
Prev 13936 / 20628 Next

Simulating Data from a Linear Mixed Model

lme4 has gotten much better at simulating since 2007; there's now a built-in
simulation mechanism.

----------
dd <- expand.grid(item=factor(1:10),subj=factor(1:30),condition=factor(1:2))
form <- log.rt ~ condition + (1+condition|item) + (1+condition|subj)

set.seed(101)

library("lme4")

dd$log.rt <- simulate(form[-2], newdata=dd,
                newparams=list(beta=rep(0,2),
                theta=rep(1,6), ## length = 2 * (2*(2+1)/2)
                sigma=1),
                family=gaussian)[[1]]

ff <- lFormula(form,data=dd)
names(ff$reTrms$Ztlist) ## subj comes before item ...
m <- lmer(form,data=dd)

The hardest part is figuring out the proper order/configuration
of the theta (Cholesky factor) parameters (column wise/lower triangular/
subject first, then item).