Tutorials › Generalized Linear Mixed Models (GLMM) › Building Models With Mixed Nested and Crossed Effects

Crossed and Nested Random Effects · Tutorial 484 of 698

Building Models With Mixed Nested and Crossed Effects

Build a mixed nested-and-crossed random-effects structure by matching each term to the way units are sampled and observed.

Advanced 11 min read

What You'll Learn

  • Distinguish patients nested within clinics from clinicians who appear in multiple clinics.
  • Index repeated observations without confusing a patient’s clinic with a clinician’s identity.
  • Write a conditional GLMM that includes nested and crossed random intercepts.
  • Compute a fitted conditional probability from specified fixed and random effects.
  • Diagnose when a proposed grouping structure conflicts with the observed design.
  • Compare crossed-and-nested specifications with simpler alternatives and identify their limitations.

Why a Model May Need Both Structures

A study can contain more than one kind of grouping. Patients may belong to a single clinic, so patients are nested within clinics. Clinicians, however, may see patients in several clinics, so clinician identity crosses clinic membership. A model that treats every grouping factor as nested can split one clinician into several artificial clinic-specific identities. A model that omits patient or clinic grouping can overlook repeated or shared structure in the observations.

The key is to describe the data-generating design before writing the random-effects terms. Identify the unit attached to each observation, which group each unit belongs to, and whether a factor’s levels occur in only one level or in multiple levels of another factor. The model then encodes these relationships while allowing the outcome distribution and link to suit the response.

Definition: A factor is nested within another factor when each of its levels belongs to exactly one level of that other factor. A factor is crossed with another factor when its levels occur in multiple levels of the other factor. A mixed nested-and-crossed model includes random effects for both kinds of grouping, with the same factor retaining the same identity wherever it appears.

Index the Units Before Writing the Model

Suppose clinics are indexed by \(k\), patients within clinic \(k\) by \(j\), and repeated observations on a patient by \(t\). Let \(l(k,j,t)\) denote the clinician who provides the care recorded at that observation. The notation matters: \(j\) is a within-clinic index, whereas \(l\) is a clinician’s global identifier. If clinician 12 works at two clinics, the clinician is still clinician 12 at both.

For a binary outcome, such as whether a hypothetical patient has a specified outcome by a follow-up visit, one possible model is:

$$ Y_{kjt}\mid u_k,v_{jk},a_{l(k,j,t)} \sim \operatorname{Bernoulli}(p_{kjt}), \qquad \operatorname{logit}(p_{kjt}) = \beta_0+\beta_1 x_{kjt}+u_k+v_{jk}+a_{l(k,j,t)}. $$

Here \(u_k\) is a clinic random intercept, \(v_{jk}\) is a patient random intercept nested within clinic, and \(a_l\) is a clinician random intercept crossed with clinic. The fixed effects \(\beta_0\) and \(\beta_1\) describe the population-level intercept and the association with covariate \(x\). The logit link is \(\operatorname{logit}(p)=\log\{p/(1-p)\}\).

For this example, assume \(u_k\sim N(0,\sigma^2_{\text{clinic}})\), \(v_{jk}\sim N(0,\sigma^2_{\text{patient}})\), and \(a_l\sim N(0,\sigma^2_{\text{clinician}})\). Assume the three sets of random effects are mutually independent, and that observations are conditionally independent given the random effects and covariates. These are modeling assumptions, not consequences of nesting or crossing. Another application may require a different distribution, link, random-effects dependence structure, or within-unit conditional distribution.

The term \(v_{jk}\) uses the patient’s identity within clinic. It must not be replaced by an effect indexed only by \(j\) if patient labels are reused across clinics. Conversely, the clinician term uses the same \(a_l\) for every observation involving clinician \(l\), regardless of clinic. This distinction is the core of the specification.

Worked Specifications and Calculations

Worked Example: Patients Nested Within Clinics, Clinicians Crossed Across Clinics

Consider a hypothetical study with two clinics. Patient P1 belongs to clinic A and has two visits; clinician C7 sees P1 at the first visit. Patient P2 belongs to clinic A and is seen by C9. Patient P3 belongs to clinic B and is seen by C7. Thus C7 appears in both clinics. The outcome is binary, and \(x\) is a centered baseline score.

Index the records. Denote clinic A by \(k=1\), clinic B by \(k=2\); use patient indices within clinic; and index clinicians globally as C7 and C9. The repeated records for P1 share the same patient index and clinic index. The records involving C7, including those from both clinics, share clinician index C7.

Specify the structure. The model has a clinic term \(u_k\), a nested patient term \(v_{jk}\), and a crossed clinician term \(a_l\). For the C7 record from clinic B, the predictor includes \(u_2+v_{1,2}+a_{\mathrm{C7}}\); for a C7 record from clinic A it includes \(u_1+v_{1,1}+a_{\mathrm{C7}}\). The clinic and patient contributions change with the record’s clinic and patient; C7’s contribution does not.

This specification matches the stated assignment: each patient belongs to one clinic, while C7 is observed in more than one clinic. It would be wrong to index the clinician effect by both clinic and clinician, such as \(a_{kl}\), because that would give C7 separate effects at clinics A and B rather than one shared effect. It would also be wrong to treat P1’s repeated visits as different patients.

Worked Example: Calculate a Conditional Outcome Probability

For one hypothetical observation, suppose \(x=2\), \(\beta_0=-1.20\), \(\beta_1=0.35\), and the specified random-effect values are \(u_k=0.20\), \(v_{jk}=-0.10\), and \(a_l=0.30\). These values are illustrative conditional effects, not estimated results from an actual study. Use the logistic model above to find the conditional probability.

First calculate the linear predictor:

$$ \eta=-1.20+(0.35)(2)+0.20-0.10+0.30 =-1.20+0.70+0.20-0.10+0.30=-0.10. $$

Convert from log odds to probability:

$$ p=\frac{\exp(\eta)}{1+\exp(\eta)} =\frac{\exp(-0.10)}{1+\exp(-0.10)} =\frac{0.9048}{1.9048}\approx 0.4750. $$

Conditional on these random effects and the covariate value, the model assigns this observation a 0.475 probability of the outcome. This is not the probability averaged over the random-effects distributions; that marginal probability requires integrating over those distributions.

Worked Example: Decide Whether a Clinician Term Is Crossed

In a second hypothetical design, each of four clinicians works at exactly one of four clinics, and no clinician works at any other clinic. Patients are still enrolled within clinics, with repeated visits. Should clinician and clinic be treated as crossed in this design?

No. Under the stated assignment, each clinician belongs to one clinic, so clinician is nested within clinic. If clinician C1 is at clinic A and C2 is at clinic B, their terms can be indexed \(a_{1,1}\) and \(a_{2,1}\), or described as clinician-within-clinic effects. A clinician identity shared across clinics is not present.

For this particular design, a model could include clinic and clinician-within-clinic random intercepts, as well as patient-within-clinic intercepts for repeated observations. The relevant distinction is the sampling structure, not whether the software can accept a formula labelled “crossed.” The design described here does not justify assigning one clinician effect to observations in multiple clinics. If a clinician later moves between clinics during the study, the analyst must determine whether the intended effect represents persistent clinician identity, clinic-specific practice, or both before choosing the terms.

Why the Terms Enter Together

A mixed-effects model combines contributions from the observation’s relevant groups in one conditional linear predictor. In the binary example, that predictor contains a clinic contribution, a patient contribution, and a clinician contribution, as well as fixed effects. Conditional on those group effects, the outcome follows its chosen response distribution. The probability model for the observed data is obtained by averaging the conditional likelihood over the random-effects distributions.

$$ L(\beta,\theta) = \int \left\{ \prod_{k,j,t} f\!\left(y_{kjt}\mid u_k,v_{jk},a_{l(k,j,t)};\beta\right) \right\} p(u,v,a;\theta)\,d(u,v,a). $$

Here \(f\) is the conditional response density or probability mass function, \(\beta\) denotes the fixed-effect parameters, and \(\theta\) denotes random-effects distribution parameters. The product expresses conditional independence of observations given all included effects; the integration accounts for uncertainty and variation in those effects. Importantly, two observations can share one group effect without sharing every other effect. For example, observations from different clinics by C7 share the clinician effect but not the clinic effect.

This justification also explains why a random-effect term must match the identity that is actually shared. If a clinician’s effect is mistakenly split into clinic-specific effects, the model no longer represents one persistent clinician contribution across clinics. If patient identities are mistakenly shared across clinics when patient labels are only locally unique, the model instead imposes sharing where the design has none.

Extensions, Diagnostics, and Alternatives

The same design logic applies to other conditional response distributions and links. A count outcome might use a log link and a count distribution; a continuous outcome might use a Gaussian conditional distribution. The nesting and crossing structure is determined by which observations share units, not by whether the response is binary, a count, or continuous.

Random slopes can be included when a covariate’s association is expected to vary across a grouping unit and the data support that additional variation. For example, if \(x\) is measured repeatedly within clinics, a clinic-specific slope could be added as \(u_{1k}x_{kjt}\), alongside the clinic intercept \(u_{0k}\). A random slope should be tied to a clearly identified unit and a meaningful varying covariate; it is not a generic remedy for lack of fit. Additional random terms require enough informative replication and may make estimation difficult.

Several checks help identify a specification that is not working as intended:

  • Audit identifiers. Verify that a patient’s clinic is unique and that a clinician who appears in multiple clinics retains one global identifier. Check for accidental reuse of local labels.
  • Check the observed overlap. Count how many clinicians appear in multiple clinics and how many patients have repeated observations. If the supposed crossing or repetition is absent, the corresponding interpretation may not be supported by the data.
  • Inspect fitting warnings. Convergence problems, very large uncertainty in variance estimates, or estimates near a boundary can signal sparse information, an overcomplicated structure, or numerical difficulty. They do not by themselves prove which term should be removed.
  • Assess conditional fit. Look for systematic residual patterns by clinic, patient, clinician, time, or covariate. A persistent pattern may indicate a missing fixed effect, an unsuitable response distribution, or an inadequate random-effects structure.
  • Check the assumptions against the design. The illustrative model assumes independent random-effect sets and conditional independence. Shared time shocks, clinician-specific slopes, or other dependencies may make those assumptions implausible.

A nested-only model is appropriate when each clinician belongs to one clinic, but it is inadequate for representing the same clinicians working across clinics if it assigns a separate clinician effect within each clinic. A crossed-only model that includes clinicians but omits clinic or patient effects discards the nesting and repetition in the design. Treating all group factors as fixed effects is another possible analysis, especially for a small set of groups that are themselves the main targets of inference, but it changes the inferential target and does not substitute automatically for a random-effects model. Model choice should follow the scientific question, the sampling design, and the available replication.

The model has limits. Random effects do not correct confounding, guarantee causal interpretation, or compensate for incorrect identifiers. A random-intercept specification also assumes a particular form of group-level variation; it may not capture changing effects over time or dependence not represented by the terms. Sparse crossing can make it difficult to estimate the intended components. A defensible formula is therefore a design-based starting point, followed by checks of fit and estimation—not a guarantee that every assumption is satisfied.

Common Mistake

Common mistake: “Clinician C7 appears in clinic A and clinic B, so I will create a separate clinician effect for C7 in each clinic because clinicians are part of clinics.” This changes the model’s meaning: it treats the two appearances as unrelated clinic-specific clinician identities. If C7 is the same clinician across both clinics, use one clinician identifier and a clinician effect shared across them. Nesting describes the assignment of levels in the design; it does not mean every factor observed within a clinic must be nested there.
Key takeaway: Write the indexing and assignment first. Use clinic and patient-within-clinic effects for the nested structure, and use one globally indexed clinician effect when clinicians recur across clinics. State the conditional distribution and random-effects assumptions explicitly.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions below.

  1. Patients belong to one clinic, while some clinicians see patients in several clinics. Which factor is nested, and which is crossed with clinic?
  2. A clinician appears in clinics 1 and 3. In the crossed model, should those records use the same clinician random effect or two clinic-specific clinician effects?
  3. For a hypothetical binary-outcome model, let \(\eta=-0.40\). Calculate the conditional probability using the logistic link.
  4. Every clinician in a hypothetical study works at one clinic only. What does that imply about clinician’s relation to clinic?
  5. Name two assumptions of the illustrative conditional model that should be checked against the study design.

Solutions. (1) Patients are nested within clinic; clinician is crossed with clinic when clinician levels occur in multiple clinics. (2) Use the same clinician effect, because the identity is shared across clinics. (3) \(p=\exp(-0.40)/\{1+\exp(-0.40)\}=0.6703/1.6703\approx0.4013\). This is the probability conditional on the specified effects and covariates. (4) Clinician is nested within clinic in the described design; the data do not show clinicians crossing clinics. (5) For example, the random-effect sets are assumed mutually independent, and observations are assumed conditionally independent given the effects and covariates. The response distribution and link are additional assumptions to assess.

Clinical Biostats