Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
A linear mixed model in R (often referred to as a linear mixed models in R, mixed model in R, or mixed effects model in R) provides a regression framework for analyzing continuous outcomes structured by clustering, hierarchy, or repeated observations. By combining population-level fixed effects with cluster-specific random effects, researchers can analyze clustered trials, longitudinal designs, and multi-site observational datasets without violating the independence assumption of classical ordinary least squares (OLS) regression.
What it is used for in research
In empirical research across psychology, ecology, biomedical trials, economics, and educational measurement, datasets frequently exhibit hierarchical grouping. Examples include repeated longitudinal measurements collected from the same human participants over successive days, students grouped within classrooms and schools, or ecological sampling points nested within geographical sites. Standard linear models assume that residual errors are independent and identically distributed (i.i.d.). When observations within the same cluster correlate with one another, OLS regression underestimates standard errors and inflates Type I error rates. When the target is a population-average effect rather than subject-specific ones, generalized estimating equations are the main alternative.
Specifying a within-subjects vs between-subjects design requires statistical models that partition variance across levels. A linear mixed model resolves dependency by simultaneously estimating two parameter sets:
- Fixed effects: Population-level average parameters representing the overall relationship between an independent vs dependent variable across the entire sample.
- Random effects: Group-level deviations from population parameters, capturing how baseline intercepts or treatment slopes vary across clusters or individuals.
Researchers can select between a random-intercept specification and a random-slope specification depending on their theoretical design. Evaluating random intercepts vs random slopes ensures that individual differences in treatment trajectories are modeled appropriately rather than collapsed into residual error.
How to install or access it
Linear mixed models in R are implemented primarily through the Comprehensive R Archive Network (CRAN) packages lme4 and lmerTest. The packages are installed directly from CRAN mirrors using standard R command-line functions:
# Install official packages from CRAN
install.packages(c("lme4", "lmerTest", "MuMIn"))
The core computational engine lme4 requires R version 3.2.0 or higher along with the compiled C++ packages Matrix, Rcpp, and RcppEigen. To access the functions within an active R session, load the libraries:
library(lme4)
library(lmerTest)
Loading lmerTest after lme4 automatically overloads the lmer fitting function and extends model summary methods with Satterthwaite approximations for denominator degrees of freedom and p-values.
Step-by-step instructions
This workflow demonstrates fitting, comparing, and interpreting a linear mixed model in R using the canonical sleepstudy dataset published in lme4 (Bates et al., 2015), which records reaction times (Reaction, in milliseconds) over consecutive days of sleep deprivation (Days, 0 to 9) across 18 human subjects (Subject).
1. Inspect dataset structure and clustering
Examine the dataframe dimensions and verify that grouping variables are encoded as factors:
data("sleepstudy", package = "lme4")
str(sleepstudy)
# Verify factor encoding for cluster identifier
sleepstudy$Subject <- as.factor(sleepstudy$Subject)
head(sleepstudy, n = 6)
2. Fit a random-intercept linear mixed model
A random-intercept model assumes that baseline reaction times vary among subjects, but the rate of change per day of sleep deprivation remains constant across all subjects:
# Fit random-intercept model via REML
fit_intercept <- lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)
summary(fit_intercept)
In this syntax, Reaction ~ Days defines the fixed effect structure, while (1 | Subject) introduces a random intercept for each level of the grouping factor Subject.
3. Fit a correlated random-intercept and random-slope model
To allow both the baseline reaction time and the effect of sleep deprivation to vary across subjects, specify a random slope on Days:
# Correlated random intercept and random slope
fit_slope <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)
summary(fit_slope)
The notation (Days | Subject) is equivalent to (1 + Days | Subject). It estimates the variance of subject intercepts, the variance of subject slopes, and the covariance parameter between them.
4. Fit an uncorrelated random-intercept and random-slope model
If covariance between baseline performance and slope degradation is assumed to be zero, suppress the correlation parameter using the double vertical bar ||:
# Uncorrelated random intercept and slope
fit_uncorr <- lmer(Reaction ~ Days + (Days || Subject), data = sleepstudy)
5. Evaluate model improvement via Likelihood Ratio Test
To test whether adding a random slope significantly improves model fit over a random-intercept model, perform a likelihood ratio test using anova():
# Compare nested models
anova(fit_intercept, fit_slope)
When comparing models with different fixed effects, models must be fit with Maximum Likelihood (REML = FALSE) rather than Restricted Maximum Likelihood (REML) because REML criterion values depend on the fixed effects design matrix. When comparing identical fixed-effects structures with differing random-effects terms, anova.merMod() performs the test directly.
6. Calculate effect sizes and variance explained
Compute Marginal and Conditional R² values following the Nakagawa and Schielzeth (2013) formulation:
library(MuMIn)
r.squaredGLMM(fit_slope)
Marginal R² (R²m) quantifies the proportion of variance explained by fixed effects alone. Conditional R² (R²c) quantifies the proportion of variance explained by both fixed and random effects combined.
Output and how to read it
Executing summary(fit_slope) produces three core diagnostic blocks in R:
Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: Reaction ~ Days + (Days | Subject)
Data: sleepstudy
Scaled residuals:
Min 1Q Median 3Q Max
-3.9559 -0.4634 0.0231 0.4634 5.1793
Random effects:
Groups Name Variance Std.Dev. Corr
Subject (Intercept) 612.10 24.741
Days 35.07 5.922 0.07
Residual 654.94 25.592
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 251.405 6.825 17.000 36.838 < 2e-16 ***
Days 10.467 1.546 17.000 6.771 3.26e-06 ***
Inspect these outputs using the following statistical criteria:
- Estimation method and convergence: The header reports that parameter optimization used Restricted Maximum Likelihood (REML) and confirms that
lmerModLmerTestis the active class. - Scaled residuals: Evaluates residual symmetry. Median residuals close to 0 with roughly symmetric 1st and 3rd quartiles support the assumption of normally distributed error terms. Compare general diagnostics using summary in R.
- Random effects variance components:
Subject (Intercept)variance of 612.10 (Std.Dev. 24.74 ms) indicates between-subject variation in baseline reaction time.Daysvariance of 35.07 (Std.Dev. 5.92 ms/day) captures individual vulnerability to sleep deprivation.Residualvariance of 654.94 (Std.Dev. 25.59 ms) measures trial-level measurement noise within subjects.Corr(0.07) indicates a negligible correlation between a participant’s baseline reaction time and their daily rate of cognitive slowing.
- Intraclass Correlation Coefficient (ICC): In a random-intercept model, the ICC equals the intercept variance divided by total variance (τ₀₀ / (τ₀₀ + σ²)). It indicates the proportion of total outcome variation attributable to cluster membership.
- Fixed effects table:
(Intercept)estimate of 251.41 ms represents the average baseline reaction time across all subjects at Day 0.Daysestimate of 10.47 ms indicates that for each additional day of sleep deprivation, average reaction time increases by 10.47 ms (t(17.0) = 6.77, p = 3.26 × 10⁻⁶).- Denominator degrees of freedom (df = 17.00) are estimated via Satterthwaite’s approximation rather than an asymptotic normal distribution assumption.
Troubleshooting
Researchers routinely encounter specific estimation warnings and convergence flags when fitting linear mixed models in R. Use these documented remediation procedures:
1. Boundary (singular) fit warning
Warning message: boundary (singular) fit: see help('isSingular')
Cause: The numerical optimizer has estimated one or more variance components on the boundary of the parameter space—either a random-effect variance estimated at zero or a correlation parameter estimated at exactly +1.0 or -1.0. This indicates an over-parameterized random-effects structure that cannot be identified from the available sample data.
Solution: Check the fit with isSingular(fit_slope, tol = 1e-4). If singular, simplify the random structure by removing the correlation parameter via (Days || Subject), or drop the random slope if its estimated variance is negligible.
2. Model convergence failures
Warning message: Model failed to converge with max|grad| = ... or Model is nearly unidentifiable: very large eigenvalue
Cause: Predictor variables exist on vastly different numerical scales (e.g., measuring age in decades alongside income in dollars), causing ill-conditioned numerical gradients during optimization.
Solution: Center and standardize continuous covariates using scale() before model fitting. If convergence issues persist, switch from the default Nelder-Mead optimizer to BOBYQA or Nelder-Mead with increased evaluation limits via lmerControl:
# Switch optimizer to BOBYQA with 100,000 maximum iterations
fit_bobyqa <- lmer(
Reaction ~ Days + (Days | Subject),
data = sleepstudy,
control = lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000))
)
3. Missing p-values in summary output
Symptom: The output displays estimates, standard errors, and t-values, but omits p-values and degrees of freedom.
Cause: Base lme4 intentionally does not print p-values because denominator degrees of freedom in unbalanced, crossed, or non-orthogonal designs lack an exact analytical formula.
Solution: Install and load the lmerTest package before running lmer(). If a model was already fitted under lme4, cast it into an lmerModLmerTest object using fit_tested <- lmerTest::as_lmerModLT(fit_slope).
Free alternatives
While lme4 is the most widely adopted tool for linear mixed models in R, multiple open-source packages and scientific platforms support mixed-effects regression. Researchers comparing analytical ecosystems can review R vs Stata for statistical analysis or select among the dedicated open-source tools below:
| Tool / Package | Ecosystem | Primary Modeling Focus | Random Effects & Error Structures | Computational Backend |
|---|---|---|---|---|
| lme4 | R (CRAN) | Linear, generalized linear, and nonlinear mixed models | Crossed and nested random effects; homoscedastic residual errors | C++ (RcppEigen, Matrix sparse Cholesky) |
| nlme | R (Base recommended) | Linear and nonlinear mixed-effects models | Nested random effects; heteroscedastic weights (varIdent) and spatial/serial correlation (corAR1) | C and Fortran routines |
| glmmTMB | R (CRAN) | Generalized linear mixed models with diverse distributions | Crossed/nested effects, zero-inflation, dispersion models, spatial/temporal covariance | Template Model Builder (TMB), C++, automatic differentiation |
| brms | R (CRAN) | Bayesian generalized non-linear multivariate multilevel models | Complex multilevel priors, ordinal/custom distributions, nonlinear formulas | Stan C++ probabilistic programming engine |
| MixedModels.jl | Julia | High-performance linear and generalized mixed models | Crossed and nested random effects, large-scale multi-million observation datasets | Native Julia compiler, high-speed linear algebra |
FAQ
What is the mathematical difference between random intercepts and random slopes?
A random-intercept model allows the baseline response (the intercept) to shift up or down for each cluster while holding the predictor slopes identical across groups. A random-slope model permits both the baseline level and the rate of change (the slope) across a continuous predictor to vary independently or correlatively across clusters.
When should REML be used instead of Maximum Likelihood (ML)?
Restricted Maximum Likelihood (REML = TRUE) accounts for the loss of degrees of freedom when estimating fixed effects, providing unbiased estimates of variance components (random effects variances and residual variance). However, when comparing two nested models that differ in their fixed effects via likelihood ratio testing, both models must be refitted using Maximum Likelihood (REML = FALSE) because REML likelihoods are evaluated on different transformed scales.
Why does lme4 not print p-values in standard summaries?
In mixed models with unbalanced designs, crossed factors, or unequal cluster sizes, the exact distribution of the test statistic is not a Student’s t-distribution, and the exact denominator degrees of freedom cannot be calculated analytically. Authors Douglas Bates and colleagues omitted p-values to prevent reliance on naive asymptotic normal assumptions. Loading the lmerTest package applies Satterthwaite approximations or Kenward-Roger methods to provide verified finite-sample p-values.
How are crossed versus nested random effects specified in R syntax?
Crossed random effects occur when factor levels appear in combination across different grouping units (such as experimental subjects responding to multiple distinct items). They are specified as (1 | Subject) + (1 | Item). Nested random effects occur when levels of one factor exist strictly inside a single level of another factor (such as classrooms within schools) and are specified as (1 | School / Classroom) or (1 | School) + (1 | School:Classroom).
How should linear mixed model parameters be reported in scholarly manuscripts?
Reporting standards require stating the software package and version (e.g., lme4 v1.1-35.5 in R 4.4), the estimation method (REML vs ML), degrees of freedom approximation (e.g., Satterthwaite via lmerTest), fixed-effect parameter estimates with standard errors and test statistics (β, SE, t, p), random-effects variance components (τ₀₀, τ₁₁, σ²), correlation of random effects, and model fit metrics including Marginal R² and Conditional R².
Sources
- Bates, D., Mächler, M., Bolker, B., & Walker, S. (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1–48. Official CRAN package documentation: https://cran.r-project.org/package=lme4 (read 2026-11-10).
- Kuznetsova, A., Brockhoff, P. B., & Christensen, R. H. B. (2017). lmerTest Package: Tests in Linear Mixed Effects Models. Journal of Statistical Software, 82(13), 1–26. Official CRAN package documentation: https://cran.r-project.org/package=lmerTest (read 2026-11-10).
- Pinheiro, J., Bates, D., DebRoy, S., Sarkar, D., & R Core Team (2023). nlme: Linear and Nonlinear Mixed Effects Models. Official CRAN package documentation: https://cran.r-project.org/package=nlme (read 2026-11-10).
- Nakagawa, S., & Schielzeth, H. (2013). A general and simple method for obtaining R2 from generalized linear mixed-effects models. Methods in Ecology and Evolution, 4(2), 133–142. Official publication: https://besjournals.onlinelibrary.wiley.com/doi/10.1111/j.2041-210x.2012.00261.x (read 2026-11-10).







