Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
“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.
A mixed-effects model is a regression that contains both fixed effects and random effects. Fixed effects estimate the average relationship between a predictor and the outcome across all groups; random effects capture how intercepts (and sometimes slopes) vary from group to group, such as patients, clinics or schools. Use a random effect when the groups are a sample you want to generalize beyond, and a fixed effect when you care about each specific group. For a broader overview of the method, see mixed-effects models.
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. If the question is about population-average effects rather than clinic-specific ones, generalized estimating equations with a working correlation structure are the usual alternative.
| 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. Outcomes that pile up at a scale limit are a common cause of skew; see floor and ceiling effects for how to detect range compression.
- 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. For the same distinction applied to pooling study results, see fixed-effect vs. random-effects meta-analysis.
- 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.
Singular Fits: What They Are and How to Respond
In lme4, a singular fit means one or more estimated variance-covariance matrices of the random effects have less than full rank. In plain terms, a variance is estimated at zero or two random effects are perfectly correlated. The lme4 function isSingular() tests for this, and its default tolerance is 1e-4. The lme4 documentation also offers rePCA (a principal components analysis of the random-effects structure) as a way to see which dimensions the data cannot support.
The lme4 help page states that there is not yet a consensus on how to handle singular fits. The two best-known positions in the literature pull in different directions:
- “Keep it maximal.” Barr, Levy, Scheepers and Tily (2013, Journal of Memory and Language 68(3):255-278) argued for the most complex random-effects structure consistent with the study design, removing terms only as far as needed to obtain a non-singular fit.
- Balance power and type I error. Matuschek, Kliegl, Vasishth, Baayen and Bates (2017, Journal of Memory and Language 94:305-315) proposed model selection that balances type I error and power, rather than defaulting to the maximal structure.
The lme4 documentation lists the practical options: avoid overly complex random-effects structures to begin with, use model selection of the kind Matuschek et al. describe, or use Bayesian regularisation (for example via the blme package) so that variance estimates are kept away from the boundary. Whichever route you take, state in the methods which random-effects structure you started with, which terms were removed, and why.
REML or ML: What the Software Does by Default
lme4’s lmer() fits models by restricted maximum likelihood (REML) by default (REML = TRUE), and can also fit by maximum likelihood. statsmodels’ MixedLM likewise supports either ML or REML. The practical rule from the Assumptions section applies in both: use ML when comparing models that differ in their fixed effects with a likelihood-ratio test, and REML for estimating variance components in a final model.
Getting p-Values for Fixed Effects
The lme4 package itself does not report p-values for fixed effects in lmer summaries. The lmerTest package (version 3.2-1, published 2026-03-05) adds p-values for lmer fits using Satterthwaite degrees of freedom, along with type I, II and III ANOVA tables; Kenward-Roger degrees of freedom are available through the pbkrtest package. Report which degrees-of-freedom method you used, since the choice can change a borderline result. For how to read and report p-values themselves, see What Is a P Value?.
Generalised Linear Mixed Models and Estimation
When the outcome is binary, a count or otherwise non-normal, the mixed-model counterpart is a generalised linear mixed model (GLMM). In lme4, glmer() uses the Laplace approximation by default (nAGQ = 1). Setting nAGQ above 1 requests adaptive Gauss-Hermite quadrature, which lme4 supports only for models with a single scalar random effect; nAGQ = 0 is faster but less exact. glmer.nb() fits negative binomial models. The glmmTMB package (version 1.1.15.2, published 2026-09-29) fits linear and generalised mixed models, including zero-inflated variants, by maximum likelihood using TMB with the Laplace approximation.
Software Compared
The data table below the guide body compares the main R and Python tools by model class, documented estimation method and documented limitation, using only what each package’s own documentation states. R users also have nlme (version 3.1-172, published 2026-10-31), which fits Gaussian linear and nonlinear mixed-effects models. The same fixed/random modelling decisions in this guide apply whichever tool you use.
Crossed random effects in Python
The statsmodels documentation (0.15.0) says MixedLM handles most non-crossed random-effects models and some crossed models; crossed designs have to be set up by treating the whole dataset as one group and specifying variance components. It also assumes independent, normally distributed errors, and it suggests generalised estimating equations (GEE) when only the marginal mean matters. Where a design has crossed factors such as raters and subjects, lme4 or glmmTMB in R express them directly with separate random-effect terms.
Sources (accessed 2026-11-16)
- lme4
lmerreference page: https://search.r-project.org/CRAN/refmans/lme4/html/lmer.html - lme4
glmerreference page: https://search.r-project.org/CRAN/refmans/lme4/html/glmer.html - lme4
isSingularreference page: https://search.r-project.org/CRAN/refmans/lme4/html/isSingular.html - lmerTest on CRAN: https://cran.r-project.org/package=lmerTest
- nlme on CRAN: https://cran.r-project.org/package=nlme
- glmmTMB on CRAN: https://cran.r-project.org/web/packages/glmmTMB/index.html
- statsmodels, Linear Mixed Effects Models: https://www.statsmodels.org/stable/mixed_linear.html
- Bates, Mächler, Bolker and Walker (2015), Fitting Linear Mixed-Effects Models Using lme4, Journal of Statistical Software 67(1):1-48, doi 10.18637/jss.v067.i01: https://www.jstatsoft.org/article/view/v067i01
- Barr et al. (2013), Journal of Memory and Language 68(3):255-278; Matuschek et al. (2017), Journal of Memory and Language 94:305-315.
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.
Why does lmer not show p-values?
lme4 does not compute them for linear mixed models. Loading lmerTest adds p-values based on Satterthwaite degrees of freedom, and Kenward-Roger degrees of freedom are available via pbkrtest.
REML or ML: which should I use?
lmer defaults to REML, which is the usual choice for a final model’s variance components. Switch to ML to compare models with different fixed effects by likelihood-ratio test.
Can statsmodels fit crossed random effects?
Only partly. Its documentation says MixedLM handles most non-crossed models and some crossed models, using the whole dataset as one group with variance components.
What does nAGQ do in glmer?
It sets the number of adaptive Gauss-Hermite quadrature points. The default, 1, is the Laplace approximation; higher values are available only with a single scalar random effect, and 0 is faster but less exact.
Is “keep it maximal” still recommended?
It is one of two published positions. Barr et al. (2013) advocate the maximal structure the design supports; Matuschek et al. (2017) advocate model selection that balances power and type I error. The lme4 documentation says there is no consensus.
Related Reading
- Intraclass Correlation Coefficient (ICC): Forms, Interpretation, and How to Report It
- Cluster Randomised Trials and the Intracluster Correlation Coefficient (ICC)
- Longitudinal Study Design: Types, Strengths, and Limitations
- Regression Analysis: Assumptions, Interpretation, and How to Report It
- Power Analysis and Sample Size Calculation: A Complete Guide
- Confidence Interval Explained: How to Calculate and Interpret One
- What Is a P Value? Definition, Misinterpretations, and How to Read One
- Effect Size
- Confounding Variable
- ANOVA (Analysis of Variance)
Data: Mixed-effects modelling software compared: model classes, estimation and inference features (R and Python)
Compiled from the official CRAN package pages, lme4 reference manual pages and statsmodels documentation: which model classes each tool fits, its stated estimation method and what its documentation says about inference and crossed effects. Cells state only what the cited page says.
Period covered: 2026.
| Tool | Language | Model classes (as documented) | Estimation (as documented) | Inference / limitation noted in documentation | Version read |
|---|---|---|---|---|---|
| lme4::lmer | R | Linear mixed-effects models | REML (default, REML = TRUE) or maximum likelihood | p-values not reported by lme4 itself; see lmerTest | Reference manual (current CRAN) |
| lme4::glmer | R | Generalized linear mixed models (glmer.nb for negative binomial) | Laplace approximation by default (nAGQ = 1); nAGQ = 0 is faster and less exact | Adaptive Gauss-Hermite quadrature only for a single scalar random effect | Reference manual (current CRAN) |
| lmerTest | R | Adds p-values and ANOVA tables (type I, II, III) to lmer fits | Not applicable (extends lmer) | Satterthwaite degrees of freedom; Kenward-Roger via pbkrtest | 3.2-1 (2026-03-05) |
| glmmTMB | R | Linear and generalized linear mixed models, including zero-inflated variants | Maximum likelihood via TMB; Laplace approximation | Random effects Gaussian on the linear predictor scale | 1.1.15.2 (2026-09-29) |
| statsmodels MixedLM | Python | Linear mixed models with random intercepts and slopes | ML or REML | Handles most non-crossed and some crossed models; errors i.i.d. normal; GEE suggested if only the marginal mean matters | 0.15.0 docs |
Sources
- lme4 lmer reference page (accessed 2026-11-16)
- lme4 glmer reference page (accessed 2026-11-16)
- lmerTest CRAN page (accessed 2026-11-16)
- glmmTMB CRAN page (accessed 2026-11-16)
- statsmodels Linear Mixed Effects Models (accessed 2026-11-16)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







