Unit 02.04: R picks your reference alphabetically
R picks your reference category alphabetically. It has no idea which group is the control.
Every coefficient is a comparison against the reference
When a factor enters a model, R codes it against a reference level and reports every other level as a difference from that reference. Which level is the reference is therefore not cosmetic — it determines what each coefficient means.
The default is the alphabetically first level. That is arbitrary, and it is frequently wrong: dose_high sorts before placebo, so a model of a dose trial silently compares everything against the highest dose.
relevel() sets the reference explicitly. The model fit does not change at all — the same predictions, the same R-squared — only which comparisons are reported.
This block fits the same model before and after setting the reference.
suppressPackageStartupMessages({library(dplyr); library(broom)})
set.seed(3)
d <- data.frame(
arm = factor(rep(c("placebo", "dose_low", "dose_high"), each = 25)),
y = c(rnorm(25, 50, 6), rnorm(25, 53, 6), rnorm(25, 58, 6))
)
cat("Default level order (alphabetical):", paste(levels(d$arm), collapse = ", "), "\n")
m1 <- lm(y ~ arm, data = d)
print(tidy(m1)[, c("term", "estimate", "p.value")])
cat("R took the alphabetically first level, dose_high, as the reference,\n")
cat("so every estimate above is a comparison against the HIGHEST dose.\n\n")
d$arm <- relevel(d$arm, ref = "placebo")
cat("After relevel:", paste(levels(d$arm), collapse = ", "), "\n")
m2 <- lm(y ~ arm, data = d)
print(tidy(m2)[, c("term", "estimate", "p.value")])
cat("\nSame data, same model fit -- R-squared is identical at",
round(summary(m1)$r.squared, 4), "and", round(summary(m2)$r.squared, 4), ".\n")
cat("Only the comparisons changed, and only the second set answers the question.\n")
The default order is dose_high, dose_low, placebo, so R takes dose_high as the reference and reports placebo as -10.0 — a comparison against the highest dose, which nobody asked for. After relevelling to placebo, the same model reports dose_high as +10.0 and dose_low as +5.70, which are the trial's actual questions. R-squared is 0.3932 both times: the fit is identical and only the reporting changed.
The mistake this prevents
The mistake is interpreting coefficients without checking the reference level. The signs come out backwards, the sentence gets written anyway, and nothing in the output looks wrong.
Takeaway
Convert grouping variables to factors and set the reference level explicitly with relevel(). State the reference in the results table, because every coefficient is meaningless without it.
