From Linear Regression to Mixed-Effects Models: Understanding Fixed and Random Effects from First Principles

On this page
อ่านฉบับภาษาไทย (Thai version)
Abstract
Mixed-effects models become easier to understand when each component is introduced before the next. This article begins with ordinary linear regression and one blood-pressure example, then distinguishes shared intercepts and slopes from personal departures. It explains why repeated observations are correlated, how random-effect variation differs from residual variation, and why a treatment interaction is different from a random slope. Practical sections introduce covariance choices, estimation, diagnostics, and alternatives without treating any method as universally best. The final part transfers the same reasoning to four aortic sections in repaired tetralogy of Fallot. Categorical contrasts, covariance identifiability, and equal standard errors receive explicit explanations. A complete synthetic wall-shear-stress example connects the research question, model, calculations, sensitivity analysis, and reporting. All numerical clinical examples are illustrative rather than empirical study findings. The aim is a self-contained path from the first regression equation to a defensible interpretation of repeated-measures analysis.
Part I. Understand Regression Before Mixed Models
Chapter 1. Linear Regression: What Are Intercept and Slope?
Start with systolic blood pressure, abbreviated BP, measured in millimetres of mercury (mmHg). We want to describe how average BP changes with months since baseline. A straight line is a useful first description, provided its limitations are checked.
Let \(Y\) denote an observed BP and \(X\) denote months since baseline. Let \(\beta_0\) be the mean BP at month zero, \(\beta_1\) the mean change per month, and \(\varepsilon\) the observation's departure from its mean. Then the regression equation is:
\[Y=\beta_0+\beta_1X+\varepsilon.\]
For illustration, set the intercept to 130 mmHg and the slope to -3.5 mmHg per month. The predicted mean is 130 at baseline, 126.5 after one month, and 119.5 after three months. Multiplying the slope by time converts a rate into an amount of change.
Suppose a month-one reading is 129 mmHg. Its residual relative to this illustrative line is 2.5 mmHg. A residual is an observed discrepancy, whereas the error term represents the unobserved departure in the statistical model.
What the line does and does not say
The intercept is meaningful only at the chosen zero of the predictor. If time zero represents baseline, it describes baseline mean BP. If zero lies far outside the observed range, the intercept may be mostly an extrapolation.
The slope describes an association in this model. A decreasing BP line alone does not establish that follow-up time causes the decrease. Treatment changes, selection, and other factors can also contribute.
A mean line is not a promise that every observed BP follows that line exactly. Measurement noise and unmeasured influences create departures. Before adding complexity, distinguish the location of the mean from the scatter around it.
The same arithmetic also prevents a common language error: a negative slope means BP falls as time increases, not that BP itself must become negative. We use the line within a clinically sensible observation window. We do not extend it indefinitely.
- Key idea: An intercept locates the mean line; a slope describes its change; a residual is the observed departure from that line.
Chapter 2. Fixed Intercept and Fixed Slope: One Regression Equation for Everyone
A fixed intercept and fixed slope provide shared coefficients for the population mean. In our simple example, everyone contributes to estimating the same baseline level and the same monthly change. Different measured values remain possible because observations depart from the mean.
The word fixed does not mean the coefficient is known, exact, or free of sampling uncertainty. A fitted slope has a standard error and a confidence interval. Those describe uncertainty in estimating the shared relationship from a finite sample.
Continuous and categorical predictors
Months are continuous in meaning even if visits occur at whole months. A one-unit change means one month, so the coefficient has units of mmHg per month. Rescaling time to years changes the numerical coefficient and its unit, without changing the underlying fitted line.
A categorical predictor needs a different interpretation. Let \(T\) equal zero for the reference BP treatment group and one for the comparison group, and let \(\alpha_0\) and \(\alpha_T\) denote its intercept and group coefficient. A model without time would be:
The notation \(E(Y\mid T)\) means the expected BP for the specified group.
\[E(Y\mid T)=\alpha_0+\alpha_TT.\]
The notation \(E(Y\mid T)\) means the expected BP for the specified group. Here \(\alpha_T\) is a difference between group means, not a rate of physical movement between two groups. Coding a category numerically does not make it a continuous quantity.
A terminology panel
In regression, fixed effects are shared mean coefficients. In panel-data discussions, a fixed-effects estimator often introduces person-specific intercepts as unrestricted nuisance parameters or removes them by within-person transformation. That approach can allow association between those intercepts and the predictors.
A conventional random-effects regression instead needs assumptions about how unobserved person effects relate to included predictors. If those assumptions are unsuitable, person-level means of changing predictors or a different analysis may be needed. The phrase fixed effects therefore requires its setting.
Meta-analysis uses fixed-effect or common-effect language for another question: whether study estimates target a common underlying effect. That is a different modelling context. None of these meanings says that a fixed coefficient must be identical to every individual's biological response.
- Key idea: Fixed coefficients describe shared population relationships and still have estimation uncertainty.
Chapter 3. Repeated Measurements: Why Four Observations Are Not Four Independent Patients
Now measure each person's BP at months zero, one, two, and three. The data contain four rows per person. Four rows describe four measurements, but they do not create four independent people.
The same person carries persistent influences across visits. Their physiology, baseline health, and measurement setting may make their BP values resemble one another. A high reading at one visit can therefore be informative about another reading from the same person.
Describe the rows before choosing a model
Use a person identifier, visit time, BP outcome, and any prespecified covariates. Each row should represent one measurement at a clearly defined time. Check whether duplicate person-time rows are intended replicates or recording errors.
Continuous describes the outcome's scale. Repeated describes how observations are organized. These are separate properties: repeated measurements can also record binary events, counts, or ordered categories.
A Gaussian model uses a normal distribution for errors remaining after the modelled mean and any personal departures are specified. It can suit continuous BP measurements when that approximation is reasonable. Other outcome types need other distributions and often other links between predictors and the mean. Repetition alone does not determine the outcome model.
Why independence matters
An analysis that treats every BP row as an unrelated person may misstate precision. The direction and size of the error depend on the design and the comparison being estimated. Such a comparison, for example a difference between treatment means, is called a contrast. There is no universal rule that every repeated-data standard error must increase by the same factor.
A within-person change uses the pairing between visits. A between-treatment comparison also depends on how many independent people were assigned to each treatment. The same data can therefore contain different amounts of information for different questions.
Keeping one average per person can sometimes answer a narrow average-BP question. It discards the shape of follow-up and may fail to answer a time-trend question. Our goal is to retain the repeated measurements while representing what they share.
- Key idea: Repeated observations share a person, so their information cannot generally be counted as independent patients.
Part II. Building a Mixed-Effects Model
Chapter 4. What Makes a Model Mixed? Fixed Effects + Random Effects
A mixed-effects model combines shared mean coefficients with person-specific random departures. Mixed refers to those two types of effect. It does not mean a mixture of continuous outcomes and repeated measurements.
Let \(i\) identify a person and \(j\) identify a measurement. Let \(Y_{ij}\) be BP and \(t_{ij}\) be months since baseline. Keep \(\beta_0\) and \(\beta_1\) for the shared intercept and time slope, introduce \(b_{0i}\) for person \(i\)'s intercept departure, and use \(\varepsilon_{ij}\) for the remaining error.
\[Y_{ij}=\beta_0+\beta_1t_{ij}+b_{0i}+\varepsilon_{ij}.\]
The fixed part describes the population mean line. The random intercept describes a person who stays above or below that mean line across visits. The remaining error describes departures not captured by either component.
What random means here
We model the person departures as draws from a population distribution, commonly a normal distribution with mean zero. Zero mean defines the fixed intercept as the population centre. It does not require every sampled person's departure to cancel exactly.
The random-effects distribution allows information to be shared across people. A person with few noisy observations has a less certain individual departure than someone with many informative observations. Predictions therefore tend to move toward the shared relationship, an effect often called partial pooling or shrinkage.
This is different from fitting an unrelated line for every person. Separate lines do not use a shared distribution to stabilize the individual estimates. The mixed model estimates common coefficients and variation across people together.
For a standard Gaussian mixed model, random effects and remaining errors are usually assumed independent of one another, with appropriate assumptions concerning the predictors. These assumptions support interpretation and estimation. They deserve scrutiny rather than being treated as automatic truths.
The model can describe clustered data beyond longitudinal follow-up. Our BP example stays with repeated visits so that only one new idea is introduced at a time. The mathematical meaning of mixed remains the same.
- Key idea: Mixed means fixed effects and random effects occur in the same model.
Chapter 5. Fixed Intercept vs Random Intercept
The fixed intercept locates the population mean at baseline. A random intercept moves one person's whole mean line up or down. The person's baseline is the sum of the shared intercept and their departure.
With the shared baseline of 130 mmHg, a departure of +10 gives a baseline of 140. A departure of -10 gives a baseline of 120. If both people share the same slope, their mean trajectories remain parallel.
One offset, repeated across rows
The important feature is that each person has one intercept departure reused at every visit. It is not a new independent draw for each BP reading. Reusing the same offset creates a shared source of variation across that person's observations.
Suppose one person has a positive departure. Even before considering measurement-specific errors, all their visits tend to sit above the population line. Another person with a negative departure tends to sit below it.
Variance measures spread as the mean squared departure from a mean. Its square root is the standard deviation (SD). Correlation ranges from -1 to +1: positive values indicate a tendency to vary together. Zero indicates no linear association, while negative values indicate opposite movement.
Let \(\tau_0^2\) denote the population variance of the intercept departures and \(\sigma^2\) the variance of independent remaining errors. Assume these components are independent. Two distinct visits from the same person then share covariance \(\tau_0^2\).
Covariance describes how two quantities vary together. It has squared outcome units; correlation rescales it to a unitless measure. A positive shared intercept variance induces positive covariance between visits under these assumptions.
Moving the time origin
Centred time means subtracting a chosen reference month from each time value. If we centre at month two, the intercept now describes mean BP at month two. The model's predicted BP values remain unchanged when the coefficients are transformed consistently.
In a random-intercept-only model, centring changes the meaning of the fixed intercept while the shared person offset stays the same. Once person-specific slopes are introduced, changing the origin also changes which person-specific level is called the random intercept. This matters when interpreting intercept-slope association.
A random intercept is not a standard error for a person's baseline. Its population spread describes heterogeneity. The uncertainty in estimating that spread, or in predicting one person's departure, is another quantity.
- Key idea: A random intercept is one person-specific offset shared across that person's measurements.
Chapter 6. Fixed Slope vs Random Slope
Parallel BP trajectories may be too restrictive. Some people improve rapidly, while others change slowly. We need a way to retain an average monthly change while allowing individual changes to differ.
Let \(b_{1i}\) denote person \(i\)'s departure from the fixed time slope \(\beta_1\). Their total slope is \(\beta_1+b_{1i}\). Adding the random slope gives:
\[Y_{ij}=(\beta_0+b_{0i})+(\beta_1+b_{1i})t_{ij}+\varepsilon_{ij}.\]
The fixed slope remains in the equation. It describes the population's mean change, while the random slope describes how one person differs from that mean. Replacing the fixed slope with a mean-zero random departure would impose a zero population slope unless another term supplied it.
Read the units
An intercept departure has units of mmHg. A time-slope departure has units of mmHg per month. Multiplying the latter by months produces an outcome-scale departure.
At baseline, time equals zero, so a slope departure contributes nothing to BP at that instant. Its effect grows in magnitude as we move from the reference time. This explains why people can become more or less separated during follow-up.
People who start high might decline faster, decline slower, or show no systematic relation between starting level and change. An intercept-slope covariance can represent these possibilities. Its interpretation depends on the time origin.
What the data must support
Learning about slope variation requires within-person variation in time and enough independent people. Two measurements can define a noisy personal change, but disentangling several variance components can still be difficult. More visits help describe trajectories; more people help characterize the population distribution.
A random time slope also assumes a particular shape for individual departures: linear in the chosen time scale. It cannot automatically represent every person's curved BP course. Curvature may require a different mean function, additional random terms, or a more suitable scientific model.
Individual trajectory plots are therefore useful before fitting. They show whether straight lines are plausible and whether people appear to differ mainly in level, change, or both. They guide a model question rather than prove a variance structure.
- Key idea: A random slope adds a person-specific deviation to the fixed slope.
Chapter 7. Four Combinations of Intercepts and Slopes
| Model | Intercept | Slope |
|---|---|---|
| A. Ordinary Linear Regression | Fixed | Fixed |
| B. Random-intercept LMM | Fixed + Random | Fixed |
| C. Random-slope LMM | Fixed | Fixed + Random |
| D. Random-intercept-and-slope LMM | Fixed + Random | Fixed + Random |
Use \(Y_{ij}\) for person \(i\)'s BP at visit \(j\), \(t_{ij}\) for months, \(\beta_0,\beta_1\) for shared coefficients, \(b_{0i},b_{1i}\) for personal departures, and \(\varepsilon_{ij}\) for remaining error. The fully annotated equation is:
\[Y_{ij}=\underbrace{\beta_0}_{\text{fixed intercept}}+\underbrace{b_{0i}}_{\text{random intercept}}+\left(\underbrace{\beta_1}_{\text{fixed slope}}+\underbrace{b_{1i}}_{\text{random slope}}\right)t_{ij}+\underbrace{\varepsilon_{ij}}_{\text{residual}}.\]
Reconstruct two synthetic trajectories
| Month | Patient A BP | Patient B BP |
|---|---|---|
| 0 | 140 | 120 |
| 1 | 135 | 118 |
| 2 | 130 | 116 |
| 3 | 125 | 114 |
Choose a shared intercept of 130 and a shared slope of -3.5. Give A an intercept departure of +10 and a slope departure of -1.5. Give B an intercept departure of -10 and a slope departure of +1.5.
A starts at 140 and changes by -5 per month. B starts at 120 and changes by -2 per month. At month three, A reaches 125 and B reaches 114, exactly reconstructing the table.
This is a synthetic decomposition of two straight lines. It is not an estimate of a population random-effect variance, a fitted software result, or evidence that two people are enough to fit a reliable mixed model. We have deliberately set the residuals to zero to isolate the arithmetic.
What each restriction allows
Model A supplies one mean line, so it cannot represent persistent personal levels or slopes in its mean structure. Model B allows different levels but keeps trajectories parallel. It can express A starting above B, but cannot reproduce their different rates exactly without residual departures.
Model C permits different rates but requires a common personal mean level at time zero because it has no random intercept. This is a substantive restriction tied to the chosen origin. It cannot reproduce the two distinct baseline levels in this table as exact personal mean trajectories.
Model D allows both personal levels and personal rates, so the illustrative decomposition fits its structure. That flexibility is useful only when supported by the study design and enough information. More flexible does not automatically mean more credible.
The rows compare model components, not four completed fits. Actual fitted fixed coefficients and residuals would depend on the data and estimation procedure. We should never present this hand-built illustration as a model comparison with estimated likelihoods or significance tests.
- Key idea: The fixed slope remains present when a random slope is added.
Chapter 8. Random-Effect Variance vs Residual Variance
Random-effect variance describes how personal mean trajectories differ. Residual variance describes remaining variation around those trajectories under the chosen model. A large amount of one does not imply a large amount of the other.
Keep \(\tau_0^2\) for random-intercept variance and \(\sigma^2\) for independent residual variance. In a random-intercept model, the marginal variance of a BP observation is their sum. Marginal means that we average over the population distribution of personal offsets.
\[\operatorname{Var}(Y_{ij})=\tau_0^2+\sigma^2.\]
For two distinct visits with the same residual variance and no residual correlation, define the intraclass correlation coefficient, abbreviated ICC, as their common within-person correlation. It is:
\[\operatorname{ICC}=\frac{\tau_0^2}{\tau_0^2+\sigma^2}.\]
For a synthetic variance of 64 between people and 36 remaining within-person variance, the total is 100 and the ICC is 0.64. The corresponding standard deviations are 8 and 6 mmHg. These numbers illustrate the definition and are not estimates from the two trajectories.
Conditional and marginal views
Conditional means holding a person's random effects fixed. Under independent residual errors, their remaining measurement departures are then independent. Marginally, those same measurements are correlated because they share an unknown personal offset across the population.
There is no contradiction. Knowing a person's persistent level removes one source of dependence. Averaging over uncertainty in that shared level restores it.
With a random slope
Let \(\tau_1^2\) be slope-departure variance and \(\tau_{01}\) the intercept-slope covariance. At time \(t\), the marginal BP variance becomes:
\[\operatorname{Var}\{Y_i(t)\}=\tau_0^2+2t\tau_{01}+t^2\tau_1^2+\sigma^2.\]
Between-person spread can now change with time. Correlations between visits can depend on both visit times, so one constant ICC no longer summarizes every pair. Reporting a single number without defining the pair or reference time can conceal this structure.
A random-effect standard deviation measures heterogeneity in outcome or slope units. A standard error measures uncertainty in an estimated coefficient or statistic. Confusing them makes a model appear to answer a different question from the one it actually answers.
- Key idea: Between-person variation and remaining measurement variation answer different questions.
Documentation supporting the model and software details: lme4 model formulation.
Part III. From Model Equations to Real Repeated-Measures Analysis
Chapter 9. Fixed Interaction Is Not a Random Slope
Suppose the BP study contains two treatment groups. One question asks whether their average BP changes differ. Another asks whether people within the same treatment group still have different changes.
Let \(T_i\) equal one for the comparison treatment and zero for reference treatment. Let \(\beta_T\) describe their baseline mean difference and \(\beta_{tT}\) describe their difference in monthly change. A model with both an interaction and individual trajectory variation is:
Here \(\beta_t\) names the fixed time slope in the reference treatment group, previously written as \(\beta_1\).
\[Y_{ij}=\beta_0+\beta_tt_{ij}+\beta_TT_i+\beta_{tT}t_{ij}T_i+b_{0i}+b_{1i}t_{ij}+\varepsilon_{ij}.\]
The reference group's mean slope is \(\beta_t\). The comparison group's mean slope is \(\beta_t+\beta_{tT}\). Within either group, person \(i\)'s slope also includes the departure \(b_{1i}\).
Systematic difference versus remaining heterogeneity
A fixed treatment-by-time interaction represents a systematic difference between group mean trends. A random time slope represents person-to-person variation left after the fixed part has been specified. Both can belong in the same model because they answer different questions.
For illustration, the reference group might average -3.5 mmHg per month while the comparison group averages -4.5. The interaction is then -1 mmHg per month. Individuals may still depart above or below their own group's average slope.
Adding random slopes cannot substitute for a scientifically required treatment interaction. Without the interaction, the model imposes the same mean slope across groups, even if it allows individuals to vary. Conversely, an interaction cannot guarantee that all people within a group have parallel trajectories.
Separate between-person and within-person information
Time varies within a person, while assigned treatment generally does not. A person's treatment indicator therefore cannot directly reveal how that same person's BP would have evolved under both assignments. Causal interpretation also depends on allocation, follow-up, and the target question.
Other changing predictors can carry both within-person and between-person information. For example, a higher BP-related covariate within one person need not have the same association as a higher average level across people. Including person means alongside within-person deviations can make that distinction explicit when relevant.
Interpret interactions at stated reference values. A baseline treatment coefficient applies at the chosen time origin; after centring time at month two, it becomes the mean treatment difference at month two. Centring changes the question attached to coefficients without changing fitted values when done consistently.
- Key idea: A fixed interaction describes systematic group differences; a random slope describes remaining person-to-person differences.
Chapter 10. How Mixed Models Handle Correlation and Variance
A random effect is one source of dependence. Remaining errors may also be correlated, or have different variances at different visits. These choices describe different parts of the same covariance model.
Let \(\mathbf Y_i\) collect a person's BP measurements. Let \(X_i\) contain their fixed-effect predictor rows and \(\boldsymbol\beta\) the shared coefficients. Let \(Z_i\) contain the predictor rows attached to the person's random-effect vector \(\mathbf b_i\), and let \(\boldsymbol\varepsilon_i\) collect the remaining errors.
Write \(G\) for the covariance matrix of the random effects and \(R_i\) for the residual covariance matrix. A covariance matrix records variances on its diagonal and covariances between distinct quantities off the diagonal. Assuming independence between random effects and residual errors gives:
The symbol \(\top\) means transpose: turn a row into a column, or interchange the rows and columns of a matrix. For an intercept and time slope, each row of \(Z_i\) is \((1,t_{ij})\). Matrix multiplication adds the shared random contributions to each pair of measurements.
\[\mathbf Y_i=X_i\boldsymbol\beta+Z_i\mathbf b_i+\boldsymbol\varepsilon_i.\]
\[\operatorname{Var}(\mathbf Y_i)=Z_iGZ_i^\top+R_i.\]
Compare the covariance choices
| Choice | What it represents | Important restriction |
|---|---|---|
| Random intercept + equal independent errors | Shared personal BP offset | Equal marginal variances and equal pairwise correlations |
| Random intercept + unequal independent errors | Shared offset with visit-dependent remaining spread | Equal off-diagonal covariances, generally unequal correlations |
| Random intercept and time slope | Shared level and change departures | Covariance follows a time-based pattern |
| AR(1) residuals | Residual association decreases with visit lag | Discrete-time form requires a meaningful lag scale |
| Unstructured marginal covariance | A separate variance and covariance for every visit pair | Requires enough information and does not identify a unique biological decomposition |
The equal-variance random-intercept model has compound symmetry: the same marginal variance at each visit and the same covariance between every distinct pair. With unequal residual variances, its common covariance survives but its correlations generally differ. Correlation divides covariance by the two marginal standard deviations.
For AR(1), adjacent equally spaced visits have one residual correlation parameter; correlations at longer lags are powers of that parameter. Unequal actual intervals need a suitable time representation or continuous-time correlation model. A visit number is not automatically a substitute for elapsed time.
Alternatives and redundancy
A marginal model can estimate the population mean and model its covariance directly without personal random-effect predictions. Generalized estimating equations, or GEE, target mean relationships using a working correlation and appropriate uncertainty estimates. Cluster-robust standard errors can protect mean-coefficient inference against some covariance misspecification when there are enough independent clusters and suitable corrections.
None is universally the best method. Choose according to the target quantity, such as a treatment difference at month three. Also consider cluster count, missingness assumptions, design, and whether individual predictions are needed. Robust uncertainty does not repair a wrong mean model or informative missingness.
Do not combine unrestricted covariance pieces indiscriminately. A random intercept plus an unrestricted residual covariance can assign the same covariance to either component, preventing separate identification. A model must distinguish the information available in the total covariance from the decomposition we wish to interpret.
- Key idea: Model covariance to answer the scientific question; do not rank methods by a universal best-to-worst ladder.
Documentation supporting the model and software details: nlme: covariance and unequal residual variances; nlme: AR(1) time covariate.
Chapter 11. Fit, Interpret, and Check a Mixed Model
Start with the intended contrast, the person identifier, actual visit times, and a defensible mean function. Plot the BP observations and missing visits before fitting. Decide which coefficients describe the scientific question and which covariance components the design can support.
Illustrative commands, not executed analysis
The following code assumes a prepared long-format dataset called bp with id, month, treatment, and BP. The treatment variable must have an explicit reference group. These commands show syntax; they have not been run on study data and generate no reported empirical results here.
# R: illustrative only
bp$id <- factor(bp$id)
bp$treatment <- factor(bp$treatment)
m_intercept <- lme4::lmer(
BP ~ month * treatment + (1 | id),
data = bp, REML = TRUE
)
m_slope <- lme4::lmer(
BP ~ month * treatment + (1 + month | id),
data = bp, REML = TRUE
)
summary(m_slope)
lme4::isSingular(m_slope)
plot(m_slope)* Stata: illustrative only; treatment is numeric categorical
mixed BP c.month##i.treatment || id: month, ///
covariance(unstructured) reml
estat recovariance
predict double bp_resid, residualsThe Stata covariance option concerns the random intercept and random month slope in this command. It does not request an unrestricted residual covariance across visits. Matching software requires matching the mean model, random terms, residual assumptions, estimation method, data, and reference coding.
Read output in layers
Fixed coefficients describe baseline, change, group differences, and interactions at stated reference values. Their standard errors and confidence intervals describe estimation uncertainty. A random-effect SD describes population heterogeneity, while residual SD describes the remaining outcome-scale spread.
A random-effect correlation describes association between personal intercept and slope departures. Its meaning changes with time centring. A correlation close to either boundary may signal insufficient information for the chosen covariance structure.
Estimation and checks
Maximum likelihood (ML) selects parameters that make the observed data most compatible with the assumed probability model. Likelihood measures that compatibility as the parameters change. Restricted maximum likelihood (REML) estimates covariance after accounting for the fixed effects that must also be estimated. Likelihood comparisons of different fixed-effect structures generally use ML on identical observations; REML comparisons require the same fixed-effect structure.
A converged optimizer has met its numerical stopping criteria. It does not certify that the mean, covariance, or missingness assumptions are adequate. A singular fit means the estimated random-effect covariance has reduced rank, for example a zero variance or a perfectly dependent combination of random effects.
Check residuals against fitted values, time, treatment, and person. Examine departures from linearity, changing spread, unusual individuals, and remaining serial patterns. Normality plots are diagnostic aids, not a replacement for assessing whether the scientific contrast is stable under plausible alternatives.
For variance components on a boundary, an ordinary likelihood-ratio test reference distribution may be inappropriate. Use a justified procedure rather than reading every model comparison as a routine chi-squared test. Report sensitivity analyses that materially change uncertainty or interpretation.
- Key idea: Code is illustrative; convergence, singularity, assumptions, and uncertainty need separate assessment.
Documentation supporting the model and software details: lme4: singular fits; lme4: estimation and model specification; Stata: mixed; Stata: mixed postestimation.
Part IV. Advanced Mixed Models: From Time to Aortic Sections
Chapter 12. Repeated Measures Without Time: Anatomical Sections
We now move explicitly from BP follow-up to repaired tetralogy of Fallot, abbreviated TOF. TOF is a congenital heart condition; repaired TOF describes people who have undergone surgical repair. Four-dimensional flow magnetic resonance imaging, or 4D flow MRI, can characterize blood flow across three spatial dimensions over the cardiac cycle.
Wall shear stress, abbreviated WSS, describes tangential stress associated with flowing blood at the vessel wall. An imaging-derived WSS measure needs a stated definition, unit, processing method, and location. We use illustrative WSS in pascals (Pa) below, without claiming any observed clinical finding.
Four locations in one person
The four teaching labels are AAO, STJ, Before branching, and DAO. AAO denotes the ascending aorta, STJ the sinotubular junction, and DAO the descending aorta. Before branching is the study's chosen location label and requires an explicit anatomical definition in a real imaging protocol.
These measurements share a person even when obtained in the same examination. Their dependence does not require follow-up time. Repeated measurements can describe locations, conditions, or visits.
Section is categorical here. Numbering the four locations one through four and fitting one numerical slope would impose equally spaced linear mean changes across those codes. Anatomical order alone does not justify that restriction.
Reference coding
Take AAO as reference. Let \(\beta_0\) be its mean, and define indicator variables \(S_{\mathrm{STJ}}\), \(S_{\mathrm{Before}}\), \(S_{\mathrm{DAO}}\), each equal to one at its named section and zero otherwise. Let the corresponding coefficients describe each section's mean difference from AAO.
\[E(Y\mid\text{section})=\beta_0+\beta_{\mathrm{STJ}}S_{\mathrm{STJ}}+\beta_{\mathrm{Before}}S_{\mathrm{Before}}+\beta_{\mathrm{DAO}}S_{\mathrm{DAO}}.\]
There are four means but only three differences from a chosen reference. A reference change alters the coefficient labels and numerical contrasts, while the four fitted means remain the same. The intercept is now a reference-section mean, not a baseline-time mean.
A random intercept adds one personal offset to all four section means. It represents a person whose WSS tends to be higher or lower throughout the profile after fixed predictors are considered. It does not by itself permit every person's section contrasts to differ.
- Key idea: Repeated measurements need a shared observational unit, not necessarily a time axis.
Chapter 13. Random Slopes for Categorical Predictors: What Can We Estimate?
1. Start with a binary section contrast
Temporarily retain only AAO and DAO. Let \(D\) be zero for AAO and one for DAO. Let \(\beta_D\) be the population mean DAO-minus-AAO difference; it is a fixed contrast.
With a random intercept \(b_{0i}\), a person's AAO mean is \(\beta_0+b_{0i}\), and their DAO mean is \(\beta_0+b_{0i}+\beta_D\). The personal offset cancels when taking DAO minus AAO. Everyone therefore has the same modelled mean contrast.
2. Allow that contrast to vary
Introduce \(b_{Di}\), a person-specific departure from the DAO contrast. The model becomes:
\[Y_{ij}=\beta_0+b_{0i}+(\beta_D+b_{Di})D_{ij}+\varepsilon_{ij}.\]
For a synthetic illustration, set the AAO population mean to 1.0 Pa and the DAO contrast to -0.2 Pa. Give one person a +0.1 personal level offset and a +0.1 contrast departure: their two means are 1.1 and 1.0. Give another person a -0.1 level offset and a -0.1 contrast departure: their means are 0.9 and 0.6.
Their DAO-minus-AAO contrasts are -0.1 and -0.3 Pa. A categorical random slope means a varying contrast. It does not require physical time or a continuous predictor.
3. Expand to four levels
Four section levels require three independent contrasts. A full (1 + section | id) structure contains four random coefficients per person: an intercept and three contrast departures. With an unrestricted covariance among these coefficients, it allows a fully flexible random profile.
Allowing only one prespecified contrast departure is a smaller model. Such a restricted structure may be identifiable from the available covariance patterns. The failure of a saturated structure is not a prohibition against all categorical random slopes.
4. Explain what cannot be separated
For the two-section case, let \(H\) be the covariance of the person's latent AAO and DAO mean departures, with diagonal entries \(h_{11},h_{22}\) and covariance \(h_{12}\). With one reading per section and independent equal residual variance \(\sigma^2\), the observed covariance \(V\) is:
\[V=\begin{pmatrix}h_{11}+\sigma^2&h_{12}\\h_{12}&h_{22}+\sigma^2\end{pmatrix}.\]
Two variances and one covariance provide three observable covariance quantities. The latent covariance contributes three unknowns, and free residual variance adds a fourth. Counting suggests a problem; an explicit construction proves it.
Let \(\delta\) be a small positive variance amount below the residual variance, and let \(I\) be the identity matrix.
The identity matrix has ones on its diagonal and zeros elsewhere, so adding \(\delta I\) changes only the diagonal variances.
Replace \(H\) with \(H+\delta I\) and replace \(\sigma^2\) with \(\sigma^2-\delta\). Both covariance components remain valid for a suitable \(\delta\), while their sum \(V\) is unchanged.
For example, observed diagonal variances of 0.10 and covariance 0.04 can arise from latent diagonal variances of 0.06 plus residual variance 0.04. They can also arise from latent diagonal variances of 0.08 plus residual variance 0.02, retaining the same latent covariance 0.04. Both latent covariance matrices are mathematically valid and yield the same observed covariance.
A four-section covariance matrix has four diagonal variances and six distinct off-diagonal covariances: ten quantities. A full latent random profile already uses ten covariance parameters; adding free residual variance gives eleven. The same diagonal-shifting construction leaves the observed four-by-four covariance unchanged.
The intercept-plus-three-contrasts representation is an invertible recoding of the four latent section levels. It therefore has the same identifiability limitation. More people estimate the total covariance more precisely, but do not resolve this structural ambiguity by themselves.
Within-section replicate readings, justified covariance restrictions, or externally known measurement error can add identifying information or assumptions. An unrestricted marginal covariance estimates the total profile covariance without claiming its decomposition. A zero residual variance constraint changes the interpretation and needs explicit scientific justification.
Use the same \(H\), \(V\), and \(h_{ab}\) notation for all four section levels, where \(a,b\) index sections. The diagonal entries of \(H\) are latent variances; its off-diagonal entries are latent covariances. The observed covariance is:
\[V=\begin{pmatrix}h_{11}+\sigma^2&h_{12}&h_{13}&h_{14}\\h_{12}&h_{22}+\sigma^2&h_{23}&h_{24}\\h_{13}&h_{23}&h_{33}+\sigma^2&h_{34}\\h_{14}&h_{24}&h_{34}&h_{44}+\sigma^2\end{pmatrix}.\]
- Key idea: A categorical slope is a random contrast; identifying its variance depends on the whole covariance specification and design.
Chapter 14. Common Variance vs Section-Specific Variance
A random intercept can coexist with either one residual variance or section-specific residual variances. Those choices change remaining measurement spread. They do not introduce personal section-contrast slopes.
Let \(\tau^2\) be the random-intercept variance and let \(\sigma_s^2\) be the residual variance for section \(s\). Assume independent residual errors across sections. The marginal section variance is \(\tau^2+\sigma_s^2\), and every distinct pair shares covariance \(\tau^2\).
If the residual variances differ, the pairwise correlations generally differ because their denominators differ. A shared offset alone does not guarantee equal correlations. Equal correlations additionally require the relevant symmetry in marginal variances.
Why standard errors can be equal
With the same complete subjects at every section, equal group sizes across sections, a common covariance structure, and symmetric mean contrasts, standard errors can be exactly equal. The sections have different means but equivalent information under the model. Equal standard errors are therefore compatible with legitimate model calculations.
They do not establish equal biological variability. Observed SD, model residual SD, random-intercept SD, and SE of a mean or contrast are distinct quantities. Each answers a different question.
Let \(n\) be the number of complete independent subjects in one group. In the simple common-variance random-intercept model, the variance of a section sample mean is \((\tau^2+\sigma^2)/n\). There is no section label in that expression, explaining equal section mean SEs in this setting.
The variance of an estimated AAO-minus-DAO mean difference is instead \(2\sigma^2/n\) under the same assumptions. The shared person intercept cancels in the within-person difference. Equal SEs for the individual means do not imply that those means are independent.
Investigate assumptions, not appearance
Inspect residual spread by section and group, missingness, influential people, and fitted covariance adequacy. Consider a section-specific variance model when the observed pattern and scientific design justify it. Do not change a model merely to make a column of SEs look less repetitive.
In R's nlme framework, weights = varIdent(form = ~1 | section) permits different residual scales across section levels. It does not add random slopes. The model can keep the same random intercept and fixed group-by-section mean structure.
Changing covariance assumptions can change SEs, confidence intervals, and P values, even when point estimates remain similar. Missingness and adjustment can also change estimates. Any sensitivity that changes a clinically relevant inference belongs in the report, including results near an arbitrary significance threshold.
- Key idea: Equal precision can follow from a symmetric model and design; covariance adequacy still needs independent assessment.
Documentation supporting the model and software details: nlme: section-specific variance.
Chapter 15. Complete Worked Case: Repaired TOF 4D Flow MRI
This complete worked case uses synthetic population quantities, not patient data or empirical findings. It compares repaired-TOF and control groups at AAO, STJ, Before branching, and DAO. The outcome is an illustrative section-level WSS summary in Pa.
Question, design, and reproducible inputs
Ask whether the TOF-minus-control mean difference varies by section, and estimate that difference at each section. Assume 50 independent people per group, each measured once at all four sections. In a real study, eligibility, repair history, imaging protocol, WSS definition, and confounder selection require prespecification.
| Section | Control mean, Pa | TOF mean, Pa | TOF minus control, Pa | Residual variance, Pa squared |
|---|---|---|---|---|
| AAO | 1.00 | 1.20 | 0.20 | 0.04 |
| STJ | 0.90 | 1.00 | 0.10 | 0.09 |
| Before branching | 0.80 | 0.85 | 0.05 | 0.04 |
| DAO | 0.70 | 0.70 | 0.00 | 0.01 |
Set random-intercept variance to 0.04 Pa squared in both groups. For simulation, independently draw one normal person offset with mean zero and SD 0.2 Pa, then four independent mean-zero normal errors with SDs 0.2, 0.3, 0.2, and 0.1 Pa. Add the appropriate group-section mean, offset, and error for each observation.
This defines the synthetic data-generating distribution completely. A seeded random-number generator would create a particular reproducible sample, whose fitted estimates would vary around these inputs. We do not claim to have generated or fitted such a sample here.
Mean model and covariance
Let \(T_i\) indicate TOF, with controls as zero. Let \(\mu_s\) be the control mean at section \(s\), \(\Delta_s\) the TOF-minus-control difference, \(b_i\) the shared person offset, and \(\varepsilon_{is}\) the section error. The teaching model is:
\[Y_{is}=\mu_s+\Delta_sT_i+b_i+\varepsilon_{is}.\]
This is the cell-mean form of a fixed group-by-section interaction model. With AAO as reference, the group coefficient is 0.20 Pa and the other group-by-section interaction coefficients are -0.10, -0.15, and -0.20 Pa. These encode the same four group contrasts shown in the table.
The random intercept represents shared personal level variation. Residual variances represent different remaining spreads across sections. There is no full random-section profile, so the saturated random-profile-plus-free-error ambiguity is avoided.
Calculate precision from the stated assumptions
The marginal variances are 0.08, 0.13, 0.08, and 0.05 Pa squared. For two independent groups of 50, the variance of a section's difference between sample means is its marginal variance multiplied by \(1/50+1/50\). Treating these covariance inputs as known gives SEs of approximately 0.057, 0.072, 0.057, and 0.045 Pa.
For comparison, impose a common residual variance of 0.045, the arithmetic average of the four stated residual variances. Keeping the random-intercept variance at 0.04 gives a marginal variance of 0.085 everywhere. The corresponding illustrative SE is about 0.058 Pa at every section.
This comparison isolates the effect of covariance assumptions. The pooled variance is a stipulated teaching value, not a claimed ML or REML estimate from data. With finite samples and estimated covariance parameters, actual fitted SEs and reference distributions require the chosen analysis method.
Under the known heterogeneous variances and a normal approximation, an unadjusted 95% interval for the AAO contrast is approximately 0.089 to 0.311 Pa. This is an analytical illustration centred on the stipulated synthetic effect. It is not an empirical confidence interval from a completed study.
Interaction and multiplicity
A group-by-section interaction asks whether the four group contrasts are equal. It is different from asking whether one section's group contrast equals zero. Specify the primary contrast and any family of additional contrasts before examining results, and apply an appropriate multiplicity strategy.
The difference between the AAO and DAO group contrasts is 0.20 Pa. Within a person, the shared offset cancels when comparing those sections. Under the synthetic assumptions, the SE of this difference-in-differences is about 0.045 Pa because its variance is \((0.04+0.01)(1/50+1/50)\).
Diagnostics and sensitivity plan
Check residual distributions, spread by section and group, unusual profiles, missing sections, and convergence. Compare the common-residual-variance model with a justified section-specific alternative while holding the scientific mean question constant. A marginal model with an estimable covariance or a suitably corrected cluster-robust analysis can provide additional sensitivity checks.
Do not add an unrestricted residual covariance on top of an unrestricted latent profile or redundant random-intercept decomposition. State missingness assumptions and inspect whether incomplete profiles differ systematically from complete ones. Observational group differences require careful consideration of confounding and cannot automatically be interpreted as effects of repair.
Illustrative Methods text
For this synthetic teaching exercise, WSS was represented using a Gaussian linear mixed model with fixed group, categorical section, and their interaction, together with a person-level random intercept. Section-specific residual variances were specified; a common residual variance was used as a sensitivity assumption. Group contrasts and differences between those contrasts were defined before calculation.
Illustrative Results text
The stipulated TOF-minus-control differences were 0.20, 0.10, 0.05, and 0.00 Pa across the four sections. With known synthetic covariance inputs, the corresponding approximate SEs were 0.057, 0.072, 0.057, and 0.045 Pa; imposing the stipulated common residual variance produced equal SEs of about 0.058 Pa. These are worked calculations and do not constitute evidence about repaired-TOF patients.
A real report must replace these inputs with verified estimates, confidence intervals, diagnostic findings, and fully described methods. It must distinguish primary findings from sensitivity results and state any changes in interpretation. The model's roles remain traceable: fixed effects describe group-section means, random effects describe shared personal departures, and residual structure describes remaining spread and dependence.
- Key idea: A worked calculation can demonstrate model logic without inventing clinical data, fitted outputs, or empirical conclusions.
Documentation supporting the model and software details: nlme: fitting the stated mean and covariance model.
Concept checklist
01. Must a linear mixed model always have a continuous outcome and repeated measures?
The standard Gaussian linear mixed model is designed for an approximately continuous outcome with a suitable conditional error distribution. Repeated measurements are one useful setting, but clustering can also arise from other shared units. Binary and count outcomes generally require an appropriate generalized mixed model rather than the same Gaussian model.
02. Can fixed and random intercepts occur in the same model?
Yes. The fixed intercept describes the shared reference mean, and the random intercept describes a person's departure. Their sum, \(\beta_0+b_{0i}\), gives that person's modelled reference level.
03. Does a random slope replace the fixed slope?
No. The fixed slope describes the mean relationship, and the random slope adds a personal departure. The person's slope is \(\beta_1+b_{1i}\).
04. Are a fixed interaction and a random slope the same thing?
No. A fixed interaction describes a systematic difference in mean relationships between specified groups. A random slope describes remaining variation in relationships across people or other clusters. Both may be needed in one model.
05. Can a categorical section predictor have random slopes?
Yes. They are personal departures from section contrasts. Four levels require three independent contrasts, and an intercept plus all three random contrasts creates a full personal profile. Whether its covariance is identifiable depends on residual assumptions and replication.
06. Why does the teaching TOF analysis omit full random-section effects?
With one observation per person per section, a full unrestricted latent four-section covariance already uses the ten observable covariance quantities. Adding a free independent residual variance creates a decomposition that cannot be uniquely recovered. More people alone do not resolve that ambiguity; replicate readings or justified restrictions may help.
07. Is section-specific residual variance a random slope?
No. It lets remaining measurement spread differ by section. A random section slope lets personal mean contrasts differ. These are distinct components and should not be substituted for one another.
References and further verification
- Fitting Linear Mixed-Effects Models using lme4 https://lme4.github.io/lme4/articles/lmer.html
- R nlme documentation: Linear Mixed-Effects Models https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/lme.html
- R nlme documentation: AR(1) Correlation Structure https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/corAR1.html
- lme4 documentation: Test Fitted Model for (Near) Singularity https://lme4.github.io/lme4/reference/isSingular.html
- R nlme documentation: Constant Variance Function, varIdent https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/varIdent.html
- Stata manual: Multilevel mixed-effects linear regression https://www.stata.com/manuals/memixed.pdf
- Stata manual: Postestimation tools for mixed https://www.stata.com/manuals/memixedpostestimation.pdf
Key takeaways
- Fixed effects describe shared mean relationships; random effects describe personal departures around those relationships.
- A random slope adds to the fixed slope, and a fixed interaction answers a different question.
- Repeated measurements can occur across time or anatomical locations; modelling choices follow the question and design.
- Separate total covariance from its decomposition, and check that the requested components are identifiable.
- Equal standard errors can follow from symmetry; judge covariance assumptions through diagnostics and sensitivity analysis.
Related in the wiki: [[describe-data-structure-models]] [[mixed-model-random-slopes]] [[correlated-data-model-guide]] [[mixed-model-random-intercept-shared-offset]]