<- formula(Y ~ treatment1 + treatment2)
my_formula class(my_formula)
[1] "formula"
Mixed-effects models are called “mixed” because they simultaneously model fixed and random effects. Fixed effects (e.g. treatments) represent population-level (average) effects that should persist across experiments. Fixed effects are similar to the parameters found in “traditional” regression techniques like ordinary least squares. Random effects are discrete units sampled from some population (e.g. plots, participants), and thus they are inherently categorical.
Recall simple linear regression with intercept (\(\beta_0\)) and slope (\(\beta_1\)) effect for subject \(i\). The slope and intercept are chosen in a way so that the residual sum of squares is minimized.
\[ Y = \beta_0 + \beta_1 X + \epsilon \]
If we consider this model in a mixed model framework, \(\beta_0\) and \(\beta_0\) are considered fixed effects (also known as the population-averaged values) and \(b_i\) is a random effect for subject i. The random effect can be thought of as each subject’s deviation from the fixed intercept parameter. The key assumption about \(b_i\) is that it is independent, identically and normally distributed with a mean of zero and associated variance. Random effects are especially useful when we have (1) lots of levels (e.g., many species or blocks), (2) relatively little data on each level (although we need multiple samples from most of the levels), and (3) uneven sampling across levels.
For example, if we let the intercept be a random effect, it takes the form:
\[ Y = \beta_0 + b_i + \beta_1 X + \epsilon \]
In this model, predictions would vary depending on each subject’s random intercept term, but slopes would be the same.
In second case, we can have a fixed intercept and a random slope. The model will be:
\[ Y = \beta_0 + (\beta_1 + b_i)(X) + \epsilon\]
In this model, the \(\beta_i\) is a random effect for subject \(i\). Predictions would vary with random slope term, but the intercept will be the same:
Third case would be the mixed model with random slope and intercept:
\[ Y = (\beta_0 + a_i) + (\beta_1 + b_i)(X) + \epsilon\]
In this model, \(a_i\) and \(b_i\) are random effects for subject \(i\) applied to the intercept and slope, respectively. Predictions would vary depending on each subject’s slope and intercept terms:
Formula notation is often used in the R syntax for linear models. It looks like this: \(Y ~ X\), where \(Y\) is the dependent variable (the response) and \(X\) is/are the independent variable(s) that is, the experimental treatments or interventions.
The package ‘lme4’ has some additional conventions regarding the formula. Random effects are put in parentheses and a 1|
is used to denote random intercepts (rather than random slopes). The table below provides several examples of random effects in mixed models. The names of grouping factors are denoted g
, g1
, and g2
, and covariates as x
.
Formula | Alternative | Meaning |
---|---|---|
(1|g) |
1 + (1|g) |
Random intercept with a fixed mean |
(1|g1/g2) |
(1| 1) + (1|g1:g2) |
Intercept varying among g1 and g2 within g1 |
(1|g1) + (1|g2) |
1 + (1|g1) + (1|g2) |
Intercept varying among g1 and g2 |
x + (x|g) |
1 + x + (1 + x|g) |
Correlated random intercept and slope |
x + (x||g) |
1 + x + (1|g) + (0 + x|g) |
Uncorrelated random intercept and slope |
The first example, (1|g)
suffices for most models and is the only structure used in this guide.