Linear mixed effect modesl

Welcome to this introduction to Linear Mixed-effects Models (LMM)!! In this tutorial we will use R to run some simple LMM and we will try to understand together how to leverage these model for our analysis. LMMs are amazing tools that have saved our asses countless times during our PhDs and Postdocs. They'll probably continue to be our trusty companions forever.

Note

Please bear in mind that this tutorial is designed to be a gentle introduction to running linear mixed-effects models. It will not delve into the mathematics and statistics behind LMM. For those interested in these aspects, numerous online resources are available for further exploration.

But why are these LMMs so amazing?

Linear Mixed-Effects Models are basically linear models… but they allow us to account for both fixed and random effects, making them perfect for dealing with data that has multiple sources of variability, like repeated measures or hierarchical structures. Nice isn’t it? Amazing!!

But……what are fixed and random effects 😅?

Well, while fixed effects represent the consistent, predictable part of your model—the data you are interested in and actively want to explore—random effects represent the variability of different sources in your data that you haven’t explicitly measured and usually aren’t interested in, but which you believe affect your data. These random effects help you model these variations, making your predictions more reliable and robust.

It probably still sounds complex, but everything will be clearer once we get our hands dirty with them! Let’s start!!!!

Settings and data

In this code section we will import the necessary libraries and the data that we will use for this tutorial.

library(lme4)
library(lmerTest)

setwd(".\\")

"C:/Users/tomma/Downloads/dragons(1).RData"
[1] "C:/Users/tomma/Downloads/dragons(1).RData"

The lme4 package is undoubtedly the most popular library for LMM in R. To enhance its functionality, there’s also the lmerTest package, which allows you to extract p-values from lme4 models and provides additional functions to explore your models. In my opinion, you should always use lmerTest alongside lme4. It makes everything easier.

The data

Now let’s import the data. You can download it from here. This dataset is simulated, so some of its variables might not make much sense, but it’ll do the job for this tutorial.

[1] "C:/Users/tomma/OneDrive - Birkbeck, University of London/TomassoGhilardi/PersonalProj/Website/DevStart/CONTENT/Stats"
load(".\\resources\\dragons.RData")
Back to top