Mixed models and trial-level data
What averaging-then-testing discards, linear mixed models with subject and item random effects on single trials, convergence, and reporting.
Prerequisites: L3.6 · Single-trial approaches
1 claim on this page is unverified. TODO(confirm) marks a specific statement the author has not yet checked against a
primary source. Everything else on this page has been reviewed. Treat a marked claim as
provisional and go to the cited source rather than quoting the sentence.
Objectives
- Explain what averaging-then-testing discards
- Fit linear mixed models with subject (and item) random effects on single-trial amplitudes
- Handle convergence problems
- Report mixed-model results
Why this matters
The standard ERP analysis throws away almost everything it measured. Forty trials per condition per subject become one number per condition per subject, and the paired test that follows never learns that one subject contributed 38 clean trials and another contributed 12, or that reaction time varied fourfold across trials, or that half the items were words the participant had seen before. A linear mixed model keeps the trials and adds the structure back explicitly — and, done carelessly, it is also the fastest way to turn 800 non-independent observations into a p-value that claims 798 degrees of freedom.
Concepts
What the average discards
Averaging is not wrong. It is a variance-reduction step with a known cost, and it is the right move when the design is balanced and the question is about condition means. What it removes:
- Unequal precision. A subject with 12 surviving trials and a subject with 38 enter the group test as one number each, weighted equally. The standardized measurement error (L3.5) says they are not equally precise, and the group model has no way to know.
- Trial-level predictors. Reaction time, trial number, preceding-trial condition, stimulus repetition, pre-stimulus alpha power — all of these vary within subject and all of them vanish in the average. L3.6 introduced single-trial views for exactly this reason.
- Item structure. When the stimuli are a sample — faces, words, sounds — they are a random factor as much as subjects are. A result that generalises over these subjects but not over these items is a weaker result than it appears, and averaging over items hides the distinction entirely.
- The shape of the within-subject distribution. Skew, outliers and bimodality are invisible in a mean.
The model, in one line and then in words
For a single-trial amplitude y measured on trial j of subject i:
y[i,j] = β0 + β1 · x[i,j] + u0[i] + u1[i] · x[i,j] + ε[i,j]
β0,β1— the fixed effects: the grand intercept and the average condition effect.β1is the number the paper reports.u0[i],u1[i]— the random effects: this subject’s departure from the grand intercept, and this subject’s departure from the average condition effect. They are not parameters to be estimated one per subject; they are draws from a distribution whose variance (and their correlation) is what the model estimates.ε[i,j]— trial-level residual noise.
The random intercept says subjects differ in overall amplitude. The random slope says the effect itself differs between subjects — and that is the assumption a paired t-test on averages makes implicitly and cannot examine.
Adding a second, crossed random factor for items is a one-term change, u0[k] for item k, and it is crossed rather than nested because every subject sees (some of) the same items. Crossed random effects are the reason mixed models displaced the older by-subject and by-item analyses that had to be run twice and reconciled by hand.
What changes when you fit one, and what does not
Two expectations worth correcting before the notebook runs.
The fixed-effect estimate usually moves very little. In a balanced within-subject design with equal trial counts, the mixed model’s β1 is close to the mean of the per-subject differences, because that is very nearly what it is estimating. Where it moves is where the design is unbalanced — unequal trial counts, missing cells, subjects contributing different numbers of items — because the model weights each subject by the precision of that subject’s data instead of equally. That weighting is the point, not a side effect.
The standard error and the degrees of freedom are where the action is. A random-slope term acknowledges that the effect varies across subjects, which usually widens the interval on β1 relative to a model with random intercepts only. A model that omits it can report a confident fixed effect that the data do not support. The exercise below asks you to compare the two on real data and report both numbers, because “the estimate barely moved but the interval grew by half” is the normal and instructive result.
What a mixed model does not fix is the unit of analysis. Fitting ordinary least squares to single trials with no random effect for subject treats 800 correlated observations as 800 independent ones; the estimate may be fine and the standard error will be far too small. This is pseudoreplication, and L6.3 shows the same failure in a dataset where 500 segments come from ten people.
Random-effects structure: maximal, parsimonious, and declared
The literature offers two defensible positions. Keep it maximal: include every random slope the design justifies, because omitting a slope that is really there inflates the Type I error rate for the corresponding fixed effect. Keep it parsimonious: over-parameterised random structures fail to converge, and a model that cannot be estimated is not a conservative model, it is no model.
The practical resolution is procedural rather than statistical, and it is the Level 6 answer to most such questions: decide the structure and the simplification rule before fitting, write both down, and report which branch you took. A rule such as “start maximal; if the fit is singular, drop the random correlation, then the random slope for the smaller-variance term, and report the first model that converges” is defensible precisely because it could not have been steered by the result.
Convergence, and what a singular fit is telling you
A singular fit — a random-effect variance estimated at zero, or a random intercept–slope correlation at exactly ±1.00 — is usually not a numerical failure. It is the estimate landing on the boundary of the parameter space, which means the data do not contain enough information to identify that many random-effect parameters. The useful responses, in order:
- Check the data first. Wildly different predictor scales, an uncentred continuous predictor, or a factor with an empty cell produce convergence problems that are really data problems.
- Centre and scale continuous predictors, and use a sum or deviation coding for factors, so that the intercept is interpretable and the random terms are not fighting a strong intercept–slope dependence created purely by the parameterisation.
- Try another optimizer or more iterations. This is a legitimate diagnostic — if several optimizers agree, the fit is real. What is not legitimate is running several and reporting whichever one converged to the answer you preferred; that is a search, and Level 6 is about searches.
- Simplify by the rule you wrote down, and say in the paper that you did, and which model you ended on.
- Report the singularity if you keep the model. A variance of zero is a finding about your data, not an embarrassment.
Where the confound lives
A trial-level model is also where subject-level covariates enter naturally — age, group, medication state, site — as fixed effects alongside the experimental manipulation. That is an opportunity and a trap. pf-group-demographic-confound is the trap: when patients are older and more male than controls, a group term in a mixed model does not disentangle group from age; it estimates group adjusted for age under the assumption that the adjustment is correctly specified and that the two are not collinear. With ds-iowapd’s 68/32 male-to-female split in the patient group against 26/23 in the controls, and every patient recorded on medication, “adjusted for age and sex” is a modelling assumption to defend, not a box that has been ticked.
Reporting a mixed model
A reader needs to be able to rebuild the model from the text:
- The data structure: what one row is, how many rows, how many subjects, how many items, trials per subject per condition after rejection, and what was excluded.
- The full model formula, including the random-effects structure, in a notation that names the software (
lme4orstatsmodelssyntax is fine, and naming it is required, because the same formula means slightly different things in different packages). - The contrast coding for every factor, since it determines what the intercept and the fixed effects mean.
- Fixed effects with estimate, standard error, confidence interval and the degrees-of-freedom method used for any p-value (Satterthwaite, Kenward–Roger, likelihood-ratio, or parametric bootstrap — they disagree, and the choice is reportable).
- Random effects: estimated standard deviations and correlations, and the residual.
- Convergence status, the optimizer, any simplification applied and the pre-specified rule that triggered it.
- Software and versions, and the seed for anything resampled.
Fit the mixed model because the design has structure the average cannot represent — unequal trial counts, a trial-level covariate, crossed items — not because it is more sophisticated. If the design is balanced, the question is about condition means, and you have no trial-level predictor, the paired test on per-subject averages is the same analysis with fewer ways to go wrong, and it is easier for a reader to check. The model should be the simplest one that can express the question you pre-registered.
Explore
This lesson has no widget; the object of study is the shape of your own data table. Before you fit anything, do this on paper for the design you actually have:
- write down what one row is — one trial of one subject, or one trial of one subject on one item — and count the rows, the subjects and the items;
- for each factor, mark whether it varies within subject or between subjects. Only the within-subject ones can carry a random slope by subject, which settles most of the “maximal model” argument before it starts;
- list the trial-level predictors you actually recorded — reaction time, trial number, preceding condition, pre-stimulus power — and cross out the ones the per-subject average would destroy;
- write the surviving trial counts per subject per condition and find the widest and the narrowest. That ratio is how much the mixed model’s precision weighting will differ from an equal-weight average;
- write the simplification rule you will apply if the maximal model does not converge, and date it. That sentence is worth more than any of the modelling that follows.
Practice
Mixed models and trial-level data: random intercepts, random slopes, crossed subject-by-item effects and single-trial regression on P3 amplitude, with the lme4 equivalents nb-6-2-lmm
Downloads from ds-erpcore.
Single-trial P3 amplitudes from ds-erpcore, one row per trial, fitted with statsmodels MixedLM: first random intercepts by subject, then random intercepts and slopes, with the lme4 formula printed beside each so the R equivalent is explicit. The notebook prints the fixed-effect estimate and its standard error for both models, the random-effect standard deviations, the convergence status, and — for contrast — the per-subject average analysis on the same data.
Exercises
Exercise ex-6-2-random-slope-fixed-effect
NumericFrom the notebook: the condition fixed effect, in µV, from the model with random intercepts AND random slopes by subject. Note the random-intercept-only estimate and both standard errors as you go — the next exercise asks for them.
Exercise ex-6-2-random-slope-report
Free responseReport both fits in the two sentences a results section would use: the condition fixed effect with its standard error from the random-intercept model, the same from the random-intercept-plus-slope model, and what changed.
Exercise ex-6-2-what-averaging-discards
Multiple selectYou have 20 subjects × 2 conditions × up to 40 trials, and you average to one amplitude per subject per condition before testing. Select everything that this step makes it impossible to model.
Exercise ex-6-2-singular-fit
Multiple choiceYour pre-registered maximal model — random intercepts and random slopes for condition, with their correlation, by subject — returns a singular fit, with the estimated subject-level intercept–slope correlation at exactly −1.00. Which response is defensible?
Pitfalls
Age, sex or medication state differ between groups
- Symptom
- Patients older and sex-skewed relative to controls; patients recorded on medication.
- Cause
Clinical groups are rarely sampled the way controls are. Patients are older on average, drawn from a clinic rather than a community, more often of one sex for many conditions, and recorded while treated. Each of those variables has its own, well-documented effect on the EEG, and when it is unbalanced between groups the group comparison measures it too. The effect is compounded when the variable a…
- Detect
- Tabulate age, sex, medication state, handedness and any other covariate per group, with means, spreads and counts; report the imbalance rather than assuming matching. - Test whether the candidate biomarker correlates with the covariates within the control group; a measure that tracks age in controls will track an age imbalance between groups. - Compare artifact rates and data quality between gr…
- Fix
- Design: age- and sex-matched recruitment; record patients off medication where clinically possible, or record both states; document the covariates in participants.tsv. - Analysis: model the covariates (age, sex, medication) rather than only matching on them, with enough data to estimate their effects (L6.2); report the group effect with and without adjustment. - Interpretation: state the confou…
Reading
- Frömer, Maier & Abdel Rahman (2018). Single-trial LMM pipeline. unverified