1 Introduction
This guide is focused on frequentist implementations of mixed models in R, covering different scenarios common in the agricultural and life sciences.
This is not intended to be a guide to the theory of mixed models, it is focused on R syntactical issues for correct implementations of mixed models only. We do provide a short introduction to mixed models. UCLA provides a longer more comprehensive introduction.
1.1 Data
Readers of this materials are welcome to test any of the code in this guide. All data used in this tutorial are from previously conducted studies and are available in the GitHub repository for this resource and explained in the chapters they are used. You can also download all data sets as a zipped file (51 kb).
1.2 Packages
1.2.1 Table of required packages for modelling
Package | Purpose |
---|---|
lme4 (Bates et al. 2015) | main package for linear mixed models |
lmerTest (Kuznetsova, Brockhoff, and Christensen 2017) | for computing p-values when using lme4 |
nlme (J. Pinheiro, Bates, and R Core Team 2023; J. C. Pinheiro and Bates 2000) | main package for linear mixed models and part of ‘base R’ |
emmeans (Lenth 2022) | for estimating fixed effects and their confidence intervals, and conducting contrasts |
broom.mixed (Bolker and Robinson 2024) | package for presenting the model summary output into a “tidy” workflow. |
DHARMa (Hartig 2022) | for evaluating residuals (error terms) in generalized linear models |
performance (Lüdecke et al. 2021) | For creating diagnostic plots or to compute fit measures |
1.2.2 Optional packages
This entire guide will use the here package for loading data. If you can load your data fine without this package, please carry on; here is not required for running mixed models.