23  Categorical and Dichotomous Predictors

NoteWhat you’ll be able to do

By the end of this chapter you will be able to:

  1. Explain how regression handles categorical predictors through dummy coding.
  2. Read dummy-coded output: what the intercept and each coefficient mean — each level compared to the reference, not to all others.
  3. Identify and change the reference level R chose.
  4. Verify that regression with a dichotomous predictor and an independent t-test give the same answer.

Regression draws lines through continuous predictors — so what is it supposed to do with “control vs. treatment,” which isn’t a number at all? Make it one. Code control as 0 and treatment as 1, and the slope becomes the difference between the group means. That single trick turns regression and the t-test into the same machine — and it scales to any number of groups.

In Chapter 22 the predictor was continuous. But regression also handles categorical predictors — group membership variables. This chapter shows how, and why it matters.

The key insight: regression with a dichotomous predictor is the same test as an independent-samples t-test, and regression with a categorical predictor with 3+ levels is the same test as one-way ANOVA. They produce identical p-values. They differ only in the form of the output.

Once you see this equivalence, the whole landscape of Part IV and Part V collapses into one general model.

23.1 Dichotomous predictors

A dichotomous predictor has exactly two levels — male/female, control/treatment, smoke/no-smoke. To use one in regression, you code it as a 0 or 1 indicator variable.

Coding Meaning
0 Reference group (“control” or baseline)
1 Comparison group (“treatment”)

R does this automatically when you pass a factor as a predictor — it picks one level as the reference (alphabetically first by default) and codes the others as 0/1.

23.2 How the slope works with a dichotomous predictor

When X is coded 0/1, the regression model:

\[ Y = \beta_0 + \beta_1 X + \epsilon \]

becomes very interpretable:

  • \(\beta_0\) (intercept) = mean of the reference group (\(X = 0\)).
  • \(\beta_1\) (slope) = difference in means between the two groups.

So the t-test on the slope is testing whether the two group means differ — exactly the same question as an independent t-test.

23.2.1 R syntax

# Make sure the variable is a factor
data$group <- as.factor(data$group)

# Fit regression
model <- lm(score ~ group, data = data)
summary(model)

23.2.2 Worked example

Use the teaching-methods data from Chapter 15: 12 students taught with method A and 14 with method B. Method A is the reference level (R picks the first level alphabetically), so it plays the “control” role.

data <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/ch15_teaching_methods.csv")
data$method <- as.factor(data$method)
model <- lm(score ~ method, data = data)
summary(model)

Output (coefficients section):

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   16.583      1.802   9.202 2.43e-09 ***
methodB        6.631      2.456   2.700   0.0125 *  

Interpretation:

  • Intercept = 16.58 — mean score in the reference group (method A).
  • methodB = 6.63 — method B students scored 6.63 points higher than method A students on average (23.21 − 16.58).
  • t(24) = 2.70, p = .0125 — identical to the pooled-variance (Student’s) independent-samples t-test on the same data. It is not the Welch result from Chapter 15 (t(16.04) = −2.88, p = .011).

You can verify the equivalence yourself — but only against Student’s t-test, which assumes equal variances (the same assumption ordinary regression makes):

# Regression
summary(lm(score ~ method, data = data))

# Student's t-test — var.equal = TRUE matches the regression exactly
t.test(score ~ method, data = data, var.equal = TRUE)
# t = -2.7001, df = 24, p-value = 0.0125  (sign flips: t.test computes A minus B)

# R's default (Welch's t-test) does NOT match — it doesn't assume equal variances
t.test(score ~ method, data = data)
# t = -2.8817, df = 16.036, p-value = 0.01083

Regression’s p-value is identical to Student’s t-test (var.equal = TRUE) because both assume equal variances. Welch’s t-test — R’s default, and the course default — makes a different assumption and will usually give a different p-value; the two converge only when the groups’ variances are actually equal.

NoteWhy this matters

Once you understand that an independent t-test is just a regression with a dichotomous predictor, you stop thinking of statistics as a long list of separate tests. There’s one general framework — the linear model — and t-tests, ANOVA, and regression are all special cases of it. The variety in Part IV is mostly variety in output format, not in underlying math.

This is why graduate-level statistics courses sometimes skip teaching individual tests and just teach the linear model directly: students who get it never have to memorize a long list of tests, because they can derive whichever they need.

23.3 Categorical predictors with 3+ levels

When a categorical predictor has more than two levels — say teaching method (A, B, C) — R needs to encode it as multiple 0/1 indicator variables. This is called dummy coding (or treatment coding, R’s default).

With k levels, R creates k − 1 dummy variables. One level is the reference, and each dummy represents the contrast between one other level and the reference.

For three teaching methods A, B, C (with A as the reference):

Method dummyB dummyC
A 0 0
B 1 0
C 0 1

The regression equation becomes:

\[ Y = \beta_0 + \beta_1 \cdot \text{dummyB} + \beta_2 \cdot \text{dummyC} + \epsilon \]

with:

  • \(\beta_0\) — mean of method A (reference).
  • \(\beta_1\) — difference in means between B and A.
  • \(\beta_2\) — difference in means between C and A.

23.3.1 R syntax (same as before)

Using the three-method data from Chapter 17 (10 students per method):

data <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/ch17_teaching_methods.csv")
data$method <- as.factor(data$method)
model <- lm(score ~ method, data = data)
summary(model)

R handles the dummy coding internally; you don’t need to create the dummy variables yourself.

23.3.2 Reading the output

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   68.200      1.822  37.436  < 2e-16 ***
methodB        3.100      2.576   1.203  0.23933    
methodC        8.400      2.576   3.260  0.00301 ** 
  • Intercept = 68.20 — mean of method A.
  • methodB = 3.10, p = .239 — method B is 3.10 points higher than A, not a significant difference.
  • methodC = 8.40, p = .003 — method C is 8.40 points higher than A.

These unadjusted p-values are not the Tukey-adjusted ones from Chapter 17 (C vs. A there: p = .008) — dummy-coefficient tests make no multiple-comparison correction.

ImportantCommon Misconception: dummy coefficients compare each group to all others

Each dummy coefficient is a contrast between that level and the reference group only — not between that level and the average of all groups, and not between that level and every other group. In a three-group model (A, B, C with A as reference), methodB tells you how B differs from A; it says nothing directly about B vs. C. To get the B-vs-C comparison, you need emmeans() or TukeyHSD().

What’s missing: the B-vs-C contrast. Dummy coding gives you each non-reference level vs. the reference. To get all pairwise comparisons, use Tukey HSD or emmeans:

library(emmeans)
emmeans(model, pairwise ~ method, adjust = "tukey")

23.3.3 The equivalence with one-way ANOVA

# Regression approach
model_lm <- lm(score ~ method, data = data)
anova(model_lm)             # produces the ANOVA table

# Classical ANOVA approach
model_aov <- aov(score ~ method, data = data)
summary(model_aov)

These give identical F-tests. The regression summary() gives per-coefficient t-tests too; the ANOVA summary() gives just the omnibus F-test.

23.4 Choosing a reference level

By default R picks the alphabetically first level as the reference. To pick a different one (relevel() only works on a factor, so convert with as.factor() first):

# Make "treatment" the reference
data$group <- relevel(data$group, ref = "treatment")

# Or with the factor() constructor
data$group <- factor(data$group, levels = c("treatment", "control"))

This matters when you want the contrasts to be interpreted relative to a meaningful baseline (a true “control” or the “standard” condition).

23.5 Mixing categorical and continuous predictors

You can combine them freely:

model <- lm(score ~ method + study_hours, data = data)

This fits a model where:

  • Each method has its own intercept.
  • All methods share the same slope for study_hours.

This additive, parallel-slopes model — one categorical factor plus a continuous covariate — is the regression equivalent of ANCOVA (analysis of covariance). Standard ANCOVA assumes the groups share a common slope, which is exactly what method + study_hours encodes.

If you want each method to have its own slope too, add an interaction:

model <- lm(score ~ method * study_hours, data = data)

This relaxes the equal-slopes assumption (sometimes called ANCOVA with heterogeneous slopes); it is not standard ANCOVA. A significant interaction is itself a warning that the simple parallel-slopes ANCOVA does not fit the data.

23.6 Reporting

For a dichotomous predictor:

A linear regression indicated that students taught with method B scored significantly higher than students taught with method A, b = 6.63, 95% CI [1.56, 11.70], t(24) = 2.70, p = .013, \(R^2\) = .23.

For a categorical predictor with 3+ levels, prefer the ANOVA-style write-up (since dummies-vs-reference is rarely the substantive question you care about):

A regression of score on teaching method indicated a significant effect, F(2, 27) = 5.44, p = .010, \(R^2\) = .29. Tukey HSD comparisons indicated that method C (M = 76.60) scored higher than method A (M = 68.20; p = .008); method B (M = 71.30) did not differ significantly from either A (p = .462) or C (p = .118).

23.7 Common mistakes

Wrong Right
Groups coded as numbers (1, 2, 3) lm() fits one slope instead of group differences; convert with as.factor()
Confusing the reference level Use relevel() or factor(..., levels = ...) to set it explicitly
Reporting only the dummy coefficients for a 3-level predictor Report the F-test (or include all pairwise comparisons)
Interpreting methodB as the “effect of method B” It’s the contrast between B and the reference (usually A)
Forgetting to use Tukey for pairwise comparisons Use emmeans() or TukeyHSD() for multiple comparisons

23.8 Quick reference

Element R code
Dichotomous predictor lm(y ~ group, data = data) (group as factor)
Categorical predictor (3+ levels) lm(y ~ method, data = data)
Change reference level data$group <- relevel(data$group, ref = "control")
Pairwise comparisons library(emmeans); emmeans(model, pairwise ~ method, adjust = "tukey")
ANOVA-style omnibus test anova(model)
ANCOVA (parallel slopes) lm(y ~ method + x, data = data)
Heterogeneous slopes (not standard ANCOVA) lm(y ~ method * x, data = data)
TipThe big picture

When you understand this chapter, three rows of Table 2.2 become one row:

What you thought you had What it really is
Independent t-test Regression with one dichotomous predictor
One-way ANOVA Regression with one categorical predictor (3+ levels)
Two-way ANOVA Regression with two categorical predictors (and possibly their interaction)

All produced by lm(). All interpretable through the same lens. The “linear model” is a single general framework that subsumes most of what Part IV taught as separate tests.

NoteInterpret the Output

Read this output carefully. The R code that produced it is not shown. There are three conditions (A, B, C).

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   44.655      2.283  19.562  < 2e-16 ***
conditionB     9.285      3.228   2.876  0.00565 ** 
conditionC     3.045      3.228   0.943  0.34954    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.21 on 57 degrees of freedom
Multiple R-squared:  0.1311,    Adjusted R-squared:  0.1006 
F-statistic: 4.299 on 2 and 57 DF,  p-value: 0.01823
  1. How many conditions are there? Which is the reference group, and how do you know?
  2. What does the intercept (44.655) represent in plain language?
  3. Interpret the coefficient for conditionB in one sentence.
  4. Is condition C significantly different from condition A? How do you know?
  5. Is condition B significantly different from condition C? How do you know — and what would you need to do to find out?
  6. A classmate says “condition B significantly outperforms both A and C.” What is wrong with this conclusion?
  7. Write one APA 7 sentence for the overall model.

TipChapter Summary

Core idea: Regression handles categorical predictors through dummy coding — k − 1 indicator (0/1) variables, each contrasting one level against the reference. The result is mathematically identical to a t-test (two groups) or one-way ANOVA (3+ groups).

You should understand:

  • What the intercept represents when the predictor is categorical (mean of the reference group)
  • What each dummy coefficient represents (contrast vs. reference, not vs. all other groups)
  • Why k − 1 dummies are needed for a k-level factor
  • How to get pairwise comparisons not shown in the dummy output

You should be able to do in R:

  • Fit: lm(y ~ group, data = data) (group as factor)
  • Change reference: relevel(group, ref = "control")
  • All pairwise comparisons: library(emmeans); emmeans(model, pairwise ~ group, adjust = "tukey")
  • ANOVA-style omnibus: anova(model)

Plain-language check: A model with condition (A/B/C, A = reference) gives conditionB = 6.2, p = .003 and conditionC = 1.4, p = .41. Which groups differ significantly from A? Which comparison is missing from this output?

The one mistake to avoid: Concluding that a non-significant dummy coefficient means “that group is the same as all others.” It only means that group is not significantly different from the reference. You cannot infer anything about comparisons not explicitly tested.

Key terms from this chapter are defined in the Glossary.

TipReporting checklist for regression with categorical predictors
NoteCategorical Predictors Practice

Practice the skills from this chapter using real data: Categorical Predictors in Regression: Practice.

Next: when you have more than one predictor.