Week 12: Logistic Regression

Date: November 12, 2026

Today…

When the outcome is yes or no

  • Why lm() breaks when Y is 0 or 1
  • Probability → odds → log-odds (the one new idea today)
  • glm(..., family = binomial), and turning its output into odds ratios
  • Predicted probabilities: the way to explain a logistic model
  • 👻 Who believes in ghosts?

The plan for class

Lecture ~50 min
🚿 Activity ~20 min
☕ Break
📂 M2 work time the rest of class

M2 is due Sunday 11/15. Bring your questions.

library(tidyverse)
library(rio)
library(here)
library(broom)
library(easystats)

options(scipen = 999)

cah <- import(here("files", "data", "CAH.csv")) %>%
  mutate(across(where(is.character), ~ na_if(.x, "")))

The data: Cards Against Humanity’s Pulse of the Nation poll (2017). 1,000 U.S. adults, real survey questions, and some less serious ones.

Part 1: Why Not Just lm()?

A yes/no outcome

So far every outcome has been a number on a scale. Lots of outcomes in psychology are binary:

  • Diagnosed or not
  • Relapsed or not
  • Completed the study or dropped out
  • Answered the item correctly or not

Code it 1 = the thing happened and 0 = it didn’t. Then the mean of the 0s and 1s is the proportion of 1s, which is a probability.

What happens with lm()?

A made-up class of 200: hours studied and whether they passed the exam.

Code
set.seed(1112)
exam <- tibble(hours = runif(200, 0, 20),
               passed = rbinom(200, 1, plogis(-6 + 0.6 * hours)))

lpm <- lm(passed ~ hours, data = exam)
range(predict(lpm))
[1] -0.1555193  1.1928485
Code
ggplot(exam, aes(hours, passed)) +
  geom_jitter(width = 0, height = 0.03, alpha = 0.4) +
  geom_smooth(method = "lm", se = FALSE, color = "#C8102E") +
  geom_hline(yintercept = c(0, 1), linetype = "dashed") +
  labs(x = "Hours studied", y = "Passed (1) or not (0)") +
  theme_bw(base_size = 15)

Three problems

  1. Impossible predictions. A student who studied 0 hours has a “probability” of passing of −16%. Someone at 20 hours: 119%.
  2. The residuals can’t be normal. For any X, the residual can only take two values (1 − Ŷ or 0 − Ŷ).
  3. The variance isn’t constant. It’s largest near p = .5 and shrinks toward 0 and 1. (Week 10: heteroscedasticity, built in.)

We need a model whose predictions stay between 0 and 1 and bend near the edges.

The S-curve

Code
ggplot(exam, aes(hours, passed)) +
  geom_jitter(width = 0, height = 0.03, alpha = 0.4) +
  geom_smooth(method = "lm", se = FALSE, color = "gray60", linetype = "dashed") +
  geom_smooth(method = "glm", method.args = list(family = binomial),
              se = FALSE, color = "#C8102E", linewidth = 1.3) +
  labs(x = "Hours studied", y = "Probability of passing") +
  theme_bw(base_size = 15)

Same data. The red curve is a logistic regression: never below 0, never above 1, steepest in the middle.

Part 2: Probability, Odds, Log-Odds

Three ways to say the same thing

Probability p Odds = p / (1 − p) Log-odds = ln(odds)
.10 0.11 (1 to 9) −2.20
.25 0.33 (1 to 3) −1.10
.50 1 (even) 0
.75 3 (3 to 1) 1.10
.90 9 (9 to 1) 2.20
  • Probability is stuck between 0 and 1.
  • Odds run from 0 to ∞.
  • Log-odds run from −∞ to +∞, like any regression outcome.

So logistic regression fits a straight line on the log-odds scale:

\[ \ln\left(\frac{p}{1-p}\right) = b_0 + b_1 X_1 + b_2 X_2 + \cdots \]

A room of 10 people 👻

Ten people answer “Do you believe in ghosts?”

Room A

👻👻👻👻👻👻👻👻🙅🙅

Probability 8 of 10 = .80
Odds 8 believers to 2 = 4
Log-odds ln(4) = +1.39

Room B

🙅🙅🙅🙅🙅🙅🙅🙅👻👻

Probability 2 of 10 = .20
Odds 2 believers to 8 = 0.25
Log-odds ln(0.25) = −1.39
  • Odds are a head count: “4 believers for every non-believer.”
  • Room B is Room A flipped, but the odds (4 vs. 0.25) don’t look like mirror images.
  • The log-odds do: +1.39 vs. −1.39. Zero is the 5-and-5 room.

Why bother with the log?

Because adding on the log-odds scale is multiplying the odds.

Log-odds Odds Probability
0 1 .50
0 + 0.69 → 1 × 2 = 2 → .67
0.69 + 0.69 → 2 × 2 = 4 → .80
1.39 + 0.69 → 4 × 2 = 8 → .89
  • A regression line adds a fixed amount for each unit of X. That’s the log-odds column.
  • exp(0.69) = 2, so each step doubles the odds. That’s the odds ratio.
  • The probability goes up by less and less each step: +.17, then +.13, then +.09. It can’t pass 1.

Important

One coefficient, three readings: +0.69 in log-odds = × 2 in odds = “it depends where you start” in probability.

Turn the dial 🎛️

Try 0, then ±1.39 (the two rooms), then ±4. Each step of 1 near the middle moves the probability a lot; out near the edges it barely moves.

The catch

Coefficients come out in log-odds, and nobody thinks in log-odds.

So we translate:

Scale How to get there Read it as
Log-odds what glm() gives you sign and p-value only
Odds ratio exp(b) “the odds are multiplied by ___”
Probability predict(..., type = "response") “a ___ has a __% chance”

Important

Odds ratio = 1 means no effect. Above 1: higher odds. Below 1: lower odds. An OR of 0.40 means the odds are 60% lower, not that the probability is.

Part 3: Who Believes in Ghosts? 👻

The question

“Do you believe in ghosts?” (Yes / No)

ghosts <- cah %>%
  filter(gender %in% c("Female", "Male"), !is.na(ghosts)) %>%
  mutate(ghost  = if_else(ghosts == "Yes", 1, 0),          # 1 = believes
         gender = factor(gender, levels = c("Female", "Male"),
                         labels = c("Woman", "Man")))

ghosts %>% summarise(n = n(), prop_believe = mean(ghost))
    n prop_believe
1 946    0.3773784

About 38% believe. (I dropped the 28 people who answered “Other” for gender. With n = 28, the estimate would be very noisy. In your own work, make that decision before you see the results, and report it.)

glm(): one new argument

m_age <- glm(ghost ~ age, data = ghosts, family = binomial)
tidy(m_age)
# A tibble: 2 × 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)   0.0292   0.211       0.139 0.890  
2 age          -0.0109   0.00412    -2.63  0.00842
  • family = binomial is what makes it logistic. Leave it off and you’ve fit lm() again.
  • The age coefficient is −0.011 in log-odds: older people are less likely to believe, p = .008.
  • What does −0.011 log-odds per year mean? Nothing intuitive. Translate.

Odds ratio for age

model_parameters(m_age, exponentiate = TRUE)
Parameter   | Odds Ratio |       SE |       95% CI |     z |     p
------------------------------------------------------------------
(Intercept) |       1.03 |     0.22 | [0.68, 1.56] |  0.14 | 0.890
age         |       0.99 | 4.08e-03 | [0.98, 1.00] | -2.63 | 0.008

OR = 0.989 per year: each year of age multiplies the odds of believing by 0.989, about 1% lower odds per year.

Per year is tiny. Per decade: exp(10 × −0.0109) = 0.90, so 10% lower odds for every 10 years older.

A categorical predictor: gender

Before the model, the 2 × 2 table:

ghosts %>%
  group_by(gender) %>%
  summarise(n = n(), believe = sum(ghost), p = mean(ghost)) %>%
  mutate(odds = p / (1 - p))
# A tibble: 2 × 5
  gender     n believe     p  odds
  <fct>  <int>   <dbl> <dbl> <dbl>
1 Woman    476     232 0.487 0.951
2 Man      470     125 0.266 0.362

Odds ratio by hand: 0.362 / 0.951 = 0.38. Men have about 38% of the odds that women do.

m_gender <- glm(ghost ~ gender, data = ghosts, family = binomial)
model_parameters(m_gender, exponentiate = TRUE)
Parameter    | Odds Ratio |   SE |       95% CI |     z |      p
----------------------------------------------------------------
(Intercept)  |       0.95 | 0.09 | [0.79, 1.14] | -0.55 | 0.582 
gender [Man] |       0.38 | 0.05 | [0.29, 0.50] | -6.94 | < .001

Same number. The odds ratio is just a ratio of two groups’ odds. The intercept (0.95) is the reference group’s odds: women.

Both predictors

m_both <- glm(ghost ~ age + gender, data = ghosts, family = binomial)
model_parameters(m_both, exponentiate = TRUE)
Parameter    | Odds Ratio |       SE |       95% CI |     z |      p
--------------------------------------------------------------------
(Intercept)  |       1.88 |     0.44 | [1.19, 2.98] |  2.70 | 0.007 
age          |       0.99 | 4.23e-03 | [0.98, 0.99] | -3.17 | 0.002 
gender [Man] |       0.37 |     0.05 | [0.28, 0.48] | -7.14 | < .001

Same “holding constant” logic as multiple regression:

  • Age: holding gender constant, each year multiplies the odds of believing by 0.99.
  • Gender: holding age constant, men have 0.37 times the odds of believing that women do (63% lower odds).

Follow one person through the model

A 25-year-old woman. Plug her into the model, one scale at a time:

Step Woman, 25 Man, 25
Log-odds = b0 + bage × 25 (+ bman) 0.630 + (-0.0136 × 25) = 0.29 0.29 + (-1.00) = -0.71
Odds = elog-odds e^0.29^ = 1.34 e^-0.71^ = 0.49
Probability = odds / (1 + odds) 1.34 / 2.34 = .57 0.49 / 1.49 = .33
  • On the log-odds scale, being a man subtracts 1.00.
  • On the odds scale, it multiplies by 0.37: 1.34 × 0.37 = 0.49. That’s the odds ratio.
  • On the probability scale, it’s .57 → .33. That is not .57 × 0.37.

plogis() does the last two steps in one go: plogis(0.29) = 0.57.

Same odds ratio, different probability change

Men have 0.37 times the odds. What that does to the probability depends on where you start:

Woman’s probability Same-age man (× OR on the odds) Change (percentage points)
.05 .02 -3
.20 .08 -12
.50 .27 -23
.80 .59 -21
.95 .87 -8

The odds ratio is the same in every row. The change in probability is biggest near .50 and tiny near 0 or 1, because that’s where the S-curve is steepest.

Tip

This is why a Results section reports the odds ratio (one number that’s true everywhere), and the Discussion uses predicted probabilities at values of X that matter.

The best way to explain it: predicted probabilities

Odds ratios are for the Results section. Probabilities are for humans.

new_people <- expand_grid(age = c(25, 65), gender = c("Woman", "Man"))

new_people %>%
  mutate(p_believe = predict(m_both, newdata = new_people, type = "response")) %>%
  mutate(p_believe = round(p_believe, 2))
# A tibble: 4 × 3
    age gender p_believe
  <dbl> <chr>      <dbl>
1    25 Woman       0.57
2    25 Man         0.33
3    65 Woman       0.44
4    65 Man         0.22

A 25-year-old woman: 57%. A 65-year-old man: 22%.

Warning

type = "response" matters. Leave it off and predict() returns log-odds.

Plot the probabilities

augment(m_both, type.predict = "response") %>%
  ggplot(aes(age, .fitted, color = gender)) +
  geom_line(linewidth = 1.3) +
  labs(x = "Age", y = "Predicted probability of believing in ghosts", color = NULL) +
  ylim(0, 1) +
  theme_bw(base_size = 15)

The lines look almost straight here because the probabilities stay in the middle of the range. The S only shows up near 0 and 1.

Did gender earn its place?

Last week’s block comparison works here too, with one change:

anova(m_age, m_both, test = "Chisq")
Analysis of Deviance Table

Model 1: ghost ~ age
Model 2: ghost ~ age + gender
  Resid. Df Resid. Dev Df Deviance           Pr(>Chi)    
1       944     1246.9                                   
2       943     1193.8  1   53.136 0.0000000000003113 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Logistic regression has no F test. It compares deviance (the logistic version of leftover error) with a χ² test. Adding gender reduced deviance by 53.1, χ²(1) = 53.14, p < .001.

r2(m_both)
# R2 for Logistic Regression
  Tjur's R2: 0.063

There’s no true R² either. Tjur’s R² = .063: the gap between the average predicted probability for believers and for non-believers. Small, so most of what decides who believes in ghosts isn’t in this model.

Writing it up

We used logistic regression to predict belief in ghosts (1 = yes) from age and gender (N = 946; 37.7% believed). Holding age constant, men had lower odds of believing than women, OR = 0.37, 95% CI [0.28, 0.48], p < .001. Each additional year of age was associated with slightly lower odds, OR = 0.987, 95% CI [0.978, 0.995], p = .002. For example, the model predicts a 57% probability of believing for a 25-year-old woman and 22% for a 65-year-old man.

Tip

Report the odds ratio with its CI, then give at least one predicted probability so readers can tell how big the effect is.

Three ways to get it wrong

  1. Not checking which level is 1. If R treats “No” as the outcome, every odds ratio flips. Always create the 0/1 variable yourself and say what 1 means.
  2. Reading log-odds as probabilities. “−0.011” doesn’t mean 1.1 percentage points. Exponentiate, or predict.
  3. Treating an odds ratio as a risk ratio. OR = 0.37 doesn’t mean men are 37% as likely to believe. Their probability is 27% vs. 49%, a ratio of 0.55. Odds ratios exaggerate when the outcome is common.

Logistic vs. linear, side by side

Linear Logistic
Outcome Continuous Binary (0/1)
R lm(y ~ x) glm(y ~ x, family = binomial)
Coefficient Change in Y Change in log-odds
Easier to read b itself exp(b) = odds ratio
Compare models anova() → F anova(test = "Chisq") → χ²
Fit R² Tjur’s R² (or another pseudo-R²)
Explain it with Predicted Y Predicted probability

💻 Your Turn: 🚿 Is It Acceptable?

Tip

~20 min · Pairs · Same CAH data

“Is it acceptable to pee in the shower?”

Who says yes? Age, gender, and a predicted probability for two people.

Open the activity

☕ Break

📂 M2 Work Time

Due Sunday 11/15. Use the rest of class. I’ll come around.

M2 checklist (from Week 11)

Is your outcome binary? Then today’s model is your model. Ask me about it while you’re here.

For next time

📂 M2, due Sunday 11/15

📋 Lab 11: Logistic Regression, due Tuesday 11/17 at 11:59pm

📖 Readings for Mixed/Multilevel Models:

  • Chapter 8.8 — MSR
  • Chapter 15.8 — ST

💾 Class Check next week