Source-linked AI summary
Bayesian linear mixed models using Stan: A tutorial for psychologists, linguists, and cognitive scientists
Tanner Sorensen, Shravan Vasishth
TL;DR
Linear mixed models are widely used in repeated-measures research, but Bayesian model specification can be difficult for researchers with limited statistical background. This tutorial introduces Stan through progressively more complex examples, including two-condition and 2 × 2 factorial designs. It presents Stan as capable of fitting highly complex models while cautioning that model fit and model appropriateness are distinct concerns.
Problem
Bayesian linear mixed-model specification requires statistical background, creating a practical entry barrier for researchers who use LMMs but have limited knowledge of their underlying machinery.
Method
The tutorial teaches Bayesian LMM specification in Stan by progressing from a two-condition repeated-measures study to a 2 × 2 factorial design represented with a design matrix.
Results
The tutorial demonstrates Stan’s ability to fit arbitrarily complex LMMs, including models with crossed subject and item random effects and large variance-covariance matrices.
Takeaways & Limitations
Stan provides a scalable framework for learning and implementing Bayesian LMMs across standard and more complex experimental designs.
Takeaways & Limitations
The ability to fit a complex model does not establish that fitting such a model is appropriate in general.
Abstract
from arXiv · showhide
With the arrival of the R packages nlme and lme4, linear mixed models (LMMs) have come to be widely used in experimentally-driven areas like psychology, linguistics, and cognitive science. This tutorial provides a practical introduction to fitting LMMs in a Bayesian framework using the probabilistic programming language Stan. We choose Stan (rather than WinBUGS or JAGS) because it provides an elegant and scalable framework for fitting models in most of the standard applications of LMMs. We ease the reader into fitting increasingly complex LMMs, first using a two-condition repeated measures self-paced reading study, followed by a more complex $2\times 2$ repeated measures factorial design that can be generalized to much more complex designs.
1 Introduction
The tutorial introduces Bayesian linear mixed-model specification in Stan for researchers who use LMMs but may have limited background in their underlying machinery. It motivates Stan’s use for increasingly complex designs while noting that model complexity and appropriateness are separate concerns.
- Motivation: Bayesian programming languages make linear mixed models easier to fit, but defining the models requires statistical background.The paper highlights the difficulty of putting Bayesian statistics into practice despite tools such as WinBUGS, JAGS, and Stan.
- Purpose and audience: The tutorial eases psychologists, linguists, and cognitive scientists into their first Stan-based Bayesian linear mixed models.It targets lme4 users who may not understand design matrices, contrast coding, or random-effects variance-covariance matrices.
- Why Stan: Stan is chosen over JAGS and WinBUGS because it can fit arbitrarily complex models, including models with crossed subject and item random effects.The example includes 14 fixed-effect predictors and two 14 × 14 random-effects variance-covariance matrices.
- Scope and assumptions: The authors assume readers already want to learn Bayesian LMMs and know Bayes’ Theorem, while directing broader Bayesian-versus-frequentist justification elsewhere.The tutorial is not intended as a comprehensive explanation of the advantages of Bayesian methods or as a replacement for advanced statistical references.
- Tutorial structure: The tutorial builds increasingly complex LMMs from a two-condition repeated-measures dataset before demonstrating a 2 × 2 factorial design.It explains model structure and inference for the two-condition design, then generalizes the approach to the factorial design.
- Scope and assumptions: Although Stan can fit highly complex models, whether such a model makes sense is a separate issue.The authors point readers to discussions of the appropriateness of complex models.
2 Example 1: A two-condition repeated measures design
This example uses Chinese self-paced reading data to build increasingly complex Bayesian linear mixed models in Stan. The analysis provides evidence that object relatives may be easier to process than subject relatives, but credible intervals include no difference.
- Modeling strategy: The tutorial builds from a fixed-effects model to varying intercepts and then varying intercepts with varying slopes for subjects and items.These models express how the observed reading times were generated while accounting for subject- and item-level variability.
- Fixed-effects model: With ±1 contrast coding, β0 is the grand mean of log reading time and 2 × β1 is the difference between object- and subject-relative means.The expected log reading time is β0 + β1 for object relatives and β0 − β1 for subject relatives.
- Results: Approximately 94% of the fixed-effects posterior for β1 was below zero, but its 95% credible interval included zero.The varying-intercepts model likewise retained a credible interval for β1 that included zero, so no difference in reading times could be excluded.
- Results: In the varying-intercepts, varying-slopes model, the posterior probability that β1 was below zero fell to 90%, and subject intercepts and slopes showed a negative correlation.The negative correlation suggests that slower-average subjects were slower on object relatives, whereas item intercepts and slopes showed no clear pattern.
3 Inference
Inference uses posterior distributions and 95% credible intervals to assess fixed effects and correlations among varying intercepts and slopes. The results provide weak evidence for a negative relative-clause slope and insufficient information about item-level correlations.
- 95% credible intervals summarize posterior uncertainty for parameters such as the fixed slope β1 and subject-level correlation ρu.The tutorial presents credible intervals as one approach to inference from the posterior distributions.
- The 95% credible interval for β1 is [−0.092, 0.023], so the interval includes 0 despite a 90% posterior probability that β1 is negative.The authors therefore find it difficult to conclude that object relatives are read faster than subject relatives.
- The 95% credible interval for ρu is (−1, 0.1), yielding uncertain evidence that subject-level varying intercepts and slopes are negatively correlated.The authors describe only weak evidence that faster-than-average readers show greater object-relative slowdowns.
- With only 15 items, the analysis cannot support conclusions about item-level correlations between varying intercepts and slopes.The authors suggest fitting a simpler model with item varying intercepts only, although fitting the full covariance model is not harmful here.
- The main conclusion is that the data do not establish that object relatives are harder to process than subject relatives because β1’s credible interval includes 0.The authors allow that the approximately 90% probability of a negative parameter provides weak evidence in that direction.
4 Example 2: Generalizing the linear mixed model to factorial designs
The tutorial extends Bayesian linear mixed models to a 2 × 2 repeated-measures factorial design using Stan's matrix formulation. The model captures fixed effects and by-subject and by-item varying intercepts and slopes, while the formulation can be reused for more complex designs.
- Design: The example analyzes a 2 × 2 repeated-measures self-paced reading study of relative clause type and distance.Relative clause type is coded by so, distance by dist, and their interaction by int.
- Model: The model uses a varying intercept and varying slopes for both subjects and items, with fixed effects for relative clause type, distance, and their interaction.Subject- and item-specific effects are modeled with multivariate normal distributions and covariance matrices.
- Implementation: The design matrix X contains an intercept, the two predictors, and their interaction, with the factorial design repeated across subjects.For this dataset, X has N = 1440 rows and P = 4 fixed-effect columns.
- Implementation: Stan specifies log-normal reading times using fixed effects plus subject and item adjustments, with priors on random-effects correlation matrices.Stan compiles the model and samples from the posterior distribution of its parameters.
- Results: The posterior intervals indicate faster reading of object relatives, a distance-related slowdown, and a greater short-distance slowdown for subject relatives.The HPD interval for relative clause type and distance is below zero, while the interaction HPD is above zero.
- Generalization: The matrix formulation allows future repeated-measures factorial designs to use the same Stan model by changing the design matrices and data specification.This extends the approach beyond the present factorial example without writing a new Stan model.
5 Concluding remarks, and further reading
The paper closes by positioning the tutorial as an introduction to Bayesian linear mixed models and directing readers to further textbooks and examples. The recommended materials span general, psychology-specific, and BUGS-based treatments.
- Conclusion: The tutorial is intended to give readers an initial sense of fitting Bayesian linear mixed models rather than a complete treatment.The authors note that much more remains to be covered.
- Further reading: Recommended further reading includes general introductions by Gelman and Hill and McElreath, plus psychology-specific books by Kruschke and Lee and Wagenmakers.The authors also recommend additional BUGS-language examples as a second book.
- Further reading: Readers familiar with BUGS syntax can consult emerging Stan versions of examples from those books.The paper notes that Stan code for such materials is gradually becoming available on the Stan home page.