# [R] mixed model

Douglas Bates bates at stat.wisc.edu
Wed May 25 18:06:27 CEST 2005

```NATALIA F TCHETCHERINA wrote:
>  Hello all,
>  I have problem with setting up random effects.
>  I have a model:
>  y=x1+x2+x1*x2+z1+z1*x2
>  where x1, x2, x1*x2 are fixed effects
>  and z1, z1*x2 are random effects (crossed effects)
>  I use library(nlme) 'lme' function.
>  My question is: how I should set up random effects?
>  I did
>  lme(y~x1+x2+x1:x2, data=DATA, random=~z1+z1:x2, na.action='na.omit')
>  but it did not work.
>
>  Sincerely, Natalia.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help

The answer will depend on the types of x1, x2 and z1 (i.e. whether each
of them is numeric or a factor).  Because you use x1:x2 I will assume
that x1, x2 and z1 are all factors.  In that case the formula term x1*x2
is equivalent to x1 + x2 + x1:x2 and you could write the call to lme as

lme(y ~ x1*x2, data = DATA, random = ~1|z1/x1)

For lmer from the lme4 package it would be

lmer(y ~ x1*x2 + (1|z1) + (1|z1:x1), data = DATA)

```