Why Coding Matters
A nested random effect can be written using a hierarchy, such as classrooms within schools, or by giving every classroom a label that identifies both its school and its classroom number. These are two ways to describe the same grouping structure, provided the labels assign observations to exactly the same groups. A change in notation alone should not change the model; a change in membership can.
This distinction is especially important when child-level labels are reused. If each school numbers its classrooms 1, 2, and 3, “classroom 1” is not one group across the whole dataset. It refers to a different classroom in each school. An explicit label must therefore include enough information to distinguish each school–classroom pair.
Two Notations for One Nested Structure
Consider a GLMM with a random intercept for each parent and a random intercept for each child within its parent. A standard model specification is
Here \(g\) is the link function, \(\mathbf{x}_i^\mathsf{T}\boldsymbol{\beta}\) is the fixed-effects predictor, \(b_g\) is the parent random intercept, and \(u_{g,h}\) is the child-within-parent random intercept. For the usual random-intercept specification, assume \(b_g\sim N(0,\tau_P^2)\) and \(u_{g,h}\sim N(0,\tau_C^2)\), with effects independent across groups and between levels. Responses are conditionally independent given the random effects and fixed predictors, and their conditional distributions belong to the chosen GLM family \(\mathcal{F}\).
The same structure can be written by creating a unique child-group label for every parent–child pair. Call this label \(q_i=(g(i),h(i))\). The model then uses one parent grouping factor and one explicit child-pair grouping factor. In common mixed-model formula syntax, the hierarchical expression \((1\mid P/C)\) expands to \((1\mid P)+(1\mid P:C)\). The interaction factor \(P:C\) is the explicit pair label; it is a random grouping term here, not a fixed-effects interaction.
The translation is valid if and only if the parent factor assigns observations to the same parents in both specifications, and the explicit child-pair factor assigns observations to the same parent–child groups as the hierarchical notation. Matching names are not required; matching memberships are.
Why Matching Memberships Preserves the Model
A grouping factor can be represented by a membership matrix: each row is an observation, each column is a group, and a row has a 1 in the column for its group and 0 elsewhere. Let \(Z_P\) be the parent membership matrix and \(Z_C\) the child-within-parent membership matrix. The random part of the linear predictor is \(Z_P\mathbf{b}+Z_C\mathbf{u}\). Replacing a hierarchical child index by explicit pair labels merely reorders the columns of \(Z_C\), if and only if the groups are identical.
With independent Gaussian random intercepts, the covariance of the latent linear predictor therefore depends on whether each pair of observations shares a parent and whether it shares a child within that parent:
where \(\eta_i=\mathbf{x}_i^\mathsf{T}\boldsymbol{\beta}+b_{g(i)}+u_{g(i),h(i)}\). Equal membership matrices produce equal covariance matrices and the same conditional linear predictors. For non-Gaussian responses this is a statement about the latent linear predictor; it does not claim that the marginal response covariance has this same simple form.
The justification is direct: the covariance of a sum of independent random effects is the sum of their covariances. A parent effect contributes \(\tau_P^2\) to the covariance when two observations share a parent and zero otherwise. A child effect contributes \(\tau_C^2\) when they share the same child group and zero otherwise. If the explicit labels encode exactly the same child groups, both indicators agree for every pair \(i,j\), so the covariance entries agree. The fixed-effects predictor and conditional response family have not changed either.
Worked Example: Checking Pair Labels With a Membership Table
Suppose a hypothetical dataset has observations from three classrooms. Schools A and B each contain classrooms 1 and 2, while school C contains classroom 1 only. Two observations come from A1, one from A2, and two from B1. The labels 1 and 2 are local classroom numbers, so the explicit child labels must include the school.
| Observation | School | Local classroom | Explicit child label |
|---|---|---|---|
| 1 | A | 1 | A:1 |
| 2 | A | 1 | A:1 |
| 3 | A | 2 | A:2 |
| 4 | B | 1 | B:1 |
| 5 | B | 1 | B:1 |
The hierarchical child memberships are \(\{1,2\}\) in A1, \(\{3\}\) in A2, and \(\{4,5\}\) in B1. The explicit labels give exactly those sets: A:1 identifies observations 1 and 2, A:2 identifies observation 3, and B:1 identifies observations 4 and 5. Thus the membership columns match, up to their names.
For illustration, let the parent and child variance components be \(\tau_P^2=0.36\) and \(\tau_C^2=0.16\). Observations 1 and 2 share both school and classroom, so their latent-predictor covariance is \(0.36+0.16=0.52\). Observations 1 and 3 share school A but not classroom, giving covariance \(0.36\). Observations 1 and 4 share neither parent nor child group, giving covariance \(0\). The hierarchical and explicit encodings give these same values because their memberships agree.
Building and Verifying Explicit Labels
A safe label is a collision-free representation of the tuple \((\text{parent},\text{child})\). A data system may store that tuple as two columns, or encode it as one unique identifier. If combining text strings, use an unambiguous delimiter or an encoding that cannot turn two different pairs into the same string. For instance, naive concatenation can make distinct pairs difficult to distinguish if the component labels themselves contain the delimiter or have variable lengths.
A reliable check is to compare the partition of observations induced by each coding. Two observations belong to the same child group under one encoding exactly when they belong to the same child group under the other. This pairwise check is stronger than checking that the two columns have the same number of distinct levels: two factors can have the same number of levels while assigning observations differently.
For every observation, record its parent label and its child label within that parent.
Create one unique group identifier for each observed parent–child pair, retaining the parent term as a separate grouping factor when the model includes a parent random effect.
Check that each pair of observations agrees on whether it shares a parent and whether it shares a child group under both encodings.
Verify that the parent and child random terms, their assumed distributions, and all fixed-effects terms are unchanged. A matching child label alone is not enough if a term was added, dropped, or redefined.
Worked Example: Why Reused Child Labels Can Change the Model
Suppose a hypothetical dataset has two schools, East and West. Each has a classroom numbered 1, with two observations in each classroom. Under the hierarchy, the groups are East:1 and West:1. An explicit child factor coded only as “1” assigns all four observations to one group instead.
Under the correct nested encoding, two observations from East:1 share both the school and classroom effects. An East:1 observation and a West:1 observation share neither. If \(\tau_P^2=0.25\) and \(\tau_C^2=0.09\), the first pair has latent-predictor covariance \(0.25+0.09=0.34\), while the cross-school pair has covariance \(0\).
With the incorrect child-only label, the East and West observations are assigned to the same child group. They would then share the child effect and, because “1” has been treated as one group, acquire a child covariance contribution of \(0.09\). That is not a harmless relabeling: it changes the membership matrix and the covariance structure. The explicit labels East:1 and West:1 restore the intended nested grouping.
Worked Example: Confirming an Equivalent GLMM Predictor
Consider a hypothetical binary response with a logit link and the nested random-intercept structure above. For a person in school A, classroom 2, suppose the fixed-effects part is \(-1.1+0.5x\), with \(x=2\). Suppose the parent random intercept is \(b_A=0.3\), and the classroom random intercept is \(u_{A,2}=-0.2\). These values are illustrative conditional effects, not estimates from a real study.
Using hierarchical notation, the linear predictor is
The conditional probability is \(\operatorname{logit}^{-1}(0)=1/(1+\exp(0))=0.5\). With explicit labels, the same observation has parent label A and child-pair label A:2. The same random effects, \(b_A=0.3\) and \(u_{A:2}=-0.2\), enter the predictor, so the result remains \(\eta=0\) and the conditional probability remains \(0.5\). If the pair label were mistakenly coded as 2 shared by all schools, the model would instead use a child effect associated with that broader group; it would no longer be the stated nested model.
Choosing Between Notations and Detecting Errors
Hierarchical notation is compact and makes the intended parent–child relationship easy to read. Explicit pair labels can be convenient when constructing design matrices, joining records, or checking group membership in a dataset. Neither notation is inherently a different statistical method. The decisive issue is whether the two specifications imply the same grouping structure and retain the same random-effects assumptions.
| Specification | What it represents | When it is appropriate |
|---|---|---|
| Parent plus nested child, such as \((1\mid P)+(1\mid P:C)\) | A parent effect and a separate child effect for each parent–child pair | When child labels may be local to their parent |
| Parent plus globally unique child identifier | The same nested structure if every child identifier uniquely identifies one parent–child pair | When uniqueness has been verified from the data |
| Parent plus reused child label alone, such as \((1\mid P)+(1\mid C)\) | A child grouping that merges equal labels across parents | Not equivalent to nesting when labels are local and reused |
| One child factor without a parent effect | A child-level random intercept only | Only when that is the intended model, not when a parent effect is also required |
This translation does not turn a nested factor into a crossed factor. It also does not make a fixed-effects interaction equivalent to a random child grouping term: fixed and random terms have different roles and assumptions. If a child truly occurs under more than one parent, a one-parent-per-child nesting representation is not suitable; that situation calls for a different structure rather than a relabeling trick.
Useful checks include tabulating parent–child combinations, confirming that each child pair maps to one parent, and comparing membership columns or pairwise same-group indicators. A mismatch in the number of distinct groups is a warning, but matching counts alone do not establish equivalence. Sparse or unobserved parent–child combinations also require care: the explicit factor should represent observed memberships as intended, not create accidental groups through inconsistent data cleaning.
Common Mistake and Key Takeaway
Incorrect reasoning: “The classroom label is 1 in both specifications, so it is the same classroom group.” This confuses a local category label with a globally unique unit. If each school numbers classrooms independently, the two classrooms labeled 1 are separate children.
Correction: Compare the observations grouped together, not just the spelling of factor labels. A nested child’s identity is the parent–child pair. Use explicit pair labels only when they preserve those memberships, and retain every random term that defines the intended model.
Check Your Understanding
Answer each question, then compare with the solutions below.
- Schools A and B each have a classroom labeled 3. What explicit child labels distinguish the two nested groups?
- Two encodings have the same number of child groups. Is that sufficient to conclude they represent the same nested structure? Why or why not?
- With parent variance \(0.20\) and child variance \(0.08\), what is the latent-predictor covariance for two observations in the same child group? What is it for observations in different children of the same parent?
- A child identifier is globally unique across all parents. Under what condition can it serve as the explicit child grouping factor?
Solutions. (1) Use A:3 and B:3; the parent is part of each child’s identity. (2) No. Equal group counts do not ensure that the same observations share groups. Compare memberships pair by pair, or compare the corresponding membership matrices. (3) In the same child, the covariance is \(0.20+0.08=0.28\). In different children of the same parent, it is \(0.20\), since only the parent effect is shared. (4) It can serve as the child grouping factor if each identifier denotes exactly one parent–child unit, so its induced memberships match the hierarchical child memberships; the parent random term must still be included if the intended model has one.