[R-sig-ME] Simulating Data from a Linear Mixed Model

Ben Bolker bbolker at gmail.com
Tue Nov 10 01:57:19 CET 2015


  (please keep r-sig-mixed models in the Cc: line)

On Fri, Nov 6, 2015 at 3:54 AM, Hintz, F. (Florian) <F.Hintz at let.ru.nl> wrote:
> That's great! Thank you very much. I forgot to mention though that the experimental design was such that one subject was presented with one item in either condition 1 or condition 2. Would you know a way how to account for that in the expand.grid command? In the current version the data are simulated such that each 'new' subject has data points for one item in both conditions.

Something like this should do it:

dd <- expand.grid(item=factor(1:10),subj=factor(1:30))
dd <- transform(dd,condition=factor(ifelse(as.numeric(subj<=15,1,2)))

condition=factor(1:2))
>
> Many thanks!
>
> Florian.
>
> -----Original Message-----
> From: Ben Bolker [mailto:bbolker at gmail.com]
> Sent: donderdag 5 november 2015 16:16
> To: Hintz, F. (Florian)
> Cc: r-sig-mixed-models at r-project.org
> Subject: Re: [R-sig-ME] 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).



More information about the R-sig-mixed-models mailing list