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.
If instead we specified:
(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.