Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

lmer in R: Syntax, Arguments and a sleepstudy Example

How to fit and read a linear mixed model with lmer() in R: arguments, formula patterns, sleepstudy output, lmerTest p-values and singular fits.

Written and maintained by CASRAI Editorial Board

Last updated

Who this is for: researchers who have repeated measurements, or observations grouped by subject, site or batch, and need to fit a linear mixed-effects model in R. After reading you should be able to write the lmer() call, read its printed output, and know what to do about missing p-values and singular fits.

lmer() is the function in the lme4 package that fits a linear mixed-effects model (LMM) by restricted maximum likelihood (REML) or maximum likelihood. Searches for “lmer r” refer to the same function. Package version checked: lme4 2.1-0 (reference manual dated 23 October 2026, read 2026-11-08).

What lmer is used for in research

Use lmer() when the response is continuous and observations are not independent because they share a grouping unit, for example repeated reaction-time measurements on the same participant. Fixed effects estimate the average relationship; random effects absorb between-group variation. For the underlying concepts see mixed-effects models: random vs fixed effects and random intercepts vs random slopes. For non-normal responses (counts, binary outcomes) see generalized linear mixed models.

Install or access

lme4 is distributed on CRAN, the official R package repository. In R:

install.packages("lme4")library(lme4)

The example data set sleepstudy ships with lme4, so no download is needed.

Syntax and arguments

The usage in the lme4 reference manual is:

lmer(formula, data = NULL, REML = TRUE, control = lmerControl(),     start = NULL, verbose = 0L, subset, weights, na.action,     offset, contrasts = NULL, devFunOnly = FALSE)
  • formula: a two-sided formula. Random-effects terms are written with a vertical bar, such as (1 | Subject).
  • data: optional, but the package authors strongly recommend supplying it, because functions such as update and drop1 may fail otherwise.
  • REML: TRUE by default (restricted maximum likelihood); FALSE gives maximum likelihood.
  • control: optimizer and convergence settings, built with lmerControl().
  • weights, subset, na.action, offset, contrasts: as in other R modelling functions.

Collinear fixed effects are dropped automatically with a message; fixef(fit, add.dropped = TRUE) returns the dropped coefficients as NA. Do not pass the formula as a character vector; build it with as.formula() or reformulate().

Formula patterns

You write Meaning
(1 | g) Random intercept for each level of g
(1 | g1/g2) Nested: equivalent to (1 | g1) + (1 | g1:g2)
(1 | g1) + (1 | g2) Crossed random intercepts
x + (x | g) Same as 1 + x + (1 + x | g): correlated random intercept and slope
x + (x || g) Same as 1 + x + (1 | g) + (0 + x | g): uncorrelated intercept and slope

Source: lme4 vignette, formula table (read 2026-11-08). The double-bar || shorthand works only for numeric predictors with default settings; for a categorical predictor use diag(f | g).

Step-by-step: fit the sleepstudy model

The sleepstudy data hold 180 observations of three variables: Reaction (average reaction time in ms), Days of sleep restriction, and Subject (18 levels). They come from a study (Belenky et al., 2003) in which participants had normal sleep on day 0 and then 3 hours a night.

  1. Load the package: library(lme4)
  2. Fit the model with a random intercept and a random slope for Days by Subject: fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
  3. Print the results: summary(fm1)
  4. Extract pieces as needed: fixef(fm1), ranef(fm1), VarCorr(fm1), sigma(fm1), nobs(fm1).

Output and how to read it

The summary opens with “Linear mixed model fit by REML [‘lmerMod’]”, the formula, and “REML criterion at convergence: 1744”. Key values in the vignette output:

Block Row Values Reading
Random effects Subject (Intercept) variance 612.1, SD 24.74 Spread of subjects’ baseline reaction times
Random effects Subject Days variance 35.1, SD 5.92, correlation 0.07 Spread of subjects’ slopes; intercept-slope correlation
Random effects Residual variance 654.9, SD 25.59 Within-subject variation left unexplained
Fixed effects (Intercept) 251.41, SE 6.82, t 36.84 Mean reaction time at Days = 0
Fixed effects Days 10.47, SE 1.55, t 6.77 Average increase per day of restriction

The footer reports 180 observations in 18 Subject groups. Source: Bates, Mächler, Bolker and Walker, “Fitting Linear Mixed-Effects Models using lme4” (read 2026-11-08).

Why there are no p-values

The vignette states that the summary has no p-values. To get them, load the lmerTest package, which “provides p-values for type I, II, or III ANOVA and summary tables for lmer model fits from lme4” (CRAN, lmerTest 3.2-1, published 2026-03-05). It uses Satterthwaite degrees of freedom by default and can use Kenward-Roger via pbkrtest. Load lmerTest and refit:

library(lmerTest)fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)summary(fm1)

lmerTest also supplies step, drop1 and ranova for model selection. To compare nested models with a likelihood-ratio test, anova(fm1, fm2) refits the models with ML; see likelihood-ratio tests for nested models.

Troubleshooting

“boundary (singular) fit”

isSingular(fit) tests whether estimated parameters lie on the boundary of the parameter space, meaning variances at or near zero. The lme4 manual notes that complex mixed-effects models frequently produce singular fits. In an intercept-and-slope model it appears as near-zero variance or as a correlation of almost exactly -1 or 1; rePCA() gives more detail. Singular models are still statistically well defined; a common response is to simplify the random-effects structure.

Model refuses to update or drop terms

Supply the data argument explicitly (see above).

Correlated or uncorrelated slopes

Switch between (x | g) and (x || g) as in the formula table; with a categorical x use diag(f | g).

Free alternatives

Option What it is When it fits
lmerTest CRAN package that adds p-values and ANOVA tables to lme4 lmer fits You want the same model with significance tests
nlme (lme()) Another R mixed-model package; see lme in R You need nlme’s own syntax for the model
afex See the afex guide ANOVA-style workflows

FAQ

What is the difference between lmer and lme?

lmer() is from lme4; lme() is from nlme. They are separate packages with different formula syntax for random effects. See lme in R.

Is “lmer r” a different function from lmer in R?

No. It is the same lmer() function from lme4.

Does lmer use REML or ML?

REML by default (REML = TRUE); set REML = FALSE for maximum likelihood.

How do I get p-values from lmer?

Load lmerTest and refit, as shown above.

Sources

  • lme4 reference manual, version 2.1-0, CRAN: https://cran.r-project.org/web/packages/lme4/lme4.pdf (read 2026-11-08)
  • Bates, Mächler, Bolker, Walker. Fitting Linear Mixed-Effects Models using lme4: https://cran.r-project.org/web/packages/lme4/vignettes/lmer.pdf (read 2026-11-08)
  • lmerTest 3.2-1, CRAN: https://cran.r-project.org/web/packages/lmerTest/index.html (read 2026-11-08)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about lmer in R: Syntax, Arguments and a sleepstudy Example

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

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 →