Consider the scenario in which a dataset has two grouping variables (say group 1 and group 2), and a time variable $x$. I would like to understand the difference between the following two models:

m1: lmer(y ~ fixed_component + ( 1 + x | g1 ) + ( 1 + x | g2 )
m2: lmer(y ~ fixed_component + ( 1 + x | g1 ) + ( 1 + x | g2:g1 ) 

For the first one, the statistical model implied is:

$$ y_{ijk} = \text{fixed component} + (u_{jI} + v_{kI}) + (u_{jS} + v_{kS})x_i + \text{error} $$ where $x_i$ is the time variable for the $i$-th observation, for group 1 $=j$ and group 2 $=k$, and where $I,S$ denote intercept and slope random effects respectively, and where $$ \begin{bmatrix}u_{jI} \\ u_{jS}\end{bmatrix} \sim N \left ( \begin{bmatrix}0 \\ 0\end{bmatrix} , \begin{bmatrix}\sigma^2_{uI} & \rho_U\sigma_{uI} \sigma_{uS} \\ \rho_U\sigma_{uI} \sigma_{uS} & \sigma^2_{uS}\end{bmatrix} \right ) $$ $$ \begin{bmatrix}v_{kI} \\ v_{kS}\end{bmatrix} \sim N \left ( \begin{bmatrix}0 \\ 0\end{bmatrix} , \begin{bmatrix}\sigma^2_{vI} & \rho_V\sigma_{vI} \sigma_{vS} \\ \rho_V\sigma_{vI} \sigma_{vS} & \sigma^2_{vS}\end{bmatrix} \right ) $$ where the two vectors are independent.

I am confused about what statistical model we have for the second lmer formulation though, since in that model we restrict the second random effects to model the residuals remaining after the first group is accounted for.

edit: Assume that the nested group is not coded uniquely. So e.g. group1 is school1, school2, school3,.., and group2 is class1, class2, class3,...

I know that the second model will be

$$ y_{ijk} = \text{fixed component} + (u_{jI} + v_{jkI}) + (u_{jS} + v_{jkS})x_i + \text{error} $$ and I know that the distribution of $[u_{jI}, u_{jS}]^T$ will not differ, but what about the distribution of $[v_{jkI}, v_{jkS}]^T$ ?


1 Answer 1


It depends on the study design and on how the data are encoded.

Generally speaking, in the first model we have an intercept varying within g1 and g2, while in the 2nd model we have an intercept varying within g1, and g2 varying within g1. The second formulation is typically used for nested factors, where levels of g2 appear in 1 and only 1 level of g1. An example of this would be students nested within schools. Each student "belongs" to one and only one school.

The first formulation is typically used when we have crossed factors, where individual obervations are associated with all levels of both factors (fully crossed in that case). An example of this would be students and exam questions. All students answer all questions on the exam, and all questions are answered by all students.

In terms of the data, for a nested study, when the lower level factors are coded uniquely, then the two formulations will be equivalent. For example, with students nested within schools, of students are not coded uniquely. Consider two students in different schools. If both students had the same ID, say student1 then it is necessary to use the second formulation, but if the students are coded unuquely, say student1-1 and student1-2, then the two formulations are equivalent.

  • $\begingroup$ thanks! I should have clarified that we have the coding group 1: school1, school2, ..., and group 2: class1, class2,... so they are not coded uniquely. I guess my question is more to do with the distribution of the random effects in the second case. Does the distribution of the random effect for class depend on that of the random effect for school? I've added some clarifying remarks at the end of my question $\endgroup$ Commented Sep 20, 2020 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.