# Lmer model syntax for a combination of crossed and nested random effects

I'm trying to use the lmer() function in R to specify a particular random effects structure for a model that has four levels: each measurement on a students occurs in one or more groups, and each group occurs in one of several districts.

The structure of the data is such that I have a combination of nested and crossed random effects:

1. Groups are nested in districts
2. Students are crossed with groups
3. Students are nested in districts
4. Students can contribute a data point to more than one group

In other words, a specific student can occur in more than one group within the same district (e.g. Student S2 in the graph below occurs in Group 1 and Group 2), but not in more than one district (Student S2 only ever occurs in District 1). A specific group occurs in only one district (e.g. Group 4 only ever occurs in District 2).

I know how to specify a 2-level model with crossed or nested effects. For instance, if I wanted to specify random intercepts:

In a 2-level model with crossed effects district and group, I would use

(1 | district) + (1 | group)


In a 2-level model with group nested within district, I would use

(1 | district/group)


But how do I specify the combination of crossed and nested effects outlined for my 4-level model above, and in the graph below? I'm not sure how to translate all the dependencies into the correct lmer() model syntax.

UPDATE: I left out some important details about data at the student level:

1. Within each group, there is one data point per student in that group
2. 95% of the students are associated with only one group (that is, they contribute one data point to the analysis)
3. 5% of the students are associated with more than one group (usually, with 2 groups and at most with 3 groups); they contribute multiple data points to the analysis
4. Of those 5%, about half contribute the same measurement (that is, the same values for the predictors and dependent variable) as a data point in more than one group
5. The other half of those 5% do not contribute the same measurement across different groups. That is, the same student contributes (partly) different values for the predictors and a different value for the dependent variable across groups

Note here that you don't have crossed random effects. Here group is fully nested within district and

(1 | district) + (1 | group)


is equivalent to

(1 | district/group)


because

(1 | district/group)


is the same as

(1 | district) + (1 | group:district )


and

(1 | group:district )


is the same as

 (1 | group)


because group is coded uniquely across district.

So there is no crossed random effects here.

What you have is multiple membership where students can simultaneously belong to more than 1 group, it doesn't have anything to do with crossed or nested random effects.

• I'm not sure I understand what you say about the students. If I don't model student as part of the random effects structure, the model will treat a data point for S2 in Group 1 as independent from a data point for S2 in Group 2. Sep 10, 2020 at 13:24
• Wait, so you are saying you have repeated measures within students ? That's not at all clear from the question. In that case you need (1 | district) + (1 | group) + (1 | student) where all are coded uniquely - in that case you have partially crossed random effects for students and groups, and that would be a 4-level model, not 3-level. I thought from your question that you had a single measure for each student, but a student could belong to more than 1 group (which would be multiple membership) Sep 10, 2020 at 13:28
• You are right, it is a 4-level model. I've updated the OP to clarify that. If I understand correctly, (1 | district) + (1 | group) + (1 | student) will yield the right random effects structure as long as each district, each group and each student is coded uniquely. (Importantly, data points associated with the same student across groups should NOT have student coded uniquely; they should have the same value for student.) This is how my data is currently coded. Sep 10, 2020 at 14:55
• Yes, lme4 will handle the partially crossed/nested structure, provided that you code the factors in the way you have in the diagram in the question. Sep 11, 2020 at 8:02
• @ Robert Long Hi, here is my new question post: stats.stackexchange.com/questions/529436/…, many thanks in advance! Jun 5, 2021 at 7:14