Explain fixed and random effects and when clustered or repeated data need a mixed model.
Why this matters
Real data is clustered. Several measurements come from the same patient, plate, batch, field plot, school or machine. Observations within a cluster are more alike than observations from different clusters, which breaks the independence assumption behind OLS and ANOVA. Ignore it and your standard errors are too small, your p-values too optimistic, and your "significant" result may not replicate. Mixed-effects models (also called multilevel or hierarchical models) handle clustering directly, and they are now the default analysis for repeated measures, split-plot experiments and multi-site studies.
Fixed effects and random effects
A mixed model contains both:
- Fixed effects are the effects you want to estimate and report: the treatment, the dose, the temperature. Their levels are chosen deliberately, and you would use the same levels again if you repeated the study.
- Random effects describe variation between clusters that are a sample from a larger population: these 12 batches out of all batches the plant will ever make, these 30 patients out of all patients. You are not interested in batch 7 for its own sake; you want to know how much batches vary and to account for it.
A useful test: if you repeated the study, would you use the same levels (fixed) or new ones drawn from the same population (random)? Treatments are fixed; batches, subjects and days are usually random.
The random-intercept model
Suppose 20 cell-culture flasks are split between two media, and each flask is measured on 5 days. A random-intercept model is:
y_ij = b0 + b1*medium_i + b2*day_ij + u_i + e_ij
u_i ~ Normal(0, σ_u²) flask-to-flask variation
e_ij ~ Normal(0, σ_e²) day-to-day noise within a flask
Each flask gets its own baseline b0 + u_i, drawn from a normal distribution. Two variance components are estimated: σ_u² (between flasks) and σ_e² (within flasks). Their ratio gives the intraclass correlation:
ICC = σ_u² / (σ_u² + σ_e²)
An ICC of 0.6 means 60% of the variance is between flasks: measurements from the same flask are strongly correlated, and the effective sample size for the medium comparison is much closer to 20 flasks than to 100 measurements.
Random slopes
If the growth rate itself varies by flask, add a random slope: each flask gets its own deviation from the average slope for day, with its own variance (and usually a correlation with the random intercept). Random slopes are important when the fixed effect of interest varies across clusters; leaving them out can make tests of that effect anti-conservative.
Fitting one in Python
import statsmodels.formula.api as smf
# Random intercept per flask
m1 = smf.mixedlm("growth ~ C(medium) + day", data=df, groups=df["flask"]).fit()
print(m1.summary()) # fixed effects plus "Group Var" (σ_u²) and residual scale (σ_e²)
# Random intercept and random slope for day
m2 = smf.mixedlm("growth ~ C(medium) + day", data=df,
groups=df["flask"], re_formula="~day").fit()
In R, the usual tool is lme4: lmer(growth ~ medium + day + (1 | flask)) for a random intercept and (1 + day | flask) for a random slope. For non-normal responses there are generalised linear mixed models (glmer in R, BinomialBayesMixedGLM or other packages in Python).
Shrinkage: why random effects are "partially pooled"
There are three ways to handle the flasks:
- Complete pooling: ignore flasks (plain OLS). Wrong standard errors.
- No pooling: a separate fixed intercept for every flask. Uses many parameters, and flasks with few measurements get noisy estimates.
- Partial pooling (random effects): each flask's estimated intercept is pulled toward the overall mean, more strongly when the flask has few or noisy measurements. This shrinkage is a feature: it borrows strength across clusters and usually predicts new data better.
Where mixed models appear in experiments
- Blocked designs with many blocks (days, lots, sites): blocks as random effects.
- Split-plot designs: whole plots (an oven run, a field strip) are a random effect, so the hard-to-change factor is tested against whole-plot variation, not the smaller subplot error.
- Repeated measures over time on the same subjects or units.
- Multi-site trials and inter-laboratory studies (sites or labs as random effects; the variance components give repeatability and reproducibility).
- Nested sampling such as several aliquots per sample, several samples per batch.
Practical cautions
- You need enough clusters. Estimating a variance from 3 batches is very imprecise; 5–6 is a bare minimum and more is much better. With very few clusters, treat them as fixed.
- REML vs ML. Variance components are usually estimated by restricted maximum likelihood (REML, the default). To compare models that differ in their fixed effects with a likelihood-ratio test, refit with ML.
- Degrees of freedom for fixed-effect tests are not obvious in mixed models. Methods such as Satterthwaite or Kenward–Roger (in R's
lmerTest/pbkrtest) give better small-sample p-values than a naive z-test. - Convergence warnings often mean the random-effects structure is too complex for the data. Simplify (drop a correlation or a random slope) and say so.
Common mistakes
- Treating repeated measurements as independent replicates (pseudo-replication again).
- Making the treatment a random effect because it has "levels".
- Reporting only fixed effects and hiding the variance components, which are often the most useful result for process improvement.
- Using a random effect for a factor with two or three levels.
Quick recap
- Fixed effects: levels of interest. Random effects: clusters sampled from a population.
- A random intercept models cluster-to-cluster baseline variation; the ICC says how strong it is.
- Mixed models give honest standard errors for clustered, repeated and split-plot data and shrink cluster estimates toward the mean.
Try it
This lesson's Do tab is a short quiz on choosing fixed versus random effects and reading variance components.