Skip to main content
v2026.11,610 entries · CC-BY 4.0

Mixed-Effects Models: Choosing Random vs. Fixed Effects for Nested Research Data

Choosing between fixed and random effects is determined by data structure, not preference. A decision table for nested, clustered, and repeated-measures data, plus a worked interpretation of real-looking model output.

Ask about Mixed-Effects Models: Choosing Random vs. Fixed Effects for Nested Research Data

Answers are drawn from this guide and the rest of the CASRAI corpus, with a link to every source.

Answers are AI-generated from CASRAI’s own published pages and can be wrong, so check the linked sources before relying on one; your question is logged without personal data — never sold, never used to train a third-party model — to show us what CASRAI is missing, so please do not type personal or confidential details. How we use this

“Fixed or random effects?” is not a modeling preference — it is determined by the structure of your data and what you want to generalize to. A common misreading treats the choice as a matter of taste, or as something to decide by trying both and seeing which gives a “better” (usually smaller) p-value. That is backwards. The structure of the data — whether observations are nested within subjects, sites, clinics, schools, or repeated over time — determines which effects can validly be treated as random, and the answer is often “you need both in the same model,” which is exactly what a mixed-effects model is for.

What a Mixed-Effects Model Actually Does

A mixed-effects model (also called a multilevel model, hierarchical linear model, or, in some fields, a random-coefficients model) combines two kinds of terms in a single regression:

  • Fixed effects estimate the population-average relationship between a predictor and the outcome — the same coefficient you would get from ordinary regression, e.g., “treatment increases score by X points on average.”
  • Random effects allow that relationship (or just the intercept) to vary by group, treating the group-specific deviations as draws from a distribution rather than as separate parameters to estimate one by one. This is what lets the model account for the fact that observations within the same group — the same patient measured repeatedly, the same clinic’s patients, the same classroom’s students — are correlated, not independent.

The practical payoff is that a mixed-effects model gives correct standard errors for nested data. Ignoring the nesting and running an ordinary regression on every observation treats correlated observations as independent, which artificially shrinks standard errors and inflates the apparent statistical significance of predictors measured at the group level. This is the same underlying problem described in cluster-randomized trial design, where the intracluster correlation and design effect quantify exactly how much a cluster-randomized analysis needs to correct for.

Fixed vs. Random Effects: The Decision Table

Use the structure of the grouping variable, not the variable’s name, to decide. The two questions that matter are: (1) is the number of groups small and fixed, or large and effectively sampled from a bigger population, and (2) do you want to estimate and compare the specific groups in your data, or generalize beyond them?

Data structure Example Treat as Why
Repeated measurements on the same subject Symptom score recorded at 4 visits per patient Random intercept (± random slope) for subject Subjects are a sample from a population you want to generalize to; you are not interested in comparing these specific individuals to each other
Many sites/clinics/schools, sampled rather than exhaustive 30 clinics drawn from a larger regional network Random effect for site You want to generalize to clinics beyond the ones observed, and 30 is enough groups to estimate a variance component reliably
A small, fixed, exhaustive set of groups you care about individually 3 treatment arms in a trial; the 4 hospital departments in your own institution Fixed effect (dummy-coded) for group There is no larger population being sampled, and you specifically want an estimate for each named group, not a variance
Very few groups (roughly fewer than 5-6) A study run across 3 study sites Fixed effect, even if the groups feel “random” A random-effect variance component estimated from only a handful of groups is unstable and often pinned at or near zero (a “singular fit”) — see Assumptions below
Two or more nested grouping levels Students within classrooms within schools Random effects at each nesting level Each level contributes its own source of correlated variation; a single-level model understates uncertainty at every level above the lowest
Crossed (non-nested) grouping factors The same set of raters scoring every subject Crossed random effects (e.g., for subject and for rater) Raters are not nested within subjects or vice versa — both contribute independent variation and both need their own random effect

Where the field draws this line differently: econometrics panel-data literature often uses “fixed effects model” to mean something narrower — a model with a dummy variable (or de-meaning transformation) for every group and no distributional assumption on the group effects at all, chosen over a random-effects specification via a Hausman test when group effects are suspected to correlate with the predictors. That is a real and different modeling choice, not the same distinction as the nested/nesting question above; if you are working in a panel-econometrics context specifically, the Hausman-test literature — not this decision table — is the relevant framework.

Worked Example: Reading a Mixed-Model Output

Illustrative worked example — the numbers below are constructed to show what a typical output looks like and how to read it; they are not drawn from any real study.

Suppose a researcher fits a mixed-effects model to symptom-score data from 240 patients (4 repeated visits each) nested within 20 clinics, testing whether a treatment lowers scores, with a random intercept for clinic to account for baseline differences between clinics.

Component Estimate SE / detail
Fixed effect: Intercept 18.4 SE = 0.9
Fixed effect: Treatment -4.2 SE = 1.1, 95% CI [-6.4, -2.0]
Random effect: Clinic (intercept) variance 6.8 SD = 2.6
Residual variance 15.1 SD = 3.9
Intraclass correlation (ICC) 0.31 6.8 / (6.8 + 15.1)

The sentence a researcher would write from this output: “Patients receiving treatment had symptom scores 4.2 points lower on average than controls (95% CI -6.4 to -2.0), after accounting for clustering of patients within clinics (ICC = 0.31), indicating that roughly a third of the unexplained variance in scores lies between clinics rather than between patients within the same clinic.”

Two things that output is doing that a plain regression on the pooled 960 observations would not: it is not treating the 4 visits per patient as 4 independent data points, and it is folding the between-clinic variance into the ICC rather than letting it masquerade as extra precision on the treatment effect. For background on what the ICC itself measures and the closely related but distinct rater-agreement ICC, see Intraclass Correlation Coefficient (ICC): Forms, Interpretation, and How to Report It.

Assumptions and When Not to Use a Mixed-Effects Model

  • Enough groups to estimate a variance component. A commonly cited convention — not a formal requirement — is at least 5-6 groups as an absolute floor, with 8-10 or more preferred before a random-effect variance estimate is trusted. Below that, fit fixed effects for the grouping variable instead.
  • Approximate normality of the random effects and residuals. Mixed models are moderately robust to mild departures, but strongly skewed outcomes usually need a transformation or a generalized linear mixed model (e.g., a mixed logistic or Poisson model) rather than the standard linear form.
  • Watch for a “singular fit” or boundary warning. Most mixed-model software flags this when a random-effect variance is estimated at or near zero — a sign the model is more complex than the data can support (too many random slopes, too few groups, or a random effect that isn’t really varying). The fix is to simplify the random-effects structure, not to ignore the warning.
  • REML vs. ML matters for what you’re doing. Restricted maximum likelihood (REML) gives less biased variance-component estimates for a given fixed-effects structure and is the standard choice for reporting a final model. Maximum likelihood (ML) is needed instead when comparing two models that differ in their fixed effects via a likelihood-ratio test, because REML likelihoods aren’t comparable across different fixed-effects specifications.
  • No clustering, no need. If observations genuinely are independent — one row per independent subject, no repeated measurement, no shared grouping — a mixed-effects model adds unnecessary complexity; ordinary regression is the right tool.
  • A mixed model does not fix confounding. Random effects absorb correlation due to shared group membership; they do nothing about a confounding variable that isn’t in the model at all.

Fixed Effects, Random Effects, and Mixed Effects: Quick Definitions

These three terms are frequently conflated. As modeling choices for the same grouping variable, they differ as follows:

  • Fixed-effects model: a separate parameter is estimated for each group (e.g., a dummy variable per clinic), with no assumption that the groups are related to each other or drawn from a distribution. Estimates for small groups are not “shrunk” toward an overall mean.
  • Random-effects model: group-level deviations are assumed to be drawn from a distribution (typically normal, mean zero), which lets estimates for groups with fewer observations “borrow strength” from the full set of groups (partial pooling / shrinkage), and lets the model generalize to groups not observed in the data.
  • Mixed-effects model: includes both fixed effects (population-average estimates for predictors of interest) and random effects (group-level variation) in the same model — the combination is what “mixed” refers to, and it’s the form used for essentially all nested, clustered, or repeated-measures research data.

Multilevel Models vs. Mixed-Effects Models: Same Thing?

In practice, yes, for almost all research purposes: “multilevel model,” “hierarchical linear model” (HLM), and “mixed-effects model” describe the same underlying method, and the terminology split is mostly a matter of which field you’re reading. Education and social-science research (following the Raudenbush and Bryk hierarchical-linear-modeling tradition) tends to say “multilevel model” and emphasizes explicit nesting levels — students within classrooms within schools. Biostatistics, ecology, and psychology more often say “mixed-effects model” or “mixed model,” with the emphasis on the fixed/random split rather than the number of levels. The underlying mathematics — and the decision table above — is the same regardless of which label a given paper or software package uses.

A Note on Software

This is a page about choosing and interpreting the model, not a syntax reference. In brief: R fits mixed-effects models via the lme4 package (with lmerTest commonly added for approximate p-values), Stata via the mixed command, SAS via PROC MIXED or PROC GLIMMIX for generalized mixed models, and Python via statsmodelsMixedLM. All implement the same fixed/random distinction described above; the modeling decisions in this guide apply regardless of which one you use.

Frequently Asked Questions

Should I always use random effects instead of fixed effects, since it seems more flexible?

No. Random effects require enough groups to estimate a variance component reliably (see Assumptions above) and are appropriate when you want to generalize beyond the specific groups observed. With a small, fixed, exhaustive set of groups you specifically care about, a fixed effect is the more defensible and often more stable choice.

How many groups do I need to use a random effect?

There is no formal minimum, but a widely used convention treats roughly 5-6 groups as an absolute floor and 8-10 or more as preferable before trusting the estimated variance component. Fewer groups than that and the variance estimate is unstable, and software will often flag a singular or boundary fit.

What is the difference between a random intercept and a random slope?

A random intercept lets each group have its own baseline (starting) value while assuming the effect of predictors is the same across groups. A random slope goes further and lets the effect of a specific predictor itself vary by group — for example, allowing the treatment effect to differ from clinic to clinic, not just the baseline score. Random slopes require more data per group to estimate and are a common source of singular-fit warnings when added without enough justification.

Can the same variable have both a fixed and a random effect in one model?

Yes, and this is the normal case, not an exception. A predictor’s fixed effect estimates its average effect across all groups; adding a random slope for that same predictor lets the model also capture how much that effect varies from group to group. The worked example above uses a random intercept only, for clinic; adding a random slope for treatment-by-clinic would be a natural next step if there were reason to think the treatment effect itself varies meaningfully across clinics.

What does a “singular fit” or “boundary” warning mean, and can I ignore it?

It means at least one random-effect variance (or correlation between random effects) has been estimated at or effectively at zero, which usually indicates the random-effects structure is too complex for what the data can support. It should not be ignored; the standard response is to simplify the random-effects structure (drop a random slope, or a correlation term between random effects) rather than to keep the warning and report the model as-is.

Related Reading

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →