Random intercepts and random slopes

Today let's understand how a mixed model lets every group in your data keep its own starting point, and sometimes its own rate of change too.

Picture a trial of a new pain-relief drug, run across thirty clinics. Each clinic enrolls ten patients, so the trial has three hundred patients in total. Every patient gets the same drug, at a dose somewhere between 0 and 10 milligrams, and a week later a relief score is recorded on a 0 to 100 scale, where 100 means complete relief.

Simulate that trial in R, so every number in this lesson is real.

RInteractive R
# Simulate 30 clinics of 10 patients, dosed 0-10 mg, relief on a 0-100 scale library(ggplot2) set.seed(2) n_clinics <- 30 n_per_clinic <- 10 clinic <- factor(rep(1:n_clinics, each = n_per_clinic)) dose <- runif(n_clinics * n_per_clinic, 0, 10) u0 <- rnorm(n_clinics, mean = 0, sd = 4) u1 <- rnorm(n_clinics, mean = 0, sd = 1.2) relief <- 50 + u0[clinic] + (2 + u1[clinic]) * dose + rnorm(n_clinics * n_per_clinic, mean = 0, sd = 5) trial <- data.frame(clinic = clinic, dose = round(dose, 1), relief = round(relief, 1)) head(trial, 6) #> clinic dose relief #> 1 1 1.8 60.4 #> 2 1 7.0 56.6 #> 3 1 5.7 67.3 #> 4 1 1.7 48.0 #> 5 1 9.4 61.8 #> 6 1 9.4 63.4

  

trial has one row per patient: which clinic they belong to, the dose they got, and the relief score they reported. There are 300 rows in total, 10 for each of the 30 clinics.

Here is a first, plain look: every patient's relief against their dose, with no clinic information at all.

Relief clearly rises with dose. But is that the whole story? Not quite. Color each point by clinic instead, and a different picture emerges.

Now plot every patient, coloring each one by clinic. With thirty colors a legend would be unreadable, so leave it off and just look at the shape of the cloud.

RInteractive R
# Plot all 300 patients' relief against dose, colored faintly by clinic ggplot(trial, aes(x = dose, y = relief, color = clinic)) + geom_point(alpha = 0.6, size = 1.8) + guides(color = "none") + labs(x = "dose (mg)", y = "relief score")

  

Now the cloud has structure. Look closely and two things stand out. Some colors sit noticeably higher or lower than others even at the same dose, so clinics seem to differ in where they start. And some colors seem to climb faster across the dose range than others, so clinics might differ in how much a milligram actually buys.

Those are the two questions this lesson answers: do clinics genuinely differ in their average relief, and does a milligram of the drug buy the same relief in every clinic?

Why one pooled line fails every clinic

The simplest thing to do with this data is to ignore which clinic each patient came from, and fit one regression line across all 300 of them.

RInteractive R
# Fit one pooled regression line across all 300 patients, ignoring clinic fit_pooled <- lm(relief ~ dose, data = trial) summary(fit_pooled) #> #> Call: #> lm(formula = relief ~ dose, data = trial) #> #> Residuals: #> Min 1Q Median 3Q Max #> -22.947 -6.071 -0.060 5.745 31.789 #> #> Coefficients: #> Estimate Std. Error t value Pr(>|t|) #> (Intercept) 51.0143 0.9923 51.41 <2e-16 *** #> dose 1.7763 0.1714 10.36 <2e-16 *** #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 #> #> Residual standard error: 8.921 on 298 degrees of freedom #> Multiple R-squared: 0.2648, Adjusted R-squared: 0.2624 #> F-statistic: 107.3 on 1 and 298 DF, p-value: < 2.2e-16

  

This one line says every patient starts at about 51.0 points and gains about 1.78 points per milligram, no matter which clinic they are in. But the R-squared is only 0.2648, meaning dose alone, fit this way, explains barely a quarter of the spread in relief scores. The rest is sitting somewhere else, unaccounted for.

Draw that same line over the by-clinic scatter from the cover, and you can see exactly where it goes wrong.

RInteractive R
# Draw the pooled regression line over the by-clinic scatter ggplot(trial, aes(x = dose, y = relief)) + geom_point(aes(color = clinic), alpha = 0.6, size = 1.8) + guides(color = "none") + geom_abline(intercept = coef(fit_pooled)[1], slope = coef(fit_pooled)[2], linewidth = 1) + labs(x = "dose (mg)", y = "relief score")

  

One straight line cannot sit close to a clinic that starts high and close to a clinic that starts low at the same time. It cannot be flat for a clinic whose relief barely moves with dose and steep for a clinic whose relief climbs fast, either. The pooled line is a compromise that fits no single clinic particularly well, because clinic identity was never part of what it was fit on.

What you actually want is a model that gives every clinic its own intercept, and lets that intercept be estimated rather than guessed. That is what a random intercept is.