The data and one line

◆ The Blueprint
The data

Group \(j\) is centered at \(c_j\) on the x axis, with the centers evenly spaced from −2 to 2. Its true intercept \(\alpha_j\) runs evenly from \(k\) down to \(-k\), where \(k\) is 3 for strong confounding, 1 for mild, and 0 for none.

$$\begin{gathered} x_{ij} \sim N(c_j,\ 0.5^2), \qquad \varepsilon_{ij} \sim N(0,\ 0.5^2) \\ y_{ij} = \alpha_j + 1 \cdot x_{ij} + \varepsilon_{ij} \end{gathered}$$

With no confounding every \(\alpha_j\) is 0, so all the groups lie along one line of slope 1.

Why the overall slope can reverse

The least squares slope through all the points is a weighted average of two slopes: the slope inside the groups, \(b_W\), and the slope of the group means against the group centers, \(b_B\). The weights are how much of the spread in x is inside the groups, \(S_W = \sum (x_{ij} - \bar x_j)^2\), and between them, \(S_B = \sum n_j (\bar x_j - \bar x)^2\).

$$\hat\beta_1 = \frac{S_W\, b_W + S_B\, b_B}{S_W + S_B}$$

With strong confounding the intercepts fall 1.5 for every 1 the center rises, so the group means lie along a slope of about \(1 - 1.5 = -0.5\). With five groups the centers spread x about 8 times as much as the points inside a group do, so \(\hat\beta_1 \approx (1 \times 1 + 8 \times (-0.5))/9 = -0.33\); the default data set gives −0.38. With mild confounding the between-group slope is about \(1 - 0.5 = 0.5\), and \(\hat\beta_1 \approx 0.56\); the default data set gives 0.52. The ratio is about 10.7 with three groups and 6.9 with eight.

In R

lm(y ~ x, data = d) fits one line. lmList(y ~ x | group, data = d) from lme4 fits a line to each group. Part 1 of The Script runs both fits.

The groups

Model 2: each group's intercept

◆ The Blueprint
Model 1: no group effect
$$y_{ij} = \beta_0 + \beta_1 x_{ij} + \varepsilon_{ij}$$

One intercept and one slope for every group: lm(y ~ x). Point \(i\) is in group \(j\).

Model 2: fixed group effect
$$y_{ij} = \beta_{0j} + \beta_1 x_{ij} + \varepsilon_{ij}$$

Each group's own intercept and one slope shared by every group: lm(y ~ x + group), a dummy variable for each group. This is the course's fixed group effect, lm(score ~ company), with x added. A group is a fixed effect when its levels are the only ones you care about. Fitting a separate line to each group, lm(y ~ x) within each group, would also give each group its own slope.

Why small groups are noisy

Group \(j\)'s intercept is its mean \(\bar y_j\) moved along the shared slope to \(x = 0\): one intercept from the group's own points, with a slope fitted to all the groups. Its standard error is about that of a mean of \(n_j\) points:

$$\text{SE}(\hat\beta_{0j}) \approx \frac{\sigma}{\sqrt{n_j}}$$

With \(\sigma = 1.5\), that is 0.67 for a group of 5 points and 0.15 for a group of 100. The error does not depend on \(\sigma_u\): each group's intercept is its mean moved along the shared slope, that slope is fitted to the variation inside the groups, and neither depends on how far apart the groups are.

The tradeoff

Model 1's intercept is biased for every group whose true intercept differs from the average. Model 2's estimates are unbiased, but in small groups their variance is large. This is the bias-variance tradeoff applied to group estimates (Bias-Variance Tradeoff).

The data

Group \(j\)'s true intercept is \(5 + u_j\), with \(u_j \sim N(0, \sigma_u^2)\), and every group's true slope is \(\beta_1\). Each x is uniform from −3 to 3, and \(y = 5 + u_j + \beta_1 x + \varepsilon\), with \(\varepsilon \sim N(0, \sigma^2)\). Unequal sizes are evenly spaced on a log scale from 5 to 100 points: 5, 11, 22, 47, and 100 for five groups.

In R

lm(y ~ x, data = d) for Model 1 and lm(y ~ x + group, data = d) for Model 2. Part 2 of The Script runs both fits.

Model 3's lines

Each group's intercept

◆ The Blueprint
Model 3: random group effect
$$\begin{gathered} y_{ij} = \beta_0 + \beta_1 x_{ij} + u_j + \varepsilon_{ij} \\ u_j \sim N(0, \sigma_u^2), \qquad \varepsilon_{ij} \sim N(0, \sigma^2) \end{gathered}$$

The groups' departures \(u_j\) are treated as random draws, so the model estimates their variance \(\sigma_u^2\) instead of one coefficient per group. A group is a random effect when its levels are a sample of many. lme4 estimates \(\sigma_u^2\) and \(\sigma^2\) by restricted maximum likelihood (REML), then \(\beta_0\) and \(\beta_1\) by generalized least squares. A model with both fixed and random effects is a mixed model.

Where the weighted average comes from

lme4's estimate of \(u_j\) combines two pieces of information: the average of group \(j\)'s residuals from the overall line, whose variance is \(\sigma^2/n_j\), and the distribution of the \(u_j\), with mean 0 and variance \(\sigma_u^2\). Each is weighted by one over its variance.

$$\hat u_j = \frac{n_j/\hat\sigma^2}{n_j/\hat\sigma^2 + 1/\hat\sigma_u^2}\,\big(\bar y_j - \hat\beta_0 - \hat\beta_1 \bar x_j\big) = w_j\,(a_j - \hat\beta_0)$$

The fraction simplifies to \(w_j = \hat\sigma_u^2/(\hat\sigma_u^2 + \hat\sigma^2/n_j)\), and the group's intercept \(\hat a_j = \hat\beta_0 + \hat u_j\) is \(w_j a_j + (1 - w_j)\hat\beta_0\). ranef() returns these \(\hat u_j\) to the last digit. The own intercept \(a_j\) is Model 2's intercept computed with Model 3's slope; the two slopes are close, so \(a_j\) is within a few hundredths of Model 2's intercept.

Without x

In lmer(y ~ 1 + (1 | group)), \(a_j\) is the group mean \(\bar y_j\) and \(\hat\beta_0\) is \(\hat\mu\), so a group's estimate is \(\hat\mu + w_j(\bar y_j - \hat\mu)\): the overall average plus a share \(w_j\) of the group's gap.

The weight, λ, and the ICC
$$w_j = \frac{n_j}{n_j + \lambda}, \qquad \lambda = \frac{\hat\sigma^2}{\hat\sigma_u^2} = \frac{1 - \text{ICC}}{\text{ICC}}$$

A higher ICC means the groups differ more relative to the noise, so \(\lambda\) is smaller and every weight is larger.

Why shrink

The weighted average adds a little bias toward \(\hat\beta_0\) and removes much of the variance of a small group's own estimate, so on average the group estimates are closer to the true intercepts. When \(\hat\sigma_u^2 = 0\), lme4 reports a singular fit and every weight is 0.

In R

fit <- lmer(y ~ x + (1 | group), data = d); fixef(fit) for \(\hat\beta_0, \hat\beta_1\); as.data.frame(VarCorr(fit)) for \(\hat\sigma_u^2\) and \(\hat\sigma^2\); coef(fit)$group for each group's intercept. Part 3 of The Script checks the weighted average against lme4.

The fitted lines

Variance components

Each group's departures

◆ The Blueprint
Random intercepts and slopes
$$y_{ij} = \beta_0 + \beta_1 x_{ij} + u_{0j} + u_{1j}\, x_{ij} + \varepsilon_{ij}$$
$$\begin{pmatrix} u_{0j} \\ u_{1j} \end{pmatrix} \sim N\!\left( \begin{pmatrix} 0 \\ 0 \end{pmatrix}, \begin{pmatrix} \sigma_{u0}^2 & \rho\,\sigma_{u0}\sigma_{u1} \\ \rho\,\sigma_{u0}\sigma_{u1} & \sigma_{u1}^2 \end{pmatrix} \right)$$

Group \(j\)'s intercept is \(\beta_0 + u_{0j}\) and its slope is \(\beta_1 + u_{1j}\). The off-diagonal entry is the covariance of the two departures. Groups that start high might rise more slowly (\(\rho < 0\)) or faster (\(\rho > 0\)).

The three models in lme4

lmer(y ~ x + (1 | group)): random intercepts, one shared slope.
lmer(y ~ x + (0 + x | group)): random slopes, one shared intercept.
lmer(y ~ x + (1 + x | group)): both, with their correlation.

Each group's estimates

As with random intercepts, lme4's estimates of a group's departures combine its own residuals with the distribution of the departures. With \(\Sigma\) the estimated covariance matrix above and \(Z_j\) group \(j\)'s columns of 1 and x,

$$\begin{pmatrix} \hat u_{0j} \\ \hat u_{1j} \end{pmatrix} = \big(\hat\Sigma Z_j^\top Z_j + \hat\sigma^2 I\big)^{-1} \hat\Sigma Z_j^\top \big(y_j - \hat\beta_0 - \hat\beta_1 x_j\big)$$
Singular fits

lme4 reports boundary (singular) fit: see help('isSingular') when an estimated variance is 0 or an estimated correlation is ±1, the edge of the values each can take. With few groups this happens even when the true variance is above 0, because six groups give six departures to estimate it from. lme4 reports a correlation of ±1 when a variance is 0, so the correlation means nothing on such a fit. A message that begins Model failed to converge means the optimizer stopped without passing lme4's convergence check; treat those estimates with caution.

When to add random slopes

Add them when the separate lines' slopes differ by more than their noise. anova(fit_ri, fit_both) refits both models by maximum likelihood and compares them with a likelihood ratio test.

In R

lmer(y ~ x + (1 + x | group), data = d), as.data.frame(VarCorr(fit)) for the variance components, ranef(fit)$group for each group's departures, and isSingular(fit). Part 4 of The Script fits the models on this tab.

The groups and the three fits

Each group's intercept

The two models over 100 data sets

◆ The Blueprint
Mixed models for any response

A generalized linear mixed model (GLMM) puts the random group effects inside a generalized linear model's link. glmer() is lmer() plus a family, the way glm() is lm() plus a family (Link Functions).

$$g(\text{mean of } y_{ij}) = \beta_0 + \beta_1 x_{ij} + u_{0j} + u_{1j}\, x_{ij}$$

Binary: \(g\) is the log-odds, \(\log\frac{p}{1-p}\), with family = binomial. Count: \(g\) is the log of the mean count, with family = poisson. Continuous: \(g\) is the identity, fitted with lmer(). This tab fits \((1 + x \mid \text{group})\) when the slope spread is above 0 and \((1 \mid \text{group})\) when it is 0.

glmer has no closed form for \(\hat u_j\). It maximizes a Laplace approximation of the likelihood, and the group estimates come out of that fit. Model 2 is glm(y ~ x + group, family = binomial): one intercept for each group and a shared slope, the course's fixed group effect.

A perfect record

When every point in a group is 1, or every point is 0, Model 2's log-likelihood keeps rising as that group's intercept grows. glm stops at a large estimate, near 20 on the log-odds scale, with an enormous standard error, and usually prints no warning. When a fitted probability reaches 0 or 1 to machine precision it warns glm.fit: fitted probabilities numerically 0 or 1 occurred. Model 3's normal distribution for the \(u_j\) keeps every group's estimate finite.

The 100 data sets

Each response type is simulated 100 times, with seeds 1 to 100, at the guided settings: five groups of 5 to 100 points, \(\beta_0 = 3\), \(\beta_1 = 0.8\), \(\sigma_{u0} = 2\), \(\sigma_{u1} = 0\), and \(\sigma = 1\). With \(\sigma_{u1} = 0\), Model 3 is \((1 \mid \text{group})\), so it and Model 2 both give each group one intercept with a shared slope. A group's true intercept is \(\beta_0 + u_{0j}\), on the link scale. The typical error is the median of \(|\text{estimate} - \text{true intercept}|\) over the 100 data sets, and the true top group is the one with the largest true intercept.

In R

glmer(y ~ x + (1 | group), family = binomial, data = d) for Model 3 and glm(y ~ x + group, family = binomial, data = d) for Model 2. Part 5 of The Script runs the 100 data sets.

lme4, function by function
CallWhat it returns
lmer(y ~ x + (1 | group), data = d)A linear mixed model, fitted by REML: a fixed intercept and slope, and a random intercept for each group.
glmer(y ~ x + (1 | group), family = binomial)The same model on the log-odds scale. family = poisson fits counts on the log scale.
fixef(fit)The fixed effects \(\hat\beta_0\) and \(\hat\beta_1\): the overall line.
as.data.frame(VarCorr(fit))The variance components: \(\hat\sigma_u^2\) for each random term and \(\hat\sigma^2\) for the residual.
ranef(fit)$groupEach group's estimated departures \(\hat u_j\) from the overall line.
coef(fit)$groupEach group's intercept and slope: the fixed effects plus its departures.
isSingular(fit)TRUE when a variance is estimated as 0 or a correlation as ±1.
lmList(y ~ x | group, data = d)A separate lm() for each group: the group lines on the Simpson's Paradox tab.
mixed_effects_in_R.R