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.
With no confounding every \(\alpha_j\) is 0, so all the groups lie along one line of slope 1.
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\).
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.
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.
One intercept and one slope for every group: lm(y ~ x). Point \(i\) is in group \(j\).
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.
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:
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.
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).
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.
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.
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.
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.
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.
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.
A higher ICC means the groups differ more relative to the noise, so \(\lambda\) is smaller and every weight is larger.
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.
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.
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\)).
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.
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,
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.
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.
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.
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).
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.
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.
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.
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.
| Call | What 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)$group | Each group's estimated departures \(\hat u_j\) from the overall line. |
coef(fit)$group | Each 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. |