- [[r - lme4]] # Idea Like all statistical models in R, the `lme4` package uses [Wilkinson notation](https://stackoverflow.com/questions/32168247/interpret-wilkinson-notation-linear-regression-model-matlab) for defining models: `y ~ x1 + x2` (learned the name for this notation at rstudio conference 2020). The `brms` package uses the same syntax as `lme4`. Note that by default, `lme4` does not provide probability values (see [Douglas Bates's reasoning](https://stat.ethz.ch/pipermail/r-help/2006-May/094765.html) - [[Bates explains why he refuses to include p-values in lme4]]). # Model specification The general syntax: `y ~ x + (x | group)`, where `|` indicates a grouping factor. - `(1 | group)`: varying group intercept - `(x | group)` or `(1 + x | group)`: varying group intercept and varying x slope within group, with correlations between intercept and slope - `(0 + x | group)` or `(-1 + x | group)`: fixed intercept and varying x slope within group - `(1 | group) + (0 + x | group)` or `(1 + x || group)`: varying group intercept and varying x slope within group, with uncorrelated intercept and slope (`||` is the double-bar notation) # Notes From [[Bates 2014 fitting linear mixed-effects models using lme4]] ![[Pasted image 20201012112016.png]] ![[s20230121_215917 1.png]] > By default, `lme4` assumes that all coefficients associated with the same random-effects term are **correlated**. To specify an uncorrelated slope and intercept (for example), one may either use **double-bar notation**, `(x || g)`, or equivalently use multiple random-effects terms, `(1 | g) + (0 + x | g)`. ![[s20230121_215932.png]] # References - [Ben Bolker explanations - very useful](https://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#model-specification) - equations and code: https://rpsychologist.com/r-guide-longitudinal-lme-lmer - https://cran.r-project.org/web/packages/lme4/lme4.pdf - https://stats.stackexchange.com/questions/13166/rs-lmer-cheat-sheet - [stackexchange cheatsheet](https://stats.stackexchange.com/questions/13166/rs-lmer-cheat-sheet?lq=1) - [Hierachical Models](http://mfviz.com/hierarchical-models/)