Tutorials › Biostatistics › Mixed Models in R: lme4 and nlme

Longitudinal & Repeated-Measures Analysis

Mixed Models in R: lme4 and nlme

An in-depth practical guide to mixed-effects models for clinical research, covering repeated measures, random intercepts and slopes, treatment-by-time interactions, covariance structures, REML versus ML, estimated marginal means, model diagnostics, convergence, missing data, and implementation with lme4 and nlme.

Advanced 35 min read

What You'll Learn

  • Why mixed models are useful for clinical-trial longitudinal data
  • Fixed effects, random effects, and within-subject correlation
  • Random-intercept and random-slope models
  • How lme4 and nlme differ
  • Compound symmetry, AR(1), unstructured, and other covariance structures
  • REML versus maximum likelihood and model comparison
  • Treatment-by-time interactions and clinical interpretation
  • Estimated marginal means, contrasts, diagnostics, and reporting

Introduction

Clinical trials frequently collect repeated measurements from the same patient. Examples include blood pressure measured at multiple visits, tumor burden assessed repeatedly, laboratory biomarkers collected over time, pulmonary function measurements, pain scores, and patient-reported outcomes.

These observations create an important statistical problem: measurements from the same patient are generally correlated.

A conventional linear regression model assumes independent observations. Applying that assumption to repeated measurements can produce incorrect standard errors and therefore misleading statistical inference.

Mixed-effects models provide one of the most flexible frameworks for addressing this problem.

Core idea: A mixed model combines fixed effects, which describe systematic population-level relationships, with random effects, which represent subject-specific variation and induce correlation among repeated observations from the same subject.

Why Mixed Models Matter in Clinical Trials

Consider a randomized clinical trial comparing an experimental treatment with placebo. Suppose the primary continuous endpoint is measured at Weeks 4, 8, 12, 24, and 36.

The data might look conceptually like this:

USUBJID Treatment Visit Week Outcome
001 Active Week 4 4 82
001 Active Week 8 8 76
001 Active Week 12 12 71
002 Placebo Week 4 4 94
002 Placebo Week 8 8 92

The three measurements from Patient 001 are not three independent people. They belong to the same individual.

A mixed model explicitly accounts for this structure.

The Basic Linear Mixed Model

A useful starting point is:

$$ Y_{ij} = \mathbf{x}_{ij}^{T}\boldsymbol{\beta} + \mathbf{z}_{ij}^{T}\mathbf{b}_{i} + \epsilon_{ij} $$

where:

  • \(Y_{ij}\) is the outcome for subject \(i\) at measurement \(j\).
  • \(\boldsymbol{\beta}\) contains the fixed-effect parameters.
  • \(\mathbf{b}_i\) contains subject-specific random effects.
  • \(\mathbf{x}_{ij}\) describes the fixed-effect design.
  • \(\mathbf{z}_{ij}\) describes the random-effect design.
  • \(\epsilon_{ij}\) is the residual error.

The random effects are commonly assumed to follow:

$$ \mathbf{b}_i\sim N(\mathbf{0},\mathbf{D}) $$

and the residual errors may follow:

$$ \boldsymbol{\epsilon}_i \sim N(\mathbf{0},\mathbf{R}_i) $$

The distinction between \(\mathbf{D}\) and \(\mathbf{R}_i\) becomes extremely important when deciding whether to use lme4 or nlme.

Fixed Effects Versus Random Effects

Component Question it answers Typical clinical-trial example
Fixed effect What is the population-average relationship? Treatment effect
Fixed effect Does the outcome change over time? Visit effect
Fixed interaction Do treatment groups change differently? Treatment × Visit
Random intercept Do patients have different baseline levels? Subject-specific baseline biomarker level
Random slope Do patients have different rates of change? Different longitudinal trajectories

A Clinical Example

Suppose a trial measures a continuous biomarker at baseline and Weeks 4, 8, 12, and 24.

The scientific question is:

Primary longitudinal question: Does the mean change in the biomarker over time differ between the active and placebo treatment groups?

This naturally suggests a model containing:

  • Treatment
  • Visit
  • Treatment × Visit
  • A subject-level random effect

A typical model might be written:

$$ Y_{ij} = \beta_0 + \beta_1 TRT_i + \beta_2 TIME_j + \beta_3(TRT_i\times TIME_j) + b_i + \epsilon_{ij} $$

Here, \(\beta_3\) represents the treatment-by-time interaction.

Why the Treatment-by-Time Interaction Matters

Suppose the treatment effect is zero at baseline but becomes increasingly negative after treatment begins.

A model containing only treatment and time would force the treatment effect to be constant unless additional structure were introduced.

The interaction allows treatment effects to vary by visit.

For a categorical visit variable, the model can estimate a treatment contrast at each post-baseline visit.

Clinical-trial interpretation: In many longitudinal efficacy analyses, the treatment-by-visit interaction is central because the treatment difference at Week 24 may be scientifically more important than an overall treatment coefficient averaged across all visits.

Visualizing Patient-Level Variation

Time Outcome Population mean trajectory

The individual lines represent subjects. The gold line represents a population-level mean trajectory.

A mixed model allows both levels of information to coexist mathematically: population-level effects are represented by fixed effects, while subject-specific deviations are represented by random effects.

Random Intercept Models

The simplest useful longitudinal mixed model gives each subject their own intercept.

$$ Y_{ij} = \beta_0 + \beta_1X_{ij} + b_{0i} + \epsilon_{ij} $$

The subject-specific intercept is:

$$ \beta_0+b_{0i} $$

Therefore, patients can start at different levels while sharing the same average slope.

Clinical Interpretation

Suppose the endpoint is systolic blood pressure.

One patient may have a baseline level of 150 mmHg and another 125 mmHg. A random-intercept model acknowledges that these patients begin at different levels.

The model does not require every patient to have the same baseline outcome.

Random Intercept in lme4

library(lme4)

fit_ri <- lmer(
  outcome ~ treatment + week + (1 | USUBJID),
  data = dat,
  REML = TRUE
)

summary(fit_ri)

The term:

(1 | USUBJID)

means that each subject receives a subject-specific random intercept.

Random Slopes

Patients may differ not only in their baseline levels but also in their rates of change.

A random-slope model can be written:

$$ Y_{ij} = \beta_0 + \beta_1t_{ij} + b_{0i} + b_{1i}t_{ij} + \epsilon_{ij} $$

The individual slope becomes:

$$ \beta_1+b_{1i} $$

Thus, different patients can follow different trajectories.

Random Intercept and Random Slope in lme4

fit_rs <- lmer(
  outcome ~ treatment * week +
    (1 + week | USUBJID),
  data = dat,
  REML = TRUE
)

The expression:

(1 + week | USUBJID)

allows each subject to have:

  • A random intercept
  • A random slope for week
  • A covariance between the random intercept and slope

What Does the Random-Effect Correlation Mean?

Suppose the model estimates a negative correlation between random intercepts and random slopes.

This could indicate that patients with higher baseline outcomes tend to have more negative longitudinal slopes.

The correlation is not automatically a treatment effect.

It describes the relationship between subject-specific deviations.

Removing the Random-Effect Correlation

In lme4, the notation:

(1 + week | USUBJID)

estimates a correlated random intercept and slope.

The notation:

(1 + week || USUBJID)

removes the intercept-slope covariance.

This can sometimes improve model stability.

Important: Removing the random-effect correlation is not merely a computational trick. It changes the statistical model. The simpler structure should be justified scientifically, statistically, or both.

Centering Time

Time is often easier to interpret when centered.

For example:

dat$week_c <- dat$week - 12

Now the model intercept corresponds to the expected outcome at Week 12 rather than at Week 0.

Centering can be particularly helpful when interpreting random intercepts.

Why Time Coding Matters

Consider:

outcome ~ treatment * week

versus:

outcome ~ treatment * factor(week)

These are different models.

Time representation Interpretation
Numeric week Assumes a linear time trend unless additional terms are included
Factor week Allows a separate mean at each visit
Polynomial time Allows nonlinear systematic trajectories
Spline time Allows flexible nonlinear trajectories
Clinical-trial caution: Do not automatically treat nominal visit as a numeric continuous variable. If the SAP specifies visit as categorical, fitting a linear week effect changes the estimand and imposes a strong functional-form assumption.

Categorical Visit Models

A common longitudinal clinical-trial model uses visit as a factor:

dat$AVISIT <- factor(
  dat$AVISIT,
  levels = c(
    "Baseline",
    "Week 4",
    "Week 8",
    "Week 12",
    "Week 24"
  )
)

fit_visit <- lmer(
  outcome ~ treatment * AVISIT +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

This estimates treatment differences at the individual visits rather than assuming that the treatment effect changes linearly over time.

The Baseline Visit Requires Care

Clinical-trial longitudinal models frequently include a baseline observation, but baseline handling requires careful thought.

For example, an analysis might model:

  • Change from baseline as the dependent variable
  • Observed post-baseline outcome with baseline adjustment
  • Repeated post-baseline outcomes with baseline as part of the covariance structure

These are not interchangeable formulations.

Change From Baseline

Suppose:

$$ CHG_{ij}=Y_{ij}-Y_{i0} $$

A mixed model can then use change from baseline as the outcome.

fit_chg <- lmer(
  CHG ~ treatment * AVISIT +
    (1 | USUBJID),
  data = postbaseline,
  REML = TRUE
)

This can be appropriate when the estimand and analysis plan are defined in terms of change from baseline.

Baseline Adjustment

Another approach is to model the post-baseline outcome while adjusting for baseline:

fit_adj <- lmer(
  outcome ~ baseline +
    treatment * AVISIT +
    (1 | USUBJID),
  data = postbaseline,
  REML = TRUE
)

This is conceptually closer to an ANCOVA-style analysis extended across multiple post-baseline visits.

Do not assume that change-from-baseline and baseline-adjusted models are automatically equivalent. The interpretation, assumptions, estimand, and treatment-effect parameter can differ.

Entering the World of nlme

The nlme package is especially important because it provides explicit facilities for modeling the residual covariance structure.

This is one of the most important practical differences between lme4 and nlme.

Capability lme4 nlme
Linear mixed models Excellent Excellent
Random intercepts Yes Yes
Random slopes Yes Yes
Explicit residual covariance structures Limited Extensive
AR(1) residual correlation Not directly through the same framework Yes
Heterogeneous residual variances Limited Yes
Nonlinear mixed models No Yes
Generalized mixed models Yes through glmer Different framework

Basic nlme Syntax

library(nlme)

fit_nlme <- lme(
  fixed = outcome ~ treatment * AVISIT,
  random = ~ 1 | USUBJID,
  data = dat,
  method = "REML"
)

summary(fit_nlme)

The syntax is somewhat different from lme4, but the underlying mixed-model concepts are the same.

Random Slopes in nlme

fit_nlme_rs <- lme(
  fixed = outcome ~ treatment * week,
  random = ~ week | USUBJID,
  data = dat,
  method = "REML"
)

The expression:

random = ~ week | USUBJID

corresponds to a subject-specific random intercept and random slope.

Random Effects Versus Residual Correlation

This distinction is fundamental.

There are two broad ways repeated observations can become correlated:

  1. Through shared random effects.
  2. Through a structured residual covariance matrix.

Mixed models can use either or both mechanisms.

Compound Symmetry

Under compound symmetry, observations from the same patient have equal pairwise correlation regardless of how far apart they are in time.

Conceptually:

$$ \mathbf{R} = \sigma^2 \begin{pmatrix} 1 & \rho & \rho & \rho\\ \rho & 1 & \rho & \rho\\ \rho & \rho & 1 & \rho\\ \rho & \rho & \rho & 1 \end{pmatrix} $$

This may be reasonable when correlation is approximately constant across visits.

AR(1) Correlation

An autoregressive AR(1) structure assumes that observations closer together in time are more strongly correlated.

The correlation between two observations can be represented as:

$$ Corr(Y_{ij},Y_{ik}) = \rho^{|t_j-t_k|} $$

for equally spaced time points under the simplest formulation.

Thus:

  • Adjacent visits have correlation \(\rho\).
  • Visits two time units apart have correlation \(\rho^2\).
  • Visits three time units apart have correlation \(\rho^3\).

AR(1) in nlme

fit_ar1 <- lme(
  fixed = outcome ~ treatment * AVISIT,
  random = ~ 1 | USUBJID,
  correlation = corAR1(
    form = ~ week | USUBJID
  ),
  data = dat,
  method = "REML"
)

summary(fit_ar1)

This is one of the major reasons nlme remains particularly useful for clinical longitudinal analyses.

Continuous-Time Correlation

When assessments occur at irregular times, the assumption that visits are equally spaced may be inappropriate.

In such settings, a continuous-time correlation structure may be preferable.

corCAR1(
  form = ~ day | USUBJID
)

The key idea is that correlation is related to elapsed time rather than merely the visit number.

Practical point: If one patient is assessed on Day 28 and another on Day 35, but both are labeled "Week 4," a correlation model based on actual elapsed time and one based on nominal visit can imply different covariance assumptions.

Heterogeneous Variances

Clinical-trial outcomes may have different residual variances at different visits.

For example, variability may increase as patients are followed longer.

The nlme package provides variance functions for this situation.

fit_het <- lme(
  fixed = outcome ~ treatment * AVISIT,
  random = ~ 1 | USUBJID,
  weights = varIdent(
    form = ~ 1 | AVISIT
  ),
  data = dat,
  method = "REML"
)

This allows residual variance to differ by visit.

Combining Heterogeneous Variance and AR(1)

A particularly useful model for longitudinal clinical data can include both:

  • A correlation structure
  • Visit-specific residual variances
fit_ar1_het <- lme(
  fixed = outcome ~ treatment * AVISIT,
  random = ~ 1 | USUBJID,
  correlation = corAR1(
    form = ~ week | USUBJID
  ),
  weights = varIdent(
    form = ~ 1 | AVISIT
  ),
  data = dat,
  method = "REML"
)

This is much closer to the type of covariance modeling often encountered in serious longitudinal clinical-trial work than a simple random-intercept model.

Random Effects and Residual Covariance Are Not the Same Thing

Consider a model with a random intercept:

$$ Y_{ij} = \beta_0+b_i+\epsilon_{ij} $$

Because all observations from a patient share \(b_i\), they become correlated.

However, this induced correlation has a particular structure.

A random-intercept model therefore should not automatically be interpreted as an arbitrary repeated-measures covariance model.

Common mistake: "Using a random intercept handles all repeated-measures covariance." It handles subject-level heterogeneity and induces correlation, but it does not reproduce every possible residual covariance structure.

lme4 Versus nlme: A Practical Decision

1
Use lme4 when your primary requirement is a flexible and efficient random-effects model.
2
Use nlme when explicit residual correlation or variance structures are important.
3
Use both during development when you want to understand how different representations of within-subject dependence affect the analysis.
4
Follow the SAP when performing a confirmatory clinical-trial analysis.

A Complete Simulated Clinical Trial

We will now build a small simulated longitudinal trial from the ground up.

Suppose:

  • 200 patients are randomized.
  • 100 receive Active treatment.
  • 100 receive Placebo.
  • The endpoint is a continuous biomarker.
  • Measurements occur at Weeks 0, 4, 8, 12, and 24.

Generate the Data

set.seed(2026)

n_subjects <- 200

subjects <- data.frame(
  USUBJID = sprintf("SUBJ-%03d", 1:n_subjects),
  treatment = rep(
    c("Placebo", "Active"),
    each = n_subjects / 2
  )
)

visits <- c(0, 4, 8, 12, 24)

dat <- merge(
  subjects,
  data.frame(week = visits)
)

dat <- dat[
  order(dat$USUBJID, dat$week),
]

dat$USUBJID <- factor(dat$USUBJID)

dat$treatment <- factor(
  dat$treatment,
  levels = c("Placebo", "Active")
)

Simulate Patient-Level Random Effects

subject_re <- data.frame(
  USUBJID = levels(dat$USUBJID),
  random_intercept = rnorm(
    n_subjects,
    mean = 0,
    sd = 8
  )
)

dat <- merge(
  dat,
  subject_re,
  by = "USUBJID"
)

dat <- dat[
  order(dat$USUBJID, dat$week),
]

Generate the Outcome

dat$outcome <-
  100 +
  0.05 * dat$week +
  ifelse(
    dat$treatment == "Active",
    -0.30 * dat$week,
    0
  ) +
  dat$random_intercept +
  rnorm(nrow(dat), 0, 5)

The active treatment therefore has a more favorable longitudinal trajectory.

Fit the Basic lme4 Model

library(lme4)

fit_lme4 <- lmer(
  outcome ~ treatment * week +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

summary(fit_lme4)

Interpreting the Fixed Effects

Because treatment uses Placebo as the reference level:

Coefficient Conceptual interpretation
(Intercept) Expected outcome for Placebo at Week 0
treatmentActive Active versus Placebo difference at Week 0
week Time slope for Placebo
treatmentActive:week Difference in longitudinal slopes between Active and Placebo

The treatment-by-week interaction is therefore the key parameter if the scientific question concerns differential longitudinal change.

Extracting Fixed Effects

fixef(fit_lme4)

To obtain confidence intervals:

confint(
  fit_lme4,
  parm = "beta_",
  method = "Wald"
)

Different confidence-interval methods can have different computational and small-sample properties. The method should be chosen deliberately rather than automatically.

Extracting Random Effects

ranef(fit_lme4)

The result contains estimated subject-specific deviations from the population fixed effects.

These are often called empirical Bayes or conditional-mode estimates, depending on the modeling framework and terminology.

Random Effects Are Not Ordinary Fixed-Effect Estimates

A common conceptual error is to interpret every estimated random effect as though it were a precisely estimated individual parameter.

Random effects are shrunk toward the population mean.

Patients with limited information tend to have stronger shrinkage toward zero than patients with extensive information.

Clinical interpretation: Subject-specific random effects are useful for describing heterogeneity and constructing predictions, but they should not automatically be interpreted as independent treatment effects for individual patients.

Adding a Random Slope

fit_lme4_rs <- lmer(
  outcome ~ treatment * week +
    (1 + week | USUBJID),
  data = dat,
  REML = TRUE
)

summary(fit_lme4_rs)

This model allows patients to differ in both baseline level and rate of change.

Comparing Random-Effects Structures

When comparing models that differ only in random-effects structure, likelihood comparisons can be informative.

anova(
  fit_lme4,
  fit_lme4_rs
)

However, mixed-model comparison requires careful attention to estimation method, nesting, boundary parameters, and the scientific purpose of the comparison.

Do not blindly apply a likelihood-ratio-test p-value to random-effects variance components. Testing whether a variance component is exactly zero is a boundary problem, so the usual regular asymptotic assumptions can fail.

REML Versus Maximum Likelihood

Mixed models are commonly estimated using either:

  • Restricted maximum likelihood (REML)
  • Maximum likelihood (ML)

REML estimates variance components while accounting for the loss of degrees of freedom associated with estimating fixed effects.

Maximum likelihood estimates all parameters under the likelihood formulation.

When Should REML Be Used?

REML is often preferred for final estimation of variance components when the fixed-effects structure is already established.

lmer(
  outcome ~ treatment * week +
    (1 + week | USUBJID),
  data = dat,
  REML = TRUE
)

When Should ML Be Used?

When comparing models with different fixed-effects structures using likelihood criteria, ML is generally used rather than REML.

fit_ml_1 <- lmer(
  outcome ~ treatment + week +
    (1 | USUBJID),
  data = dat,
  REML = FALSE
)

fit_ml_2 <- lmer(
  outcome ~ treatment * week +
    (1 | USUBJID),
  data = dat,
  REML = FALSE
)

anova(
  fit_ml_1,
  fit_ml_2
)
Rule of thumb: Use ML when comparing models with different fixed effects. Once the fixed structure has been selected, refit the final model using REML when appropriate for the analysis objective.

AIC and BIC

Information criteria can help compare competing models:

AIC(
  fit_ml_1,
  fit_ml_2
)

BIC(
  fit_ml_1,
  fit_ml_2
)

Lower AIC or BIC generally indicates a preferred balance between fit and complexity under the corresponding criterion.

These criteria should not replace scientific judgment.

Modeling Time as a Factor

For many confirmatory longitudinal analyses, visit is categorical.

dat$visit <- factor(
  dat$week,
  levels = c(0, 4, 8, 12, 24)
)

fit_factor <- lmer(
  outcome ~ treatment * visit +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

This allows the mean trajectory to take a different shape at each scheduled visit.

Why Factor Visit Is Often Attractive in Clinical Trials

Suppose the observed mean trajectory is:

$$ 100\rightarrow99\rightarrow94\rightarrow82\rightarrow81 $$

A linear time effect would impose a straight-line relationship.

A factor-time model does not.

It can estimate the Week 4, Week 8, Week 12, and Week 24 effects independently, subject to the model structure.

Estimated Marginal Means

In clinical reporting, the raw model coefficients are often not the most useful quantities to present.

Estimated marginal means are frequently more interpretable.

library(emmeans)

emm <- emmeans(
  fit_factor,
  ~ treatment | visit
)

emm

This provides estimated treatment means at each visit.

Treatment Comparisons at Each Visit

contrast(
  emm,
  method = "revpairwise"
)

A common clinical question is:

At Week 24, what is the estimated difference between Active and Placebo?

The emmeans framework makes such contrasts much easier to obtain than manually reconstructing them from coefficient tables.

Confidence Intervals for Treatment Differences

pairs(
  emm,
  adjust = "none"
)

Multiplicity adjustment should follow the statistical analysis plan rather than being selected solely because it produces a preferred presentation.

Example of a Clinical Summary Table

Visit Active LS Mean Placebo LS Mean Difference 95% CI
Week 4 96.2 99.1 −2.9 (−5.8, 0.0)
Week 8 91.3 98.2 −6.9 (−10.1, −3.7)
Week 12 86.4 97.0 −10.6 (−14.0, −7.2)
Week 24 83.1 96.4 −13.3 (−17.0, −9.6)

The numbers above are illustrative.

Why This Is More Useful Than the Raw Coefficient Table

A coefficient such as:

treatmentActive:visit24

depends on the model parameterization and reference categories.

The estimated marginal mean contrast directly answers the clinical question:

$$ \widehat{\mu}_{Active,24} - \widehat{\mu}_{Placebo,24} $$

Covariance Structures in Clinical Longitudinal Data

One of the most important advanced decisions is how to represent within-patient correlation.

Common choices include:

Structure Main assumption Typical use
Compound symmetry Equal correlation between repeated measurements Simple repeated measures
AR(1) Correlation decreases with time separation Regular longitudinal assessments
Continuous AR(1) Correlation decreases with actual elapsed time Irregular assessment timing
Unstructured Each variance/covariance estimated separately Few visits, flexible covariance
Heterogeneous variance Variance differs by visit/group Changing variability over follow-up

Unstructured Covariance

With four visits, an unstructured covariance matrix can be represented as:

$$ \mathbf{R} = \begin{pmatrix} \sigma_1^2 & \sigma_{12} & \sigma_{13} & \sigma_{14}\\ \sigma_{12} & \sigma_2^2 & \sigma_{23} & \sigma_{24}\\ \sigma_{13} & \sigma_{23} & \sigma_3^2 & \sigma_{34}\\ \sigma_{14} & \sigma_{24} & \sigma_{34} & \sigma_4^2 \end{pmatrix} $$

Every variance and covariance is estimated.

This is flexible but can require many parameters.

Why Unstructured Covariance Can Become Difficult

With \(m\) repeated measurements, an unstructured covariance matrix requires:

$$ \frac{m(m+1)}{2} $$

variance and covariance parameters.

For 5 visits:

$$ \frac{5(6)}{2}=15 $$

parameters are required.

For 10 visits:

$$ \frac{10(11)}{2}=55 $$

parameters are required.

This illustrates why covariance selection becomes increasingly important as the number of repeated assessments grows.

Using nlme for More Explicit Covariance Modeling

fit_cs <- lme(
  fixed = outcome ~ treatment * AVISIT,
  random = ~ 1 | USUBJID,
  correlation = corCompSymm(
    form = ~ week | USUBJID
  ),
  data = dat,
  method = "REML"
)

fit_ar1 <- lme(
  fixed = outcome ~ treatment * AVISIT,
  random = ~ 1 | USUBJID,
  correlation = corAR1(
    form = ~ week | USUBJID
  ),
  data = dat,
  method = "REML"
)

Comparing Covariance Structures

A common development workflow is:

1
Specify the fixed-effects structure based on the scientific question and SAP.
2
Fit plausible covariance structures.
3
Compare AIC/BIC and likelihood-based measures where appropriate.
4
Check convergence and parameter estimates.
5
Inspect residual diagnostics.
6
Select the prespecified or justified covariance model.
Important: Covariance selection should not be driven solely by whichever model produces the smallest p-value for treatment. The covariance model describes dependence and variance; it should be selected based on the design, scientific plausibility, diagnostics, and the prespecified analysis strategy.

Missing Data

Longitudinal clinical-trial datasets commonly contain missing post-baseline measurements.

Mixed models are attractive partly because they can use available observations without requiring every patient to have complete follow-up.

Under standard likelihood-based estimation, this can provide valid inference under a missing-at-random framework when the model is correctly specified and the assumptions underlying the missingness mechanism are appropriate.

MAR Does Not Mean "Missing Data Are Ignored"

Suppose patients with worsening disease are more likely to discontinue.

The probability of missingness may depend on observed prior outcomes.

A likelihood-based mixed model can potentially accommodate such a mechanism under MAR.

But the model does not magically make missingness irrelevant.

Critical distinction: Mixed models do not automatically solve missing-data bias. The validity of the analysis depends on assumptions about the missing-data mechanism and correct specification of the outcome model.

Missing Not at Random

If the probability of missingness depends on an unobserved outcome after conditioning on observed information, the missingness mechanism may be missing-not-at-random (MNAR).

Sensitivity analyses may then be needed.

Potential approaches include:

  • Pattern-mixture models
  • Selection models
  • Reference-based imputation
  • Tipping-point analyses

The appropriate strategy depends on the estimand and trial context.

Dropout Is Not the Same as a Missing Value

In clinical-trial reporting, it is useful to distinguish:

  • Intermittent missing assessments
  • Permanent discontinuation
  • Death
  • Administrative censoring
  • Missing data caused by treatment failure

These may have different implications for the estimand and sensitivity analysis.

Diagnostics

A mixed model should not be accepted simply because the software returned an estimate.

Diagnostics should be part of the analysis workflow.

Residuals Versus Fitted Values

plot(
  fitted(fit_lme4),
  resid(fit_lme4)
)

abline(
  h = 0,
  lty = 2
)

This can reveal:

  • Nonlinearity
  • Heteroscedasticity
  • Outliers
  • Model misspecification

Normal Q-Q Plot

qqnorm(
  resid(fit_lme4)
)

qqline(
  resid(fit_lme4)
)

The normality assumption is primarily relevant to the inferential framework and residual distribution; modest deviations do not automatically invalidate a mixed model.

Checking Random Effects

qqnorm(
  ranef(fit_lme4)$USUBJID[[1]]
)

qqline(
  ranef(fit_lme4)$USUBJID[[1]]
)

Strong departures from the assumed random-effects distribution can indicate model inadequacy, although diagnostics should be interpreted in context.

Singular Fits in lme4

One common warning is:

boundary (singular) fit

This can occur when the estimated random-effects covariance matrix is on the boundary of the parameter space.

For example, a random-slope variance may be estimated essentially as zero.

isSingular(
  fit_lme4_rs,
  tol = 1e-4
)

A singular fit is not merely an annoying warning to suppress.

Do not simply increase the optimizer iterations and declare victory. If a variance component is effectively zero or a random-effects covariance matrix is poorly identified, the model structure itself may need to be reconsidered.

Convergence Problems

Mixed models are nonlinear optimization problems and can sometimes fail to converge cleanly.

Potential causes include:

  • Overly complex random-effects structures
  • Highly correlated predictors
  • Poor scaling of continuous variables
  • Too few observations per subject
  • Too little information to estimate covariance parameters
  • Boundary solutions

Scaling Time

If time is measured in days:

outcome ~ treatment * day +
  (1 + day | USUBJID)

the random slope can become numerically difficult if the time scale is large.

Using weeks:

dat$week <- dat$day / 7

may make the parameters easier to interpret and sometimes improve numerical behavior.

Alternative Optimizers

For difficult lme4 models, alternative optimizers can sometimes help diagnose optimization issues.

fit_bobyqa <- lmer(
  outcome ~ treatment * week +
    (1 + week | USUBJID),
  data = dat,
  REML = TRUE,
  control = lmerControl(
    optimizer = "bobyqa"
  )
)

Optimizer changes should not be used to conceal a fundamentally unidentified model.

Model Validation Workflow

1
Confirm the analysis population.
2
Confirm baseline and endpoint derivations.
3
Confirm treatment and visit coding.
4
Confirm the fixed-effects specification.
5
Confirm the random-effects specification.
6
Confirm the residual covariance structure.
7
Check convergence and singularity.
8
Review residual and random-effect diagnostics.
9
Generate estimated marginal means and contrasts.
10
Independently validate reported estimates against source data.

Nested Random Effects

Clinical data sometimes contain hierarchical structures such as:

  • Patients within sites
  • Patients within investigators
  • Measurements within patients
  • Patients within treatment centers

For example:

outcome ~ treatment * visit +
  (1 | site/USUBJID)

The notation represents nested random effects.

This can be expanded conceptually as:

(1 | site) +
(1 | site:USUBJID)

Crossed Random Effects

Not all hierarchical structures are nested.

If observations are associated with multiple non-nested grouping factors, crossed random effects may be appropriate.

outcome ~ treatment +
  (1 | patient) +
  (1 | assessor)

This could be relevant in some measurement studies where multiple assessors evaluate multiple patients.

Multiple Random Effects

A model can contain multiple sources of random variation:

lmer(
  outcome ~ treatment * visit +
    (1 | site) +
    (1 | USUBJID),
  data = dat
)

Whether such a structure is justified depends on the design and amount of information available for each variance component.

Random Slopes by Treatment?

A common question is whether treatment should appear in the random-effects structure.

Usually, treatment is a fixed effect when the scientific objective is to estimate the treatment effect.

The random slope typically represents subject-specific variation in a continuous covariate such as time.

(1 + week | USUBJID)

is therefore fundamentally different from:

(1 + treatment | USUBJID)

The latter requires a meaningful subject-level replication structure for the random treatment coefficient and is generally not the standard representation for a randomized treatment effect.

Repeated Measures Versus Longitudinal Random Effects

These concepts overlap but are not identical.

A repeated-measures model can represent within-subject covariance directly. A random-effects model represents subject-specific deviations that induce correlation.

In practice, the two approaches can produce similar covariance patterns in some cases.

But the implied covariance matrices are not generally identical.

Clinical-Trial Example: Blood Pressure

Suppose a trial measures systolic blood pressure at:

  • Baseline
  • Week 2
  • Week 4
  • Week 8
  • Week 12

A plausible model might be:

fit_bp <- lmer(
  SBP_change ~ treatment * visit +
    baseline_SBP +
    (1 | USUBJID),
  data = bp_dat,
  REML = TRUE
)

The treatment difference at Week 12 can then be obtained with:

emmeans(
  fit_bp,
  pairwise ~ treatment | visit
)

Clinical-Trial Example: Oncology Biomarker

Suppose a biomarker is measured repeatedly and the scientific objective is to compare longitudinal treatment trajectories.

fit_bio <- lmer(
  log_biomarker ~ treatment * visit +
    baseline_log_biomarker +
    (1 + week | USUBJID),
  data = biomarker_dat,
  REML = TRUE
)

The log transformation may be appropriate when the biomarker distribution is strongly right-skewed or multiplicative effects are scientifically meaningful.

Transformation matters: If the model is fitted on the log scale, treatment differences on the model scale are not automatically equivalent to arithmetic differences on the original scale. Back-transformation and interpretation must be handled carefully.

Clinical-Trial Example: Patient-Reported Outcome

Patient-reported outcomes often have repeated assessments and substantial between-patient heterogeneity.

fit_pro <- lmer(
  PRO_change ~ treatment * visit +
    baseline_PRO +
    (1 | USUBJID),
  data = pro_dat,
  REML = TRUE
)

If the instrument has unusual distributional properties, the appropriateness of a Gaussian mixed model should be evaluated rather than assumed.

Generalized Mixed Models

Although this tutorial focuses on linear mixed models, the same conceptual framework extends to non-Gaussian outcomes.

For binary outcomes, lme4 provides glmer().

fit_binary <- glmer(
  response ~ treatment * visit +
    (1 | USUBJID),
  data = dat_binary,
  family = binomial
)

This is a generalized linear mixed model rather than a linear mixed model.

Do not interchange lmer() and glmer(). The outcome distribution, link function, interpretation of coefficients, and estimation procedure differ.

What lme4 Does Especially Well

  • Clear random-effects formula syntax
  • Efficient estimation for many mixed-model applications
  • Random intercepts and slopes
  • Nested and crossed random effects
  • Generalized mixed models through glmer()
  • Useful extraction methods for fitted models

What nlme Does Especially Well

  • Explicit residual correlation structures
  • AR(1) modeling
  • Continuous-time correlation structures
  • Heterogeneous residual variances
  • Flexible variance functions
  • Nonlinear mixed-effects models

A More Advanced nlme Model

Consider:

fit_advanced <- lme(
  fixed =
    outcome ~ treatment * AVISIT + baseline,
  random =
    ~ 1 | USUBJID,
  correlation =
    corAR1(
      form = ~ week | USUBJID
    ),
  weights =
    varIdent(
      form = ~ 1 | AVISIT
    ),
  data = dat,
  method = "REML"
)

This model includes:

  • Fixed treatment effects
  • Fixed visit effects
  • Treatment-by-visit interaction
  • Baseline adjustment
  • Subject-specific random intercept
  • AR(1) residual correlation
  • Visit-specific residual variance

That is a substantially richer representation of longitudinal dependence than a basic random-intercept model.

Model Complexity Must Be Earned

A more complicated covariance model is not automatically a better model.

Every additional variance or covariance parameter requires information.

A useful principle is:

Choose the simplest covariance structure that adequately represents the data and is consistent with the scientific design and analysis plan.

Common Mistake: Random Intercept by Default

A very common workflow is:

lmer(
  outcome ~ treatment * visit +
    (1 | USUBJID),
  data = dat
)

followed by assuming that the repeated-measures problem has been completely resolved.

That may be adequate.

But it should be evaluated rather than assumed.

The subject-level random intercept imposes a particular covariance structure.

Common Mistake: Random Slope by Default

The opposite mistake is fitting:

(1 + week | USUBJID)

to every dataset simply because longitudinal observations exist.

A random slope requires enough within-subject information to estimate subject-specific changes.

With only two observations per patient, a complex random-effects structure may be poorly identified.

Common Mistake: Treating Every Visit as Independent

A model such as:

lm(
  outcome ~ treatment * visit,
  data = dat
)

does not account for repeated observations from the same patient.

The standard errors can therefore be inappropriate.

Common Mistake: Ignoring Treatment Assignment in the Longitudinal Structure

A model such as:

outcome ~ treatment + visit

assumes the treatment effect is constant across visits.

If the scientific question concerns whether trajectories differ, the treatment-by-visit interaction should be considered.

Common Mistake: Automatically Using Numeric Visit

This:

treatment * week

is a linear-time model.

This:

treatment * factor(week)

is a categorical-visit model.

They answer different questions.

Common Mistake: Comparing REML Fits With Different Fixed Effects

Suppose:

fit1 <- lmer(
  outcome ~ treatment + visit +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

fit2 <- lmer(
  outcome ~ treatment * visit +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

Comparing their REML likelihoods for fixed-effect selection is generally not the appropriate approach.

Use ML for fixed-effect model comparison:

fit1_ml <- update(
  fit1,
  REML = FALSE
)

fit2_ml <- update(
  fit2,
  REML = FALSE
)

anova(
  fit1_ml,
  fit2_ml
)

Common Mistake: Confusing Random Effects With Repeated-Measures Covariance

A random intercept captures subject-specific heterogeneity.

An AR(1) residual structure describes how residual correlation changes with time separation.

These are related concepts but not interchangeable.

Common Mistake: Reporting Only the p-Value

A clinical-trial mixed-model result should generally include:

  • Estimated treatment difference
  • Confidence interval
  • P-value where appropriate
  • Analysis visit/time point
  • Analysis population
  • Model specification
  • Covariance/random-effects structure

A p-value without the estimated treatment effect provides limited clinical information.

Reporting a Mixed-Model Result

A concise report might state:

Example: At Week 24, the estimated least-squares mean difference between the active and placebo treatment groups was −13.3 units (95% CI: −17.0 to −9.6). The analysis used a linear mixed-effects model including treatment, visit, and treatment-by-visit interaction, with baseline outcome adjustment and a subject-level random intercept.

The exact wording and estimand should follow the statistical analysis plan.

Clinical Study Report Considerations

Item Should be specified?
Analysis population Yes
Outcome derivation Yes
Baseline definition Yes
Fixed effects Yes
Time coding Yes
Random effects Yes
Residual covariance Yes when applicable
Estimation method Yes
Missing-data assumptions Yes
Contrasts and estimands Yes
Multiplicity Yes when applicable

Model Specification Example

A full statistical-programming specification might read conceptually:

Fixed effects

Outcome = Treatment + Visit + Treatment × Visit + Baseline

Random effects

Subject-specific random intercept

Residual covariance

AR(1) correlation within subject

Estimation

Restricted maximum likelihood

Traceability in Clinical Programming

For regulated clinical-trial analyses, the model result should be traceable from the source analysis dataset through derived variables to the final reported table or figure.

1
SDTM/source data
2
Analysis dataset
3
Endpoint derivation
4
Analysis population
5
Mixed-model program
6
Estimated marginal means and contrasts
7
CSR table/figure
8
Independent QC and validation

Reproducibility

A clinical-trial mixed-model program should record:

  • R version
  • Package versions
  • Model specification
  • Analysis dataset version
  • Derivation programs
  • Random seed when simulation or stochastic procedures are involved
  • Model convergence status
  • Warnings
  • Output-generation code

Package Versions

sessionInfo()

packageVersion("lme4")
packageVersion("nlme")
packageVersion("emmeans")

For regulated environments, package versions should be controlled and documented as part of the validated or appropriately qualified computational environment.

Extracting a Clean Results Dataset

For reporting, it is often useful to transform model-derived results into a structured dataset.

emm_results <- as.data.frame(
  emmeans(
    fit_factor,
    ~ treatment | visit
  )
)

contrast_results <- as.data.frame(
  pairs(
    emmeans(
      fit_factor,
      ~ treatment | visit
    )
  )
)

The resulting datasets can feed standardized table-generation programs.

Predicted Trajectories

Mixed models can also generate predicted trajectories.

newdat <- expand.grid(
  treatment = levels(dat$treatment),
  week = seq(0, 24, by = 1)
)

newdat$pred <- predict(
  fit_lme4,
  newdata = newdat,
  re.form = NA
)

The argument:

re.form = NA

requests predictions based on the population-level fixed effects rather than subject-specific random effects.

Population-Level Versus Subject-Level Prediction

Prediction Meaning
Fixed-effects prediction Expected population-level trajectory
Conditional prediction Trajectory incorporating estimated subject-specific random effects

The distinction matters greatly when creating clinical figures.

Visualization of Estimated Treatment Trajectories

library(ggplot2)

ggplot(
  newdat,
  aes(
    x = week,
    y = pred,
    color = treatment
  )
) +
  geom_line(
    linewidth = 1
  ) +
  labs(
    x = "Week",
    y = "Estimated Mean Outcome",
    color = "Treatment"
  ) +
  theme_classic()

For a publication or CSR, confidence intervals around the estimated marginal means should generally be included where they add useful inferential context.

Model-Based Versus Raw Means

A frequent reporting mistake is to call model-adjusted estimates "observed means."

They are not the same.

Quantity Description
Observed mean Arithmetic mean of observed data
LS mean / estimated marginal mean Model-based estimated mean under specified covariate conditions
Subject-specific prediction Prediction incorporating random effects
Population-level prediction Prediction based on fixed effects

Unbalanced Data

One advantage of mixed models is their ability to work with unbalanced longitudinal data.

For example:

Patient Baseline Week 4 Week 8 Week 12 Week 24
001 ✓ ✓ ✓ ✓ ✓
002 ✓ ✓ — ✓ —
003 ✓ — — ✓ ✓

A mixed model can use the available observations under its modeling assumptions rather than automatically discarding every patient with an incomplete trajectory.

But Unbalanced Does Not Mean Unproblematic

Suppose patients who deteriorate rapidly are more likely to discontinue.

The observed data could then become increasingly enriched for patients doing well.

The mixed model can account for observed longitudinal information under an appropriate missingness assumption, but the analyst must still investigate the dropout process.

Sensitivity Analyses

For important confirmatory analyses, sensitivity analyses may examine whether conclusions change under alternative assumptions.

Potential sensitivity analyses include:

  • Alternative covariance structures
  • Alternative missing-data assumptions
  • Alternative baseline definitions where scientifically justified
  • Alternative time representations
  • Alternative handling of post-treatment events
  • Pattern-mixture or reference-based approaches

Post-Baseline Intercurrent Events

Clinical-trial mixed models must be considered in the context of the estimand framework.

Examples of intercurrent events include:

  • Treatment discontinuation
  • Rescue medication
  • Switching treatment
  • Death
  • Protocol-defined treatment failure

The model should not be selected independently of the estimand.

Key principle: The statistical model is a tool for estimating a prespecified clinical question. It should not silently redefine what treatment effect is being estimated.

A Practical End-to-End Analysis Script

library(lme4)
library(emmeans)
library(ggplot2)

#--------------------------------------------------
# 1. Prepare analysis variables
#--------------------------------------------------

dat$USUBJID <- factor(dat$USUBJID)

dat$treatment <- factor(
  dat$treatment,
  levels = c("Placebo", "Active")
)

dat$visit <- factor(
  dat$visit,
  levels = c(
    "Baseline",
    "Week 4",
    "Week 8",
    "Week 12",
    "Week 24"
  )
)

#--------------------------------------------------
# 2. Fit the longitudinal mixed model
#--------------------------------------------------

fit <- lmer(
  outcome ~ baseline +
    treatment * visit +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

#--------------------------------------------------
# 3. Review model
#--------------------------------------------------

summary(fit)

#--------------------------------------------------
# 4. Check singularity
#--------------------------------------------------

isSingular(fit)

#--------------------------------------------------
# 5. Extract fixed effects
#--------------------------------------------------

fixef(fit)

#--------------------------------------------------
# 6. Estimated marginal means
#--------------------------------------------------

emm <- emmeans(
  fit,
  ~ treatment | visit
)

#--------------------------------------------------
# 7. Treatment comparisons
#--------------------------------------------------

contrasts <- pairs(
  emm,
  adjust = "none"
)

#--------------------------------------------------
# 8. Convert to reporting datasets
#--------------------------------------------------

emm_results <- as.data.frame(emm)

contrast_results <- as.data.frame(
  contrasts
)

#--------------------------------------------------
# 9. Diagnostics
#--------------------------------------------------

plot(
  fitted(fit),
  resid(fit)
)

qqnorm(
  resid(fit)
)

qqline(
  resid(fit)
)

#--------------------------------------------------
# 10. Record environment
#--------------------------------------------------

sessionInfo()

How to Choose Between lme4 and nlme

Question Preferred starting point
Do I need random intercepts? Either
Do I need random slopes? Either
Do I need crossed random effects? lme4 is often attractive
Do I need AR(1) residual correlation? nlme
Do I need heterogeneous residual variance? nlme
Do I need continuous-time correlation? nlme
Do I need a generalized mixed model? lme4
Do I need nonlinear mixed-effects modeling? nlme

Important Clinical-Trial Distinction

There is no universal rule that says:

$$ \text{lme4 is better than nlme} $$

or:

$$ \text{nlme is better than lme4} $$

They provide different modeling capabilities.

The correct package depends on the model required by the scientific question and analysis plan.

Checklist for Clinical-Trial Mixed Models

1
Define the estimand.
2
Define the analysis population.
3
Define the endpoint and baseline.
4
Determine whether time is categorical or continuous.
5
Specify treatment and treatment-by-time effects.
6
Determine the random-effects structure.
7
Determine the residual covariance structure.
8
Determine the estimation method.
9
Evaluate missing-data assumptions.
10
Check convergence and diagnostics.
11
Generate estimands and contrasts.
12
Validate the final outputs independently.

Quick Reference: lme4

library(lme4)

# Random intercept
lmer(
  y ~ x + (1 | subject),
  data = dat
)

# Random intercept + slope
lmer(
  y ~ x + (1 + x | subject),
  data = dat
)

# Uncorrelated intercept and slope
lmer(
  y ~ x + (1 + x || subject),
  data = dat
)

# Generalized mixed model
glmer(
  y ~ treatment + (1 | subject),
  data = dat,
  family = binomial
)

Quick Reference: nlme

library(nlme)

# Random intercept
lme(
  fixed = y ~ x,
  random = ~ 1 | subject,
  data = dat
)

# Random intercept + slope
lme(
  fixed = y ~ x,
  random = ~ x | subject,
  data = dat
)

# AR(1)
lme(
  fixed = y ~ x,
  random = ~ 1 | subject,
  correlation =
    corAR1(
      form = ~ time | subject
    ),
  data = dat
)

# Heterogeneous residual variance
lme(
  fixed = y ~ x,
  random = ~ 1 | subject,
  weights =
    varIdent(
      form = ~ 1 | visit
    ),
  data = dat
)

Final Conceptual Framework

The easiest way to remember mixed models is to separate three questions.

Question Model component
What population-level effects are we estimating? Fixed effects
How do patients differ from one another? Random effects
How are repeated residual measurements correlated? Residual covariance structure

This distinction is the key to understanding why lme4 and nlme can produce different-looking model specifications even when they are addressing the same general longitudinal problem.

The Most Important Clinical-Trial Example

Suppose a randomized trial has a continuous endpoint measured repeatedly after baseline.

A strong starting conceptual model is:

$$ Y_{ij} = \text{Treatment} + \text{Visit} + \text{Treatment}\times\text{Visit} + \text{Baseline} + \text{Subject Variation} + \text{Residual Dependence} $$

The programming implementation then depends on how subject variation and residual dependence are intended to be represented.

With lme4, this might be:

lmer(
  outcome ~ baseline +
    treatment * visit +
    (1 | USUBJID),
  data = dat,
  REML = TRUE
)

With nlme, a richer covariance specification might be:

lme(
  fixed =
    outcome ~ baseline +
      treatment * visit,

  random =
    ~ 1 | USUBJID,

  correlation =
    corAR1(
      form = ~ week | USUBJID
    ),

  data = dat,

  method = "REML"
)

The second model explicitly describes residual correlation in addition to subject-level heterogeneity.

What to Remember

1. Repeated observations are correlated.

Mixed models account for that correlation rather than treating every measurement as an independent observation.

2. Fixed effects describe population-level relationships.

Treatment, visit, baseline, and treatment-by-visit interaction are common fixed effects in clinical-trial longitudinal analyses.

3. Random effects describe subject-level heterogeneity.

Random intercepts allow patients to have different baseline levels. Random slopes allow patients to have different rates of change.

4. Residual covariance is a separate concept.

An AR(1), compound-symmetry, heterogeneous, or other covariance structure describes how residual observations remain correlated after accounting for the fixed and random effects.

5. lme4 and nlme have different strengths.

lme4 is particularly strong for random-effects modeling and generalized mixed models. nlme provides especially rich residual covariance and variance structures.

6. REML and ML serve different purposes.

ML is generally used when comparing different fixed-effects structures. REML is commonly used for final estimation of variance components once the fixed-effects structure is established.

7. Treatment-by-visit interactions are often central.

They allow the estimated treatment difference to vary across scheduled visits.

8. Model diagnostics matter.

Convergence warnings, singular fits, residual patterns, covariance estimates, and random-effect behavior should be investigated rather than ignored.

9. Mixed models do not automatically solve missing-data problems.

Likelihood-based longitudinal models can provide valid inference under appropriate assumptions, commonly involving MAR, but the missingness process and estimand must still be considered.

10. The model should answer the clinical question.

Do not choose the statistical model first and then retrofit the scientific question. Start with the estimand, trial design, endpoint, and analysis plan.

Summary

Mixed-effects models are among the most important statistical tools for analyzing longitudinal clinical-trial data.

They provide a principled way to combine population-level treatment effects with subject-level heterogeneity while accounting for the correlation created by repeated measurements.

In R, lme4 provides an intuitive and powerful framework for random-effects modeling, including random intercepts, random slopes, nested effects, crossed effects, and generalized mixed models.

nlme adds particularly important capabilities for explicit residual covariance and variance modeling, including AR(1), continuous-time correlation, compound symmetry, and heterogeneous residual variance.

For clinical-trial statisticians, the most important conceptual distinction is not simply "which package should I use?"

The more important questions are:

  • What is the estimand?
  • What are the fixed effects?
  • How much subject-level heterogeneity should be modeled?
  • What residual covariance structure is plausible?
  • How should missing observations be handled?
  • What treatment contrast is clinically relevant?
  • How will the result be validated and reported?

Once those questions are answered, lme4 and nlme become implementation tools for expressing the statistical model in R.

Bottom line: For serious longitudinal clinical-trial work, understanding mixed models means understanding more than the syntax of lmer() or lme(). The analyst must understand fixed effects, random effects, residual covariance, estimation, missing-data assumptions, estimands, and model diagnostics. lme4 is an excellent framework for flexible random-effects models, while nlme is particularly valuable when the residual covariance structure itself is an important part of the analysis.

References

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.

Pinheiro, J.C., & Bates, D.M. (2000). Mixed-Effects Models in S and S-PLUS. Springer.

Pinheiro, J., Bates, D., DebRoy, S., Sarkar, D., & R Core Team. nlme: Linear and Nonlinear Mixed Effects Models.

Verbeke, G., & Molenberghs, G. (2000). Linear Mixed Models for Longitudinal Data. Springer.

Fitzmaurice, G.M., Laird, N.M., & Ware, J.H. (2011). Applied Longitudinal Analysis. Wiley.

McCulloch, C.E., Searle, S.R., & Neuhaus, J.M. (2008). Generalized, Linear, and Mixed Models. Wiley.