Logistic Regression






Grayson White

Stat 243
Fall 2026, Week 8

Setting: Springfield, Illinois. April 1846.

  • Two brothers, George and Jacob decide to leave Illinois and lead a group of migrants to head west to California.

  • Expected trip duration: four to six months

  • Slowed by taking a new route, the Hastings Cutoff, which crossed the Wasatch mountains and Great Salt Lake Desert.

Setting: Springfield, Illinois. April 1846.

  • Two brothers, George and Jacob decide to leave Illinois and lead a group of migrants to head west to California.

  • Expected trip duration: four to six months

  • Slowed by taking a new route, the Hastings Cutoff, which crossed the Wasatch mountains and Great Salt Lake Desert.

  • In early November, the “Donner Party” is trapped by an early snowfall in the Sierra Nevada mountains.

Data: Donner Party

library(tidyverse)
library(Sleuth3)
data(case2001)
donner <- case2001 %>%
  as_tibble() %>%
  mutate(Survived = case_when(
    Status == "Survived" ~ 1,
    Status == "Died" ~ 0
  ))
donner
# A tibble: 45 × 4
     Age Sex    Status   Survived
   <int> <fct>  <fct>       <dbl>
 1    23 Male   Died            0
 2    40 Female Survived        1
 3    40 Male   Survived        1
 4    30 Male   Died            0
 5    28 Male   Died            0
 6    40 Male   Died            0
 7    45 Female Died            0
 8    62 Male   Died            0
 9    65 Male   Died            0
10    45 Female Died            0
# ℹ 35 more rows
  • Q: What does a row represent?
    • A: Each row represents a member of the Donner Party!

We’d like to describe the relationship between survival in the Donner Party and other recorded variables: Sex and Age

Exploratory data analysis

ggplot(data = donner, 
       mapping = aes(x = Sex, fill = Status)) +
  geom_bar(position = "fill")

  • What can we learn from this graph?

Exploratory data analysis

ggplot(data = donner, 
       mapping = aes(x = Age, y = Status, color = Sex)) + 
  geom_boxplot(color = "black", alpha = 0.5) + 
  geom_point(alpha = 0.5,
             size = 3)

  • What about this one?

Can we use a model to estimate the probability of survival given these predictors?

Recall: Linear Regression

Model Form:

\[ \begin{align} y &= \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p + \epsilon \end{align} \]

Linear regression is a flexible class of models that allow for:

  • Both quantitative and categorical explanatory variables.

  • Multiple explanatory variables.

  • BUT the response variable is quantitative.

Linear Regression for predicting probability of survival

Model Form:

\[ \begin{align} y = P(\text{survival}) = \beta_0 + \beta_1 x_{age} + \beta_2 x_{sex} + \epsilon \end{align} \]

library(moderndive)
ggplot(data = donner, 
       mapping = aes(x = Age, 
                     y = Survived, 
                     color = Sex)) +
  geom_point(alpha = 0.5,
             size = 3) + 
  geom_parallel_slopes(method = "lm", se = FALSE) + 
  labs(y = "P(Survival)") + 
  theme(legend.position = "bottom")

Linear Regression for predicting probability of survival

library(moderndive)
ggplot(data = donner, 
       mapping = aes(x = Age, 
                     y = Survived, 
                     color = Sex)) +
  geom_point(alpha = 0.5,
             size = 3) + 
  geom_parallel_slopes(method = "lm", se = FALSE) + 
  labs(y = "P(Survival)") + 
  theme(legend.position = "bottom")

Think-Pair-Share:

  1. What do the regression lines represent?

  2. What is the estimated probability of survival for 20 year old female members of the Donner Party?

  3. What about the estimated probability of survival for 65 year old male members?

02:00

Linear Regression

  1. Q: What do the regression lines represent?
    • A: Estimated probability of survival for male (blue) and female (salmon) members of the Donner Party.
  1. What is the estimated probability of survival for 20 year old female members of the Donner Party?

  2. What about the estimated probability of survival for 65 year old male members?

library(broom)
linear_mod <- lm(Survived ~ Age + Sex, data = donner)
tidy(linear_mod)
# A tibble: 3 × 5
  term        estimate std.error statistic    p.value
  <chr>          <dbl>     <dbl>     <dbl>      <dbl>
1 (Intercept)   1.07     0.209        5.10 0.00000773
2 Age          -0.0128   0.00554     -2.32 0.0253    
3 SexMale      -0.319    0.145       -2.20 0.0337    
predict(linear_mod, 
        data.frame(Age = c(20, 65),
                   Sex = c("Female", "Male")))
          1           2 
 0.80885450 -0.08851912 

Linear Regression for binary response data

Logistic Regression for binary response data

Logistic Regression

Logistic regression can be used when:

Response variable: A categorical variable with two categories

\[\begin{align*} Y = \left\{ \begin{array}{ll} 1 & \mbox{success} \\ 0 & \mbox{failure} \\ \end{array} \right. \end{align*}\]

\(Y \sim\) Bernoulli\((p)\) where \(p = P(Y = 1) = P(\mbox{success})\).

Explanatory variables: Can be a mix of categorical and quantitative

Goal: Build a model for \(P(Y = 1)\).

Why can’t we use linear regression?

  • Regression line = estimated probability of success

  • For valid values of \(x\), we are predicting the probability is less than 0 or greater than 1.

Why can’t we use linear regression?

  • Regression line = estimated probability of success

  • For valid values of \(x\), we are predicting the probability is less than 0 or greater than 1.

  • The estimated probabilities based on the logistic regression model are bounded between 0 and 1.

What does the logistic regression model look like?

Logistic Regression Model

\[\begin{align*} \log\left(\frac{P(Y = 1)}{1 - P(Y = 1)} \right) &= \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p \end{align*}\]

Left hand side has many interpretations:

\[\begin{align*} \mbox{LHS} &= \log\left(\frac{P(Y = 1)}{1 - P(Y = 1)} \right)\\ &= \log \left( \mbox{odds (of success)} \right)\\ &= \mbox{logit}(P(Y = 1)) \end{align*}\]

Note:

\[ \mbox{odds} = \frac{\mbox{prob of success}}{\mbox{prob of failure}} \]

Question: How do we fit the model (the red curve) in R?

Logistic Regression Model in R

Instead of a linear regression model, let’s fit a logistic regression model to the Donner Party data!

library(broom)
mod <- glm(Survived ~ Sex, data = donner, family = "binomial")
tidy(mod)
# A tibble: 2 × 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)    0.693     0.548      1.27  0.206 
2 SexMale       -1.39      0.671     -2.07  0.0388

Fitted model: \[ \log\left(\frac{P(Y = 1)}{1 - P(Y = 1)} \right) = 0.693 - 1.386 x_{Sex:Male} \]

  • But what do the coefficient estimates \(\hat{\beta}_0 = 0.693\) and \(\hat{\beta}_1 = - 1.386\) represent?

Interpretation of Coefficients

What do the coefficient estimates \(\hat{\beta}_0 = 0.693\) and \(\hat{\beta}_1 = - 1.386\) represent?

When Sex = “Female”:

\[\begin{align*} \log\left(\frac{P(Y = 1)}{1 - P(Y = 1)} \right) &= \log (\text{odds}) \\ & = 0.693 \\ &= \hat\beta_0 \end{align*}\]

Therefore, \(\hat\beta_0\) represents the \(\log\) of the odds of survival for the baseline group.

When Sex = “Male”:

\[\begin{align*} \log\left(\frac{P(Y = 1)}{1 - P(Y = 1)} \right) &= \log (\text{odds}) \\ &= 0.693 - 1.386 \\ &= \hat\beta_0 + \hat\beta_1 \end{align*}\]

Therefore, \(\hat\beta_1\) represents the change in the \(\log\) odds of survival when comparing the baseline group (Female) to Male.

Is there a more intuitive way to interpret logistic regression coefficients?

Interpretation of Coefficients

Recall the (2) following definitions:

(1) Odds:

\[\text{odds} = \frac{\text{prob of success}}{\text{prob of failure}} = \frac{P(Y = 1)}{1 - P(Y = 1)}\]

and (2) the logistic regression model:

\[\log\left(\frac{P(Y = 1)}{1 - P(Y = 1)}\right) = \beta_0 + \beta_1 x_1\]

So then

\[\text{odds} = \exp\left(\beta_0 + \beta_1 x_1\right)\]

Interpretation of Coefficients

(Estimated) odds of survival for female members of the Donner Party:

\[\exp\left(\hat{\beta}_0\right)\]

exp(0.693)
[1] 1.999706

(Estimated) odds of survival for male members of the Donner Party:

\[\exp\left(\hat{\beta}_0 + \hat{\beta}_1\right)\]

exp(0.693 - 1.386)
[1] 0.5000736

What about interpreting \(\hat\beta_1\) on it’s own?

Interpretation of Coefficients

New concept:

Odds ratio: Comparison of odds between two groups

\[\text{odds ratio} = \frac{\text{odds of group 1}}{\text{odds of group 2}}\]

Interpretation of odds ratio: The odds of success in group 1 are ___ times the odds of success in group 2.


Note: Also useful property:

\[\exp(a + b) = \exp(a) \exp(b)\]

Interpretation of Coefficients

Computing the odds ratio:

\[\frac{\exp\left(\hat{\beta}_0 + \hat{\beta}_1 \right)}{\exp\left(\hat{\beta}_0 \right)} = \frac{\exp(\hat{\beta}_0)\exp(\hat{\beta}_1 )}{\exp(\hat{\beta}_0)} = \exp(\hat{\beta}_1)\]

Interpretation of Coefficients

How do we interpret \(\hat{\beta}_1 = - 1.386\)?

\[\exp(\hat{\beta}_1) = \frac{\text{odds of survival if male} }{\text{odds of survival if female }}\]


exp(-1.386)
[1] 0.2500736
1 / exp(-1.386)
[1] 3.998823

The estimated odds of survival for female members of the Donner Party are about 4 times that of male members!

What about quantitative predictors?

A new model

library(broom)
mod2 <- glm(Survived ~ Sex + Age, data = donner, family = "binomial")
tidy(mod2)
# A tibble: 3 × 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)   3.23      1.39        2.33  0.0198
2 SexMale      -1.60      0.755      -2.11  0.0345
3 Age          -0.0782    0.0373     -2.10  0.0359
  • Interpretation of \(\hat\beta_1 = -1.597\)?
1 / exp(-1.597)
[1] 4.938196

The estimated odds of survival for female members of the Donner Party is nearly 5 times that of male members, holding age constant.

  • Interpretation of \(\hat\beta_2 = -0.078\)?

Interpretation of Coefficients

(Estimated) odds of survival for \(x_{age} = t\) years:

\[\exp\left(\hat{\beta}_0 + \hat{\beta}_1 x_{1} + \hat{\beta}_2 t\right)\]

(Estimated) odds of survival for \(x_{age} = t + 1\) years:

\[\exp\left(\hat{\beta}_0 + \hat{\beta}_1 x_1 + \hat{\beta}_2 (t + 1)\right)\]

odds ratio:

\[\frac{\exp\left(\hat{\beta}_0 + \hat{\beta}_1 x_{1} + \hat{\beta}_2 (t + 1)\right)}{\exp\left(\hat{\beta}_0 + \hat{\beta}_1 x_{1} + \hat{\beta}_2 t\right)} = \frac{\exp\left(\hat{\beta}_0\right)\exp\left(\hat{\beta}_1 x_{1}\right) \exp\left(\hat\beta_2 (t + 1)\right)}{\exp\left(\hat{\beta}_0\right)\exp\left(\hat{\beta}_1 x_{1}\right) \exp\left(\hat\beta_2 t\right)} = \exp(\hat{\beta}_2)\]

Interpretation of Coefficients

How do we interpret \(\hat{\beta}_2 = -0.078\)?

\[\exp(\hat{\beta}_2) = \frac{\text{odds of surviving at age } (t + 1)}{\text{odds of surviving at age t}}\]


exp(-0.078)
[1] 0.9249644

Holding sex constant, for a 1 year increase in age, the estimated odds of survival are multiplied by 0.925.

Prediction

How can I use this model to predict, for a given sex and age:

  1. the odds of survival,
  2. the probability of survival, and
  3. whether or not we expect survival?

Prediction

How can I use this model to predict, for a given sex and age:

  1. the odds of survival,
  2. the probability of survival, and
  3. whether or not we expect survival?

\[ \text{odds} = \frac{P(Y = 1)}{1 - P(Y = 1)} = \exp\left(\hat\beta_0 + \hat\beta_1x_1 + \hat\beta_2x_2\right) \]

In R:

odds <- exp(
  predict(mod2, newdata = data.frame(Age = c(20, 65),
                                     Sex = c("Female", "Male")))
  )
odds
         1          2 
5.29272036 0.03174105 

Prediction

How can I use this model to predict, for a given sex and age:

  1. the odds of survival,
  2. the probability of survival, and
  3. whether or not we expect survival?


ggplot(donner, aes(x = Age,
                   y = Survived, 
                   color = Sex)) + 
  geom_point(size = 3) + 
  labs(y = "P(Y = 1)") +
  geom_smooth(method = 'glm',
              method.args = list(family = "binomial"),
              se = FALSE)

Prediction

How can I use this model to predict, for a given sex and age:

  1. the odds of survival,
  2. the probability of survival, and
  3. whether or not we expect survival?


\[ P(Y = 1) = \frac{\exp\left(\hat\beta_0 + \hat\beta_1x_1 + \hat\beta_2x_2\right)}{1 + \exp\left(\hat\beta_0 + \hat\beta_1x_1 + \hat\beta_2x_2\right)} \]

In R:

prob <- odds / (1 + odds)
prob
         1          2 
0.84108622 0.03076455 
# or
prob <- predict(mod2,
                newdata = data.frame(Age = c(20, 65),
                                     Sex = c("Female", "Male")), 
                type = "response")
prob
         1          2 
0.84108622 0.03076455 

Prediction

How can I use this model to predict, for a given sex and age:

  1. the odds of survival,
  2. the probability of survival, and
  3. whether or not we expect survival?

We can use the predicted probability at for an individual to help us predict the binary outcome.

\[\widehat{P(Y = 1)} \geq 0.5 \rightarrow \hat{y} = 1\] \[\widehat{P(Y = 1)} < 0.5 \rightarrow \hat{y} = 0\]

In R:

prob >= 0.5
    1     2 
 TRUE FALSE 

Predicted probability of survival for each member of the Donner Party

preds <- predict(mod2, newdata = donner, type = "response")
head(preds)
        1         2         3         4         5         6 
0.4587010 0.5255405 0.1831661 0.3289359 0.3643360 0.1831661 

Prediction Accuracy

donner %>%
  bind_cols(preds = preds) %>% # quickly add as a column
  mutate(pred_survived = case_when(
    preds >= 0.5 ~ 1,
    preds < 0.5 ~ 0
  )) %>%
  group_by(Survived) %>%
  count(pred_survived)
# A tibble: 4 × 3
# Groups:   Survived [2]
  Survived pred_survived     n
     <dbl>         <dbl> <int>
1        0             0    23
2        0             1     2
3        1             0     8
4        1             1    12
  • Pretty good!
  • How should we quantify our error rates?

Recap

Today, we learned about logistic regression, a model suitable when your response variable is categorical and binary, but your predictors can be categorical or quantitative.

  • Logistic regression is similar to linear regression, but we link the response variable to the linear predictor with the log odds or “logit” function:

\[ \log\left(\frac{P(Y = 1)}{1 - P(Y = 1)} \right) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p \]

  • Unlike linear regression models, logistic regression models always predict the probability of success to be between 0 and 1.

  • We have to interpret odds and odds ratios to interpret the coefficients.

  • We can predict many quantities: odds of success, probability of success, and even whether or not we expect success!