# Nested mixed effects with lme4

I am trying to analyze data from an experiment using lme4. In the experiment, subjects saw either dark or bright versions of 50 stimuli (between-subjects; fixed effect "brightness"). All subjects saw the 50 stimuli 3 times in varying durations (within-subjects). A previous post (Mixed Effects Model with Nesting) suggested that the following code is the correct way to deal with the nesting in the data:

lmer(response ~ brightness+duration +(1|subject:duration),REML=FALSE,data=dat)


Is (1|subject:duration) the correct way of specifying the nested data structure (that each subject saw each stimulus in multiple durations)?

Furthermore, a tutorial (http://www.bodowinter.com/tutorial/bw_LME_tutorial2.pdf) recommends the use of random slopes for random factors. Is this appropriate here?

Edit: The factor duration has 3 levels that I compare using Tukey tests

## 1 Answer

I would say

response ~ brightness+duration+(duration|subject)


would probably be a little better. (The simpler (1|duration:subject) model is not necessarily wrong, but might be oversimplified. If I were a peer reviewer of this work I would certainly ask for a justification of the simpler model ...) The (duration|subject) model is a "random-slopes" model, more or less (although if you have coded duration as a categorical (factor or ordered factor) variable the thing that varies randomly among subjects is not a slope per se, but a between-duration difference). The specification you have ((1|subject:duration)) assumes all subject-duration effects are drawn from a single (iid) Normal distribution; (duration|subject) assumes that the duration effects for a single individual are drawn from a $3 \times 3$ multivariate Normal distribution.

More precisely: comparing the random effect specification (1|subject:duration) gives the model for the conditional modes/BLUPs of subject $s$ for duration $d$ (or duration effect $d$, depending on how the model is parameterized) $$b_{sd} \sim \textrm{Normal}(0,\sigma_{sd}^2)$$ whereas (duration|subject) gives

$$\begin{split} b_{s\cdot} & \sim \textrm{MVN}( \mathbf 0,\Sigma) \\ \Sigma & = \left( \begin{array}{ccc} \sigma^2_1 & \sigma_{12} & \sigma_{13} \\ \sigma_{12} & \sigma^2_{2} & \sigma_{23} \\ \sigma_{13} & \sigma_{23} & \sigma^2_3 \\ \end{array} \right) \end{split}$$ i.e., the different duration levels each have different among-subjects variances, and the among-subject variation in different duration levels is correlated ($\Sigma$ is a general symmetric positive (semi)definite matrix). To get back to the previous model you would need to restrict $\sigma_1^2=\sigma_2^2=\sigma_3^2=\sigma_{sd}^2$ and all of the off-diagonal elements would be zero.

• It is unclear to me what your formula exactly specifies, in particular why your suggestion assumes a 3 X 3 normal distribution. – user55987 May 9 '15 at 23:55
• In addition, is the formula I entered above clearly wrong or still acceptable given the experiment design? – user55987 May 9 '15 at 23:58
• In this answer, is the correct lmer() syntax for the simpler model (1|subject:duration) or is it (1|duration:subject)? I see the second version used at the beginning of the answer, but the first version is used later on. – ClarPaul Jan 30 '16 at 21:26
• it doesn't matter. The interaction operator : is commutative, except in some cases for the specific labels that get assigned -- but in this case even the labels are never explicitly shown. – Ben Bolker Jan 30 '16 at 21:34
• however, note that (duration|subject) and (subject|duration) are not equivalent -- only the first is at all sensible – Ben Bolker Jan 31 '16 at 3:14