I have $m=4$ group of mice (i.e group1, group2, group3, group4). Each group has a different number of mice. I measure a parameter $(y)$ on each mice of each group at $l=4$ different states (i.e state1, state2, state3, state4). I would like to build a mixed effect model to analyse the effect of group, state and group*state, allowing for the variability within each mouse and within each group.The mice within $group_{m}$ are labeled with an id (1,2,3...,number of mice of $group_{m}$)

$$y_{mln}=\mu +group_{m} +state_{l} +(group*state)*{ml}+b*{ml}+\varepsilon_{mln}
$$
with $b_{ml}$ the random effect for the nth mouse within $group_{m}$

My data frame has the following variables

```
value (num)
state (factor: 4 levels)
group (factor: 4 levels)
id (within group) (num)
```

Is the corresponding syntax correct?

```
lmer(value~group+state+group*state+(1|group))
```

r
answered 6 years ago smillig #1

I think what you're looking for is

```
lmer(value ~ group*state + (1|group) + (1|id))
```

This model estimates the fixed effect of group and state as well as the interaction between them (`R`

automatically expands `group*state`

to `group + state + group*state`

) and estimates a random intercept for the effect of each group and for each mouse.

answered 6 years ago Thierry #2

You want this

```
mouseID <- interaction(group, ID)
lmer(value ~ group * state + (1|mouseID))
```

The mouseID must be unique for each mouse.

Since group is a factor, you can't have it both in the fixed and the random part. That would lead to an unidentifiable model.