# Why do we do crossed vs. nested vs. other random effects?

Let's try a theoretical example. I am trying to predict the math scores of students within schools. I see three ways I can model this with random effects:

(1) I can "nest" the random effects. My understanding is that this would mean estimating a school random effect and then a random effect for the interaction between student and school.

(2) I can "cross" the random effects. My understanding is that I would estimate a school random effect and a student random effect.

(3) I can do a weird combination of both. I can estimate a school random effect, and then have a separate student random effect estimated for each school. For example, if I have 10 schools and 20 students per schools I would estimate 1 random effect for school + 10 random effects (one for each school).

How do I choose? Does #3 even make sense (does it imply that school isn't actually a random effect)?

• Does this help ? Commented Apr 2, 2019 at 7:57
• @RobertLong Thanks Robert! I read that (it motivated my example) but I feel like I still don't really understand what going on "behind the scenes" to make (1) and (2) different. Commented Apr 2, 2019 at 16:16
• If a factor is nested, then for any particular lower level unit you can identify which upper level unit it "belongs" to uniquely. If it belongs to more than 1 upper level unit, then it is crossed. The only ambiguity arises when the lower level factors are not coded uniquely, but this is easily handled, as in the example in that answer. Your third case doesn't make sense to me. Nesting/crossing is a property of the experimental design/data and not a modelling decision. Commented Apr 2, 2019 at 18:38

Nesting or crossing is a property of the experimental design, and therefore the data. It is not a modelling decision.

Nesting occurs when a particular level of a factor "belongs" to a unique level of an upper factor. Consider classes within schools. Let's take one particular school, School 1 with a SchoolID = 1. Then let's take a class within that school, with a ClassID = 7A. This class is unique to the school, and is therefore nested - because this particular class (ClassID = 7A) "belongs" to School 1 with no ambiguity.

If we now introduce a new school, school 2, with SchoolID = 2, then it may also have a class with a class with ClassID = 7A (a very common occurrence). Now we have a small problem. We know that class 7A in school 1 is not the same class as class 7A in school 2 - they contain different pupils at a different school. But on the face of it, without understanding the experimental/study design, we would not know that. If we treated class 7A as the same class in both schools, we would fit crossed random effects, and that would be a mistake.

To resolve this ambiguity, we simply form unique IDs for the classes, so class 7A in school 1 could be coded 1.7A, and class 7A in school 2 could be coded 2.7A. Now each class belongs to one and only 1 school, and so the nesting is explicit.

In lme4, we can fit a nested model to these data, and obtain the same output even when he nesting is not explicit. To do so when the nesting is not explicit, then we need to specify the random intercept as:

(1 | SchoolID/ClassID)


or equivalently:

    (1 | SchoolID) + (1 | SchoolID:ClassID)


that is, random intercepts for school and random intercepts for the interaction between school and class.

(1 | SchoolID) + (1 | ClassID)


we would be making a mistake because this would fit crossed random effects - that is, lme4 would assume the class 7A in school 1 is the same unit as class 7A in school 2, which, from the study design we know is wrong.

When the factors are coded uniquely, as in 1.7A and 2.7A then the nesting is explicit and we can specify the random effects as any other these:

(1 | SchoolID/ClassID)
(1 | SchoolID) + (1 | SchoolID:ClassID)
(1 | SchoolID) + (1 | ClassID)


Now, in a situation where there is a crossed structure, for example, consider an experiment were participants (SubjectID) perform a various tasks (taskID), and let's also assume that all participants perform all tasks. In this case we cannot say tha a particular participant belongs to a particular task - because they performed all tasks, ad vice-versa. This in example of a fully crossed design, and the random intercepts would simple be fitted for each factor:

(1 | SchoolID) + (1 | ClassID)


To sum up, it is very important to understand the experimental, study, or survey design, because this and this alone determines the random effects structure that should be used.

• Thanks Robert. My confusion though is in the difference between what is estimated (rather than what is specified). Why are we estimating different things in either model? Commented Apr 2, 2019 at 21:25
• Great explanation! I have a follow up question, what would be the implications of (1 | SchoolID) VS (1 / SchoolID)?? Both run in glmmTMB with slightly different outcome but I don't understand the fundamental implications of each. Commented Sep 21, 2021 at 0:45
• When we use random effects, I believe we are saying we want to predict for a 'typical' school and classroom, not just these specific ones. @RobertLong, my own question is, if we're interested in, say, schoolID as a fixed effect, do we then also need one of the random effects of schoolID to represent this nested or crossed design?
– Nate
Commented Nov 8, 2023 at 2:17
• @Nate if you fit schoolID as a fixed effect then you wouldn't fit as random, so you would just be left with classID as the only random effect Commented Nov 8, 2023 at 15:41
• @Nate, yes, I think that is correct! Commented Nov 8, 2023 at 15:58