Field Guide to the R Mixed Model Wilderness
  1. 1  Introduction
  • Preface
  • 1  Introduction
  • 2  Zen and the Art of Statistical Analysis
  • 3  Mixed Model Background
  • 4  Model Prep & Workflow
  • Experiment designs
    • 5  Randomized Complete Block Design
    • 6  Factorial RCBD Design
    • 7  Split Plot Design
    • 8  Split-Split Plot Design
    • 9  Strip Plot Design
    • 10  Incomplete Block Design
    • 11  Latin Square Design
  • 12  Repeated Measures
  • 13  Marginal Means and Contrasts
  • 14  Variance and Variance Components
  • 15  Troubleshooting
  • 16  Additional Resources
  • References

Table of contents

  • 1.1 Data
  • 1.2 Packages
    • 1.2.1 Table of required packages for modelling
    • 1.2.2 Optional packages
  • View source
  • Report an issue

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).

Download Data (.zip)

1.2 Packages

1.2.1 Table of required packages for modelling

Table 1.1: Table of required packages
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

Table 1.2: Table of optional packages
Package Name Function
here For setting the working directory
ggplot plotting
desplot plotting
agridat to download example dataset
agricolae to download example dataset

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.

Bates, Douglas, Martin Mächler, Ben Bolker, and Steve Walker. 2015. “Fitting Linear Mixed-Effects Models Using lme4.” Journal of Statistical Software 67 (1): 1–48. https://doi.org/10.18637/jss.v067.i01.
Bolker, Ben, and David Robinson. 2024. Broom.mixed: Tidying Methods for Mixed Models. https://CRAN.R-project.org/package=broom.mixed.
Hartig, Florian. 2022. DHARMa: Residual Diagnostics for Hierarchical (Multi-Level / Mixed) Regression Models. https://CRAN.R-project.org/package=DHARMa.
Kuznetsova, Alexandra, Per B. Brockhoff, and Rune H. B. Christensen. 2017. “lmerTest Package: Tests in Linear Mixed Effects Models.” Journal of Statistical Software 82 (13): 1–26. https://doi.org/10.18637/jss.v082.i13.
Lenth, Russell V. 2022. Emmeans: Estimated Marginal Means, Aka Least-Squares Means. https://CRAN.R-project.org/package=emmeans.
Lüdecke, Daniel, Mattan S. Ben-Shachar, Indrajeet Patil, Philip Waggoner, and Dominique Makowski. 2021. “performance: An R Package for Assessment, Comparison and Testing of Statistical Models.” Journal of Open Source Software 6 (60): 3139. https://doi.org/10.21105/joss.03139.
Pinheiro, José C., and Douglas M. Bates. 2000. Mixed-Effects Models in s and s-PLUS. New York: Springer. https://doi.org/10.1007/b98882.
Pinheiro, José, Douglas Bates, and R Core Team. 2023. Nlme: Linear and Nonlinear Mixed Effects Models. https://CRAN.R-project.org/package=nlme.
Preface
2  Zen and the Art of Statistical Analysis

© 2025

Uidaho Logo

  • View source
  • Report an issue