Why distinguish classes from schools?
Students in the same class share a teacher and classroom environment; students in different classes at the same school may still share school policies and resources. A model that represents only one of these groupings can miss an important part of the dependence in the data. A three-level nested model assigns a separate random intercept to each school and to each class within its school, while the students are the level-1 observational units.
The nesting is substantive: each class belongs to one school, and each student belongs to one class. The model therefore distinguishes variation between schools from additional variation between classes within schools. It does not automatically assign a random intercept to every student. For a single response per student, the student is the observational unit; in a Gaussian model the level-1 residual accounts for remaining variation, while in a binary model the conditional response distribution accounts for it.
Model specification and assumptions
For an outcome from an exponential-family distribution, write \(\mu_{ijk}=E(Y_{ijk}\mid u_k,v_{jk},x_{ijk})\). A basic three-level generalized linear mixed model is
Here \(g\) is the link function, \(\beta_0\) and \(\beta_1\) are fixed effects, and \(x_{ijk}\) is a predictor whose value may differ among students. The school effect shifts the linear predictor for the whole school; the class effect adds a further shift for one class within that school. The model commonly assumes that school and class effects are mutually independent, that the class effects are independent across classes, and that the random effects are independent of the predictors. Conditional on the random effects and predictors, responses are assumed independent and to follow the specified response distribution.
For a binary response, a common choice is the Bernoulli distribution with a logit link. For a count response, a Poisson distribution with a log link is one possible choice. For a continuous response, the Gaussian identity-link model is a special case; it usually includes a student-level residual, such as \(\epsilon_{ijk}\sim N(0,\sigma^2)\), independent of the random effects. The response distribution and link should be chosen for the outcome and checked against the data, not inferred from the hierarchy alone.
The random-effects formulation treats the observed schools and classes as realizations from populations of schools and classes represented by the model. If the scientific target is only a fixed, specifically enumerated set of schools, treating school effects as fixed may instead be appropriate. The design must also genuinely support nesting. If a class crosses school boundaries, or a student belongs to multiple classes in the relevant data structure, this simple nested model does not represent the memberships correctly.
Worked Example: Building the hierarchy for test scores
Suppose a hypothetical data set records one end-of-term score for each student. There are 4 schools, 3 classes per school, and 20 students per class. Consider a Gaussian model in which \(x_{ijk}\) is the student's study hours, and suppose the fitted fixed effects are \(\hat{\beta}_0=70\) and \(\hat{\beta}_1=2\) score points per study hour.
A suitable specification is \(Y_{ijk}=\beta_0+\beta_1x_{ijk}+u_k+v_{jk}+\epsilon_{ijk}\). The 4 school effects \(u_k\) each apply to all 60 students in one school. The 12 class effects \(v_{jk}\) each apply to the 20 students in that class. There are \(4\times3\times20=240\) student responses.
Assume, for illustration, that the fitted school and class effects for one student are \(\hat u_k=2\) and \(\hat v_{jk}=-1\), and that the student studied for \(x_{ijk}=5\) hours. The fitted conditional mean is
This is the model's fitted mean for a student with those predictor and group-effect values. The school contributes a 2-point upward shift and the class contributes a 1-point downward shift relative to the fixed-effects part. Neither shift is a student-specific random intercept.
Interpreting the effects on the link scale
A random intercept is an additive shift on the linear-predictor scale, not necessarily on the response scale. With an identity link, a one-unit shift in a random intercept changes the conditional mean by one response unit. With a logit link, it changes the log odds by one unit; the corresponding odds are multiplied by the exponential of that shift. With a log link, it changes the log mean and therefore multiplies the conditional mean by the exponential of the shift.
The fixed intercept \(\beta_0\) is the linear predictor for \(x=0\) when both random effects equal zero. It is not generally the overall population-average response. In nonlinear-link models, applying the inverse link to a predictor with random effects set to zero does not usually give the average response after integrating over the random-effects distributions.
Worked Example: Conditional probability for a student
Suppose a hypothetical binary outcome indicates whether a student meets a specified proficiency standard. A fitted logistic model has intercept \(\hat\beta_0=-1.5\), tutoring coefficient \(\hat\beta_1=0.6\), and tutoring indicator \(x_{ijk}=1\) for a tutored student. Suppose the student's school and class effects are \(\hat u_k=0.3\) and \(\hat v_{jk}=-0.4\).
The fitted linear predictor is
Applying the inverse-logit function gives the conditional fitted probability:
Thus, under the fitted model, the conditional probability for a student with these specified effects and tutoring status is about 0.269, or 26.9%. The tutoring coefficient corresponds to a conditional odds ratio of \(\exp(0.6)\approx1.822\), holding the school and class effects fixed. It does not by itself describe a causal effect, nor is this conditional probability the probability averaged over all schools and classes.
Why observations share covariance
The hierarchy induces dependence because two students in the same class share both random intercepts, while students in different classes of the same school share only the school intercept. The following calculation makes the unit represented by each effect explicit. In a Gaussian identity-link model with no predictors for simplicity, write \(Y_{ijk}=\beta_0+u_k+v_{jk}+\epsilon_{ijk}\), with independent mean-zero effects and residuals, and variances \(\tau^2_{\text{school}},\tau^2_{\text{class}},\sigma^2\).
For distinct students \(a\) and \(b\), conditional residuals are independent. In the covariance, only random effects shared by both responses contribute: the covariance of a random effect with itself is its variance, while independent effects have covariance zero. Therefore,
For one response, the variance is \(\tau^2_{\text{school}}+\tau^2_{\text{class}}+\sigma^2\), because all three independent components contribute. This pattern is a consequence of the specified random-intercept structure; it is not a general guarantee that all students within a school are equally alike in every respect.
Worked Example: Reading covariance from group membership
For a hypothetical Gaussian score model, suppose the school-intercept variance is \(4\) score-points-squared, the class-intercept variance is \(9\) score-points-squared, and the student-level residual variance is \(25\) score-points-squared. Use the nested random-intercept assumptions above.
Two distinct students in the same class share both the school and class effects, so their covariance is \(4+9=13\) score-points-squared. Two students in different classes of the same school share only the school effect, so their covariance is \(4\) score-points-squared. Students in different schools share neither effect, so their covariance under this model is \(0\).
For an individual student's response, the variance is \(4+9+25=38\) score-points-squared. The residual contributes to an individual response's variance but not to the covariance between two distinct students under the conditional independence assumption. These calculations illustrate how the random-intercept labels map onto the actual sharing of school and class membership.
Model checks, alternatives, and limitations
Before fitting the model, verify the data structure: each student should map to one class and each class to one school for this formulation. Check that class identifiers are unique within schools, or create a combined school-class identifier. Otherwise, a class label reused in two schools could mistakenly be treated as one shared class effect. Also check whether the number of schools and classes provides meaningful information for estimating their respective variance components. Many students in only a few schools do not replace having information across more schools.
After fitting, inspect convergence warnings, estimated variance components, and whether the model's conditional response distribution and link are plausible for the outcome. A variance estimate near zero can indicate little detectable variation at that level, but it can also arise when there is limited information to estimate that component. A very small number of groups, sparse binary outcomes, or separation can make estimates unstable. Residual and predictive checks should be conducted at relevant levels, since a model that fits student-level patterns poorly may also misrepresent group-level patterns.
The main alternatives answer different questions. A model with school effects but no class effects assumes there is no additional class-level variation after accounting for school and predictors. A model with only class effects does not represent the fact that distinct classes from one school may share a school-level shift. Treating every school and class as fixed effects can be useful when interest is restricted to the observed groups, but it does not provide the same population distribution of group effects. An ordinary GLM without group effects ignores the dependence represented here and may give misleading uncertainty estimates. These approaches are not interchangeable merely because they use the same response and predictors.
The random-intercept model also has limits. It assumes a common school shift across classes and a common class shift across students, on the link scale. It does not allow a predictor's association to vary across schools or classes; that requires a different random-effects structure. The independence assumptions may also be inappropriate if group effects relate systematically to predictors. A good fit to the nesting labels alone does not establish that these assumptions hold.
Check Your Understanding
Answer each question using the school-class-student indexing in this tutorial. Brief solutions follow.
- Which students share both random intercepts: students in different classes of one school, or students in the same class?
- A fitted logit-scale school effect is \(-0.2\). What is its multiplicative effect on the conditional odds?
- Two students attend different classes in the same school. Under the independent random-intercept model, which random effect contributes to their covariance?
- Why is a class identifier that is reused across schools potentially problematic?
Solutions. (1) Students in the same class share both \(u_k\) and \(v_{jk}\); students in different classes of one school share only \(u_k\). (2) The conditional odds are multiplied by \(\exp(-0.2)\approx0.8187\), a decrease of about 18.1% relative to otherwise identical odds with school effect zero. (3) Only the school effect contributes, so the covariance is \(\tau^2_{\text{school}}\). (4) If the model treats that reused label as one class, it incorrectly assigns the same class random intercept to classes that belong to different schools; use an identifier that distinguishes classes within their schools.