# Mixed effect linear modeling with multiple nested effect variables

I am trying to develop a linear mixed-effect model for a dataset with a somewhat complex nested structure. The scenario is roughly the following:

I am measuring the attention level of students at different points in a school year. The highest level of my hierarchy is District, which is marked as either Liberal (L) or Conservative (C). Each district has several schools, and each school has multiple teachers and students. Say a student $$i$$ takes classes by several teachers in the school year; I measure the attention level of a student $$i$$ with teacher $$j$$ somewhere between 5-10 times in the school year (the number of times differs for each student). Every student and teacher additionally self-reports their gender (assume we have just two levels, M and F).

I am now interested in looking at whether attention (numeric) is dependent on the genders of the student and teacher, as well as how this relation may be moderated by the district type (L or C). I also want to control for the variation within students, teachers, schools and districts.

I have my fixed effects structure set up as $$districtType * studentGender * teacherGender$$.

For my random effects, I initially started off with having each of the four variables as separate effects: $$(1|districtId) + (1|schoolId) + (1|studentId) + (1|teacherId)$$.

However, the data does follow a clear hierarchical structure, so I figure I should have my random effects nested: $$(1|districtId/schoolId/studentId)$$, and also $$(1|districtId/schoolId/teacherId)$$.

I am confused with this last step -- both teachers and students are nested under a particular school, but each student can interact with multiple teachers and vice versa (crossed??). What would be the best way to include this random effect?

An additional thought is how much the random effect specification even matters -- is it possible that the different ways of specifying them will affect the fixed effect associations of my model?

Terms of the form (1|A/B/C) are internally expanded to (1|A) + (1|A:B) + (1|A:B:C); if the lower-level labels are unique, then A:B and B are equivalent (for typical R packages). For example, as long as student and teacher IDs are unique (e.g. "student 1 in district 1/school 1" has a different label from "student 1 in district 1/school 2"), then districtId:schoolId:studentId is equivalent to studentId. You should probably you should not use both nested terms

(1|districtId/schoolId/studentId) + (1|districtId/schoolId/schoolId)


as that would lead to duplicated (1|districtId) + (1|districtId:schoolId) terms, which might not break the model but would be confusing.

All of the following are equivalent:

(1|districtId/schoolId/studentId) + (1|districtId:schoolId:teacherId)


and

(1|districtId) + 1(districtId:schoolId) +
(1|districtId:schoolId:studentId) + (1|districtId:schoolId:teacherId)


and (if lower-level factors are uniquely labeled)

(1|districtId) + 1(schoolId) + (1|studentId) + (1|:teacherId)


As long as you have unique labels and your specification is not redundant, however, the way you specify the model shouldn't matter.

In theory (1|districtId/schoolId/(studentId + teacherId)) makes sense, but I don't know if it actually works with the current formula parsing machinery.

Another explanation of unique labeling from Bolker (2015):

Most of the software that can handle both crossed and nested random effects can automatically detect when a nested model is appropriate, provided that the levels of the nested factor are uniquely labeled. That is, the software can only tell individuals are nested if they are labeled as A1, A2, . . . , A10, B1, B2, . . . B10, . . . If individuals are instead identified only as 1, 2, . . . 10 in each of species A, B, and C, the software can’t tell that individual #1 of species A is not related to individual #1 of species B. In this case you can specify nesting explicitly, but it is safer to label the nested individuals uniquely.

Bolker, Benjamin M. 2015. “Generalized Linear Mixed Models.” In Ecological Statistics: Contemporary Theory and Application, edited by Gordon A. Fox, Simoneta Negrete-Yankelevich, and Vinicio J. Sosa. Oxford University Press.

• Having trouble editing; deleted my old answer and posted this (approximately equivalent but hopefully improved) version. Oct 10, 2023 at 18:12
• That was really helpful, thanks so much! All my lower level labels are unique, so I will go with the individual random effects specification which looks cleaner to me. Oct 10, 2023 at 20:33
• If this solved your problem you are encouraged to click the check-mark to accept it ... Oct 10, 2023 at 21:32