Skip to main content

24  Logistic Regression in Tidymodels

“In God we trust, all others bring data.” - W. Edwards Deming

The previous chapter introduced logistic regression with a single predictor. This chapter adds more predictors and uses tidymodels to fit, interpret, and test the model, make predictions from it, and evaluate those predictions. Most of the second half is about evaluation, because turning probabilities into decisions raises questions that do not come up with a quantitative response: which mistakes cost more, what threshold to use, and whether the predicted probabilities can be taken at face value.

24.1 Multiple Logistic Regression

Simple logistic regression has one predictor. Multiple logistic regression has several, combined in the linear predictor just as in multiple linear regression.

For observation \(i\), the model is

\[ \log\left(\frac{\pi_i}{1-\pi_i}\right)=\eta_i=\mathbf{x}_i^\prime\boldsymbol{\beta}, \]

where \(\pi_i=P(Y_i=1)\) and \(\eta_i\) is the linear predictor. Equivalently,

\[ E(Y_i)=\pi_i= \frac{\exp(\mathbf{x}_i^\prime\boldsymbol{\beta})} {1+\exp(\mathbf{x}_i^\prime\boldsymbol{\beta})}. \]

As with one predictor, the response function is bounded between 0 and 1. The predictors can be quantitative variables, indicators for categorical variables, transformations, or interaction terms, exactly as in multiple linear regression. The coefficients are estimated by maximum likelihood.

NoteWhat changes in multiple logistic regression?

The interpretation changes from a simple odds ratio to a partial odds ratio.

For a quantitative predictor, \(\exp(\beta_j)\) is the factor by which the odds of \(Y=1\) are multiplied for a one-unit increase in \(x_j\), holding the other predictors fixed.

For an indicator predictor, \(\exp(\beta_j)\) compares the odds for the group coded 1 with the odds for the reference group coded 0, holding the other predictors fixed.

24.2 Example Using Tidymodels

Example 24.1  

ExampleDisease outbreak data and predictors

In a health study of an epidemic outbreak of a disease spread by mosquitoes, individuals were randomly sampled within two sectors of a city. The response was coded

  • \(Y = 1\): disease present,
  • \(Y = 0\): disease not present.

The predictors are:

Variable Meaning
\(X_1\) Age
\(X_2\) Indicator for middle socioeconomic class
\(X_3\) Indicator for lower socioeconomic class
\(X_4\) Indicator for sector 2

Socioeconomic class is represented by two indicators:

Class \(X_2\) \(X_3\)
Upper 0 0
Middle 1 0
Lower 0 1

Upper class is the reference group. City sector is represented by one indicator, with \(X_4=0\) for sector 1 and \(X_4=1\) for sector 2.

Example 24.2  

ExampleLoading and exploring the disease outbreak data

We load the data, name the variables, and code the response as a factor. The levels are in the order 0, 1, so glm() models the probability of the second level, disease present. (The previous chapter discussed why the order matters.)

library(tidyverse)
library(tidymodels)
library(GGally)

tidymodels_prefer()

disease_data <- read.table("CH14TA03.txt")

names(disease_data) <- c("id", "X1", "X2", "X3", "X4", "Y")

disease_data <- disease_data |>
  mutate(Y = factor(Y, levels = c(0, 1)))

disease_data |>
  count(Y) |>
  mutate(proportion = n / sum(n))
  Y  n proportion
1 0 67  0.6836735
2 1 31  0.3163265

Of the 98 people, 31 (about 32%) had the disease. Keep that number in mind. A “model” that predicts no disease for everyone would be right about 68% of the time, and any real model has to do better than that.

A scatterplot matrix gives a quick look at the response and predictors.

ggpairs(disease_data |> select(-id))

Scatterplot matrix of the disease outbreak response and predictors.

The scatterplot matrix is good for a broad scan, but with a binary response and three indicator predictors, most of its panels are hard to read. Targeted plots work better. The next two focus on age and on the indicator variables.

disease_data |>
  ggplot(aes(x = Y, y = X1, color = Y)) +
  geom_boxplot(outlier.shape = NA, alpha = 0.15) +
  geom_jitter(width = 0.12, height = 0, alpha = 0.75) +
  scale_color_manual(values = c("0" = "#D55E00", "1" = "#0072B2")) +
  labs(
    x = "Disease status",
    y = "Age",
    color = "Y",
    title = "Age by disease status"
  ) +
  theme_minimal()

Boxplot and jittered points showing age by disease status.

disease_indicator_rates <- disease_data |>
  mutate(event = as.integer(as.character(Y))) |>
  select(event, X2, X3, X4) |>
  pivot_longer(
    cols = X2:X4,
    names_to = "predictor",
    values_to = "indicator_value"
  ) |>
  group_by(predictor, indicator_value) |>
  summarize(
    disease_rate = mean(event),
    n = n(),
    .groups = "drop"
  )

disease_indicator_rates |>
  ggplot(aes(x = factor(indicator_value), y = disease_rate)) +
  geom_col(fill = "#0072B2") +
  geom_text(aes(label = paste0("n = ", n)), vjust = -0.25) +
  facet_wrap(~ predictor) +
  scale_y_continuous(
    limits = c(0, 1),
    labels = scales::label_percent()
  ) +
  labs(
    x = "Indicator value",
    y = "Observed disease rate",
    title = "Observed disease rate by indicator variable"
  ) +
  theme_minimal()

Bar charts showing disease rate by indicator value for middle class, lower class, and sector 2.

The diseased group is older on average, and the disease rate is much higher in sector 2 than in sector 1. The differences by class are smaller. These plots describe one predictor at a time; the model will describe each predictor’s association after accounting for the others.

24.2.1 Is the Logit Linear in Age?

The model assumes the log-odds change linearly with age. With a quantitative response we would check linearity with a scatterplot, but a scatterplot of 0s and 1s against age shows very little. Instead, we group the observations by age, compute the proportion with the disease in each group, convert it to a logit, and plot that against the average age of the group. This is called an empirical logit plot. If the points fall near a straight line, a linear term in age is reasonable.

Example 24.3  

ExampleEmpirical logits by age group

We cut age into six groups of about equal size. The empirical logit adds 0.5 to the counts, \(\log\left((y + 0.5)/(n - y + 0.5)\right)\), so that a group with no cases (or all cases) does not give an infinite logit.

age_groups <- disease_data |>
  mutate(
    event = as.integer(as.character(Y)),
    age_group = ntile(X1, 6)
  ) |>
  group_by(age_group) |>
  summarize(
    mean_age = mean(X1),
    n = n(),
    cases = sum(event),
    .groups = "drop"
  ) |>
  mutate(empirical_logit = log((cases + 0.5) / (n - cases + 0.5)))

age_groups
# A tibble: 6 × 5
  age_group mean_age     n cases empirical_logit
      <int>    <dbl> <int> <int>           <dbl>
1         1     4.65    17     1          -2.40 
2         2    10.5     17     4          -1.10 
3         3    17.9     16     8           0    
4         4    25.6     16     4          -1.02 
5         5    35.6     16     6          -0.480
6         6    59.2     16     8           0    
age_only_fit <- glm(Y ~ X1, data = disease_data, family = binomial)

age_groups |>
  ggplot(aes(x = mean_age, y = empirical_logit)) +
  geom_abline(
    intercept = coef(age_only_fit)[1],
    slope = coef(age_only_fit)[2],
    color = "#0072B2",
    linewidth = 1
  ) +
  geom_point(aes(size = n)) +
  labs(
    x = "Mean age in group",
    y = "Empirical logit of disease",
    size = "n",
    title = "Empirical logits by age group",
    subtitle = "Blue line = fitted logit from a logistic regression on age alone"
  ) +
  theme_minimal()

Empirical logits of disease for six age groups plotted against mean age, with a fitted straight line from a logistic regression on age.

With only about 16 people per group, each empirical logit is noisy, so we are looking for a clear curve rather than small wiggles. The third group (8 cases out of 16) sits well above the line, but two or three cases fewer would put it on the line. The points rise with age without an obvious bend, so a linear term in age looks adequate. If the points had curved, we would try a transformation of age or a quadratic term, just as in linear regression.

24.3 Fitting and Interpreting the Model

Example 24.4  

ExampleFitting a multiple logistic regression workflow

We set up the recipe, model, and workflow and fit them.

disease_recipe <- recipe(Y ~ X1 + X2 + X3 + X4, data = disease_data)

disease_model <- logistic_reg() |>
  set_engine("glm")

disease_workflow <- workflow() |>
  add_recipe(disease_recipe) |>
  add_model(disease_model)

disease_fit <- disease_workflow |>
  fit(data = disease_data)

disease_fit |>
  tidy()
# A tibble: 5 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  -2.31      0.643     -3.60  0.000319
2 X1            0.0298    0.0135     2.20  0.0276  
3 X2            0.409     0.599      0.682 0.495   
4 X3           -0.305     0.604     -0.505 0.613   
5 X4            1.57      0.502      3.14  0.00169 

tidy() returns the estimated coefficients on the log-odds scale. Exponentiating them gives estimated odds ratios, and we can ask for confidence intervals on that scale too.

disease_fit |>
  tidy(exponentiate = TRUE, conf.int = TRUE) |>
  select(term, estimate, conf.low, conf.high, p.value) |>
  rename(
    odds_ratio = estimate,
    lower_95 = conf.low,
    upper_95 = conf.high
  )
# A tibble: 5 × 5
  term        odds_ratio lower_95 upper_95  p.value
  <chr>            <dbl>    <dbl>    <dbl>    <dbl>
1 (Intercept)     0.0990   0.0254    0.323 0.000319
2 X1              1.03     1.00      1.06  0.0276  
3 X2              1.50     0.464     4.96  0.495   
4 X3              0.737    0.220     2.42  0.613   
5 X4              4.83     1.85     13.4   0.00169 

From the fitted model:

  • Each additional year of age multiplies the estimated odds of disease by about 1.03, holding class and sector fixed.
  • Compared with upper class, middle class multiplies the estimated odds by about 1.50, holding age and sector fixed.
  • Compared with upper class, lower class multiplies the estimated odds by about 0.74, holding age and sector fixed.
  • Compared with sector 1, sector 2 multiplies the estimated odds by about 4.83, holding age and class fixed.

A year of age is a small unit. The odds ratio for 10 years is \(\exp(10\hat\beta_1)\):

disease_fit |>
  tidy() |>
  filter(term == "X1") |>
  mutate(odds_ratio_10_years = exp(10 * estimate))
# A tibble: 1 × 6
  term  estimate std.error statistic p.value odds_ratio_10_years
  <chr>    <dbl>     <dbl>     <dbl>   <dbl>               <dbl>
1 X1      0.0298    0.0135      2.20  0.0276                1.35

Ten more years of age multiplies the estimated odds by about 1.35, holding the other predictors fixed.

The confidence intervals for both class odds ratios contain 1, and they are wide: the data are consistent with middle class having anywhere from about half to about five times the odds of upper class. The intervals for age and sector do not contain 1.

These are associations estimated from an observational study, not causal effects. People in sector 2 may differ from people in sector 1 in ways the model does not include.

24.3.1 Testing Several Coefficients at Once

The \(z\) statistics and \(p\)-values from tidy() are Wald tests of one coefficient at a time. Socioeconomic class is a single variable represented by two indicators, though, and the question “does class matter, after adjusting for age and sector?” is a test that both of its coefficients are zero.

In logistic regression the counterpart of the partial \(F\) test is the likelihood ratio test, also called the drop-in-deviance test. We fit the full model and a reduced model without the terms being tested. If the reduced model is correct, the increase in deviance, \[ G^2 = \text{Deviance}_{\text{reduced}} - \text{Deviance}_{\text{full}}, \] has approximately a \(\chi^2\) distribution with degrees of freedom equal to the number of coefficients dropped.

Example 24.5  

ExampleLikelihood ratio test for socioeconomic class

anova() compares two nested glm fits. We fit both models directly with glm(); the full model has exactly the same coefficients as the workflow fit.

full_glm <- glm(Y ~ X1 + X2 + X3 + X4, data = disease_data, family = binomial)

all.equal(coef(full_glm), coef(extract_fit_engine(disease_fit)))
[1] TRUE
reduced_glm <- glm(Y ~ X1 + X4, data = disease_data, family = binomial)

anova(reduced_glm, full_glm, test = "LRT")
Analysis of Deviance Table

Model 1: Y ~ X1 + X4
Model 2: Y ~ X1 + X2 + X3 + X4
  Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1        95     102.26                     
2        93     101.05  2   1.2052   0.5474

It might seem simpler to use extract_fit_engine(disease_fit), which pulls the glm object out of the workflow, as the full model. Don’t. Inside a workflow, the response is renamed ..y, and when anova() is given models with different response names, it silently drops all but the first and prints a sequential table for that one model alone. There is no error, only a warning that is easy to miss, and the output looks like a valid deviance table.

Removing the two class indicators increases the deviance by only about 1.2 on 2 degrees of freedom, with a \(p\)-value around 0.55. After adjusting for age and sector, the data give no evidence that disease odds differ by socioeconomic class.

The same idea tests one term at a time. drop1() compares the full model with each single-term deletion.

drop1(full_glm, test = "LRT")
Single term deletions

Model:
Y ~ X1 + X2 + X3 + X4
       Df Deviance    AIC     LRT Pr(>Chi)   
<none>      101.05 111.05                    
X1      1   106.20 114.20  5.1495 0.023253 * 
X2      1   101.52 109.52  0.4669 0.494416   
X3      1   101.31 109.31  0.2560 0.612892   
X4      1   111.50 119.50 10.4481 0.001228 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

For each term, the likelihood ratio \(p\)-value is close to the Wald \(p\)-value from tidy(). They are not identical, and with small samples or large coefficients they can be quite different. When they disagree, the likelihood ratio test is generally the one to trust, for the reasons covered in the previous chapter.

24.3.2 Seeing the Model on the Probability Scale

Odds ratios are constant, but the model’s consequences on the probability scale are easier to understand. A plot of fitted probabilities against one quantitative predictor, with separate curves for groups, shows them directly. These are sometimes called effect plots.

Example 24.6  

ExampleFitted probability of disease by age, class, and sector

We predict the probability of disease over a grid of ages for every combination of class and sector.

class_lookup <- tibble(
  class = factor(c("Upper", "Middle", "Lower"),
                 levels = c("Upper", "Middle", "Lower")),
  X2 = c(0, 1, 0),
  X3 = c(0, 0, 1)
)

effect_grid <- expand_grid(
  X1 = seq(1, 75, by = 1),
  class_lookup,
  X4 = c(0, 1)
)

effect_grid <- effect_grid |>
  bind_cols(predict(disease_fit, new_data = effect_grid, type = "prob")) |>
  mutate(sector = if_else(X4 == 0, "Sector 1", "Sector 2"))

effect_grid |>
  ggplot(aes(x = X1, y = .pred_1, color = class)) +
  geom_line(linewidth = 1) +
  facet_wrap(~ sector) +
  scale_color_manual(values = c(Upper = "#0072B2", Middle = "#D55E00",
                                Lower = "#009E73")) +
  scale_y_continuous(limits = c(0, 1), labels = scales::label_percent()) +
  labs(
    x = "Age",
    y = "Fitted P(disease)",
    color = "Class",
    title = "Fitted probability of disease"
  ) +
  theme_minimal()

Fitted probability of disease versus age, with one curve per socioeconomic class, in separate panels for sector 1 and sector 2; the sector 2 curves are much higher.

Sector has the largest effect: at every age and class, the sector 2 curve is far above the sector 1 curve. The class curves are close together, consistent with the likelihood ratio test.

The plot also shows something the odds ratios hide. The sector odds ratio is the same, 4.83, at every age, but the difference in probability between the sectors is not. For a young upper-class person, the probability goes from about 10% in sector 1 to about 36% in sector 2; for a 60-year-old, from about 37% to 74%. In logistic regression, a model with no interaction terms still produces different probability differences in different groups.

The curves extend over the full age range for every group, but some combinations, such as older lower-class people in sector 1, may have few or no observations. Predictions for combinations that are rare in the data are extrapolations even when each predictor is within its own range.

24.3.3 Interactions

An interaction term lets the effect of one predictor depend on another. In logistic regression, the interaction is on the log-odds scale: with an age-by-sector interaction, the age odds ratio is different in the two sectors.

Example 24.7  

ExampleDoes the age effect differ by sector?
interaction_glm <- glm(Y ~ X1 * X4 + X2 + X3,
                       data = disease_data, family = binomial)

tidy(interaction_glm)
# A tibble: 6 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  -2.81      0.824     -3.42  0.000633
2 X1            0.0475    0.0214     2.22  0.0261  
3 X4            2.41      0.937      2.58  0.00997 
4 X2            0.409     0.598      0.683 0.494   
5 X3           -0.366     0.609     -0.600 0.548   
6 X1:X4        -0.0297    0.0270    -1.10  0.271   
anova(full_glm, interaction_glm, test = "LRT")
Analysis of Deviance Table

Model 1: Y ~ X1 + X2 + X3 + X4
Model 2: Y ~ X1 * X4 + X2 + X3
  Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1        93    101.054                     
2        92     99.834  1   1.2202   0.2693

The likelihood ratio test for the interaction has a \(p\)-value of about 0.27, so the data do not show that the age effect differs by sector, and we keep the model without the interaction. If we did keep it, \(\exp(\hat\beta_{X1})\) would be the age odds ratio in sector 1 only, and the sector 2 age odds ratio would be \(\exp(\hat\beta_{X1} + \hat\beta_{X1:X4})\). With an interaction in the model, a coefficient by itself no longer describes “the” effect of its predictor.

24.3.4 Adjusted and Unadjusted Odds Ratios

The sector odds ratio from a model with sector alone is not the same as the sector odds ratio adjusted for age and class.

bind_rows(
  glm(Y ~ X4, data = disease_data, family = binomial) |>
    tidy(exponentiate = TRUE) |>
    mutate(model = "Sector only"),
  full_glm |>
    tidy(exponentiate = TRUE) |>
    mutate(model = "Adjusted for age and class")
) |>
  filter(term == "X4") |>
  select(model, odds_ratio = estimate, std.error, p.value)
# A tibble: 2 × 4
  model                      odds_ratio std.error  p.value
  <chr>                           <dbl>     <dbl>    <dbl>
1 Sector only                      5.72     0.473 0.000227
2 Adjusted for age and class       4.83     0.502 0.00169 

Part of the difference comes from confounding: if the two sectors have different age distributions, some of the unadjusted sector difference is really an age difference. In linear regression, that is the whole story, and if a predictor is unrelated to the others, adding it does not change their coefficients. Logistic regression has an extra wrinkle: odds ratios change when a predictor is added even if it is completely unrelated to the other predictors, as long as it is related to the response. This property is called non-collapsibility.

Example 24.8  

ExampleAdding an independent predictor changes the odds ratio

We simulate 100,000 observations with a binary predictor \(x\) and a standard normal predictor \(z\), generated independently, so \(z\) is not a confounder of \(x\). The true model is \(\text{logit}(\pi) = -1 + 1\cdot x + \gamma z\), so the conditional odds ratio for \(x\) is \(e^1 \approx 2.72\) for every \(\gamma\). We fit models with and without \(z\) for several values of \(\gamma\).

set.seed(2401)

noncollapse <- tibble(gamma = c(0, 0.5, 1, 1.5, 2, 3)) |>
  mutate(
    estimates = map(gamma, \(g) {
      sim <- tibble(
        x = rbinom(1e5, 1, 0.5),
        z = rnorm(1e5),
        y = rbinom(1e5, 1, plogis(-1 + x + g * z))
      )
      tibble(
        `Without z` = exp(coef(glm(y ~ x, data = sim, family = binomial))["x"]),
        `With z` = exp(coef(glm(y ~ x + z, data = sim, family = binomial))["x"]),
        cor_xz = cor(sim$x, sim$z)
      )
    })
  ) |>
  unnest(estimates)

noncollapse
# A tibble: 6 × 4
  gamma `Without z` `With z`    cor_xz
  <dbl>       <dbl>    <dbl>     <dbl>
1   0          2.73     2.73 -0.00554 
2   0.5        2.53     2.67 -0.000445
3   1          2.28     2.71 -0.00353 
4   1.5        2.03     2.66  0.00299 
5   2          1.85     2.81 -0.00436 
6   3          1.62     2.77  0.00261 
noncollapse |>
  pivot_longer(c(`Without z`, `With z`), names_to = "model",
               values_to = "odds_ratio") |>
  ggplot(aes(x = gamma, y = odds_ratio, color = model)) +
  geom_hline(yintercept = exp(1), linetype = "dashed") +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +
  scale_color_manual(values = c("With z" = "#0072B2", "Without z" = "#D55E00")) +
  labs(
    x = expression(paste("Coefficient of z, ", gamma)),
    y = "Estimated odds ratio for x",
    color = NULL,
    title = "Odds ratio for x, with and without an independent predictor z",
    subtitle = "Dashed line = true conditional odds ratio, e = 2.72"
  ) +
  theme_minimal()

Estimated odds ratio for x with and without z in the model, plotted against the strength of z; the adjusted odds ratio stays at 2.72 while the unadjusted odds ratio falls as z gets stronger.

The correlation between \(x\) and \(z\) is essentially zero, yet leaving \(z\) out of the model pulls the odds ratio for \(x\) toward 1, and the stronger \(z\) is, the more it is pulled. Neither number is wrong. They answer different questions: the odds ratio with \(z\) compares people with the same \(z\), and the one without \(z\) compares groups averaged over \(z\). Two lessons follow: an odds ratio always has to be reported along with what it was adjusted for, and odds ratios from studies that adjusted for different variables cannot be compared directly, even when the adjustment variables are unrelated to the predictor of interest.

24.4 Predictions

After fitting the model, predict() gives three kinds of predictions:

  • type = "raw" gives the fitted value on the logit scale (the linear predictor),
  • type = "prob" gives the predicted probability of each class,
  • type = "class" gives the predicted class, using a 0.5 probability threshold.

Example 24.9  

ExamplePredicted logits, probabilities, and classes
disease_predictions <- disease_data |>
  select(Y) |>
  bind_cols(
    tibble(
      .pred_logit = as.numeric(
        predict(disease_fit, new_data = disease_data, type = "raw")
      )
    ),
    predict(disease_fit, new_data = disease_data, type = "prob"),
    predict(disease_fit, new_data = disease_data, type = "class")
  )

disease_predictions |>
  slice_head(n = 10)
   Y .pred_logit   .pred_0    .pred_1 .pred_class
1  0  -1.3311818 0.7910360 0.20896395           0
2  0  -1.2716816 0.7810305 0.21896953           0
3  0  -2.1344343 0.8942052 0.10579477           0
4  0  -0.5279293 0.6290000 0.37099998           0
5  1  -2.0826877 0.8892091 0.11079091           0
6  0  -1.8446870 0.8635021 0.13649792           0
7  0  -2.4396888 0.9198041 0.08019586           0
8  1  -0.9818917 0.7274834 0.27251659           0
9  1  -1.1306422 0.7559574 0.24404261           0
10 0  -0.8033912 0.6906994 0.30930059           0

.pred_1 is the fitted probability of disease, and .pred_0 is \(1 -\) .pred_1. .pred_class is 1 when .pred_1 is at least 0.5.

disease_predictions |>
  ggplot(aes(x = .pred_logit, y = .pred_1, color = Y)) +
  geom_point(alpha = 0.8) +
  geom_hline(yintercept = 0.5, linetype = "dashed") +
  scale_color_manual(values = c("0" = "#D55E00", "1" = "#0072B2")) +
  labs(
    x = "Fitted logit",
    y = "Predicted P(Y = 1)",
    color = "Observed Y"
  ) +
  theme_minimal()

Predicted probability of disease plotted against the fitted logit, with points colored by the true disease status.

All the points lie on one logistic curve, because the probability is a function of the logit alone. The dashed line is the 0.5 threshold, which corresponds to a logit of 0. Points above it are classified as disease present and points below as disease absent. A blue point below the line or an orange point above it is a misclassification.

Notice how few points are above the line. Most of the fitted probabilities are below 0.5, including for many of the people who had the disease.

The distributions of the fitted probabilities in the two groups show how well the model separates them.

disease_predictions |>
  ggplot(aes(x = .pred_1, fill = Y)) +
  geom_histogram(binwidth = 0.05, boundary = 0, alpha = 0.6,
                 position = "identity", color = "white") +
  geom_vline(xintercept = 0.5, linetype = "dashed") +
  scale_fill_manual(values = c("0" = "#D55E00", "1" = "#0072B2")) +
  labs(
    x = "Predicted P(disease)",
    y = "Count",
    fill = "Observed Y",
    title = "Predicted probabilities for people with and without the disease"
  ) +
  theme_minimal()

Overlapping histograms of predicted disease probability for people with and without the disease; the two distributions overlap heavily.

The people with the disease tend to have higher predicted probabilities, but the two histograms overlap a great deal. No threshold will separate them cleanly. Everything in the rest of this chapter, from the confusion matrix to the ROC curve, is a way of summarizing this overlap.

24.5 Confusion Matrix

A confusion matrix cross-tabulates the predicted classes against the observed classes.

Example 24.10  

ExampleConfusion matrix and classification metrics
disease_confusion <- conf_mat(
  disease_predictions,
  truth = Y,
  estimate = .pred_class
)

disease_confusion
          Truth
Prediction  0  1
         0 58 19
         1  9 12
autoplot(disease_confusion, type = "heatmap")

Heat map of the confusion matrix for the disease model at a 0.5 threshold.

Disease present, \(Y=1\), is the event class. There are 9 false positives (predicted 1, observed 0) and 19 false negatives (predicted 0, observed 1).

Several metrics summarize the confusion matrix. Because the event class is the second factor level, we set event_level = "second".

summary(disease_confusion, event_level = "second") |>
  filter(.metric %in% c("accuracy", "sens", "spec", "ppv", "npv"))
# A tibble: 5 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.714
2 sens     binary         0.387
3 spec     binary         0.866
4 ppv      binary         0.571
5 npv      binary         0.753

The definitions, with TP, FP, TN, and FN for true and false positives and negatives:

Metric Formula Question it answers
Accuracy \((TP + TN)/n\) What fraction of all predictions are correct?
Sensitivity \(TP/(TP + FN)\) Of the people with the disease, what fraction are predicted to have it?
Specificity \(TN/(TN + FP)\) Of the people without the disease, what fraction are predicted not to have it?
Positive predictive value (PPV) \(TP/(TP + FP)\) Of the people predicted to have the disease, what fraction do?
Negative predictive value (NPV) \(TN/(TN + FN)\) Of the people predicted not to have it, what fraction do not?

With a 0.5 threshold, the model catches only 12 of the 31 cases, a sensitivity of about 0.39. It is much better at identifying people without the disease, with a specificity of about 0.87.

NoteSensitivity and specificity depend on the event class

Before computing classification metrics, decide which class is the event of interest. Here it is disease present, \(Y=1\).

yardstick treats the first factor level as the event unless told otherwise. Since the levels here are 0 then 1, we set event_level = "second". If we forgot, sensitivity and specificity would swap, and so would PPV and NPV, with no error or warning. Accuracy would not change, which makes the mistake easy to miss.

24.5.1 Accuracy Can Be Misleading

The model’s accuracy at the 0.5 threshold is about 0.71. That sounds respectable until we compare it with a model that ignores every predictor and predicts “no disease” for everyone.

Example 24.11  

ExampleComparing with a model that always predicts the majority class
majority_predictions <- disease_predictions |>
  mutate(.pred_majority = factor("0", levels = levels(Y)))

bind_rows(
  disease_predictions |>
    metrics(truth = Y, estimate = .pred_class) |>
    filter(.metric == "accuracy") |>
    mutate(model = "Logistic regression"),
  majority_predictions |>
    metrics(truth = Y, estimate = .pred_majority) |>
    filter(.metric == "accuracy") |>
    mutate(model = "Always predict 0")
) |>
  select(model, .metric, .estimate)
# A tibble: 2 × 3
  model               .metric  .estimate
  <chr>               <chr>        <dbl>
1 Logistic regression accuracy     0.714
2 Always predict 0    accuracy     0.684

Predicting “no disease” for everyone is right 68% of the time. The logistic model, even evaluated on the data it was fit to, beats that by only a few percentage points. The trivial model has sensitivity 0, so it is useless for finding cases, but on accuracy alone it looks nearly as good.

The more unbalanced the classes, the worse this gets. If a disease affects 1% of a population, predicting “healthy” for everyone has 99% accuracy. Always compare accuracy with the proportion in the larger class (the no-information rate), and report sensitivity and specificity alongside it.

24.6 Thresholds

The default threshold of 0.5 is not always the best one. If false negatives are costly, we can lower the threshold so that more people are classified as having the disease. If false positives are costly, we can raise it.

Example 24.12  

ExampleSweeping the classification threshold

Instead of trying a few thresholds, we compute sensitivity, specificity, and accuracy for every threshold from 0.05 to 0.95.

threshold_metrics <- function(predictions, cutoff) {
  predictions |>
    mutate(.pred_threshold = factor(if_else(.pred_1 >= cutoff, "1", "0"),
                                    levels = c("0", "1"))) |>
    summarize(
      threshold = cutoff,
      true_positives = sum(Y == "1" & .pred_threshold == "1"),
      false_positives = sum(Y == "0" & .pred_threshold == "1"),
      true_negatives = sum(Y == "0" & .pred_threshold == "0"),
      false_negatives = sum(Y == "1" & .pred_threshold == "0")
    ) |>
    mutate(
      sensitivity = true_positives / (true_positives + false_negatives),
      specificity = true_negatives / (true_negatives + false_positives),
      accuracy = (true_positives + true_negatives) / nrow(predictions)
    )
}

threshold_sweep <- map(
  round(seq(0.05, 0.95, by = 0.01), 2),
  \(cutoff) threshold_metrics(disease_predictions, cutoff)
) |>
  list_rbind()

threshold_sweep |>
  filter(threshold %in% c(0.25, 0.50, 0.75)) |>
  select(threshold, sensitivity, specificity, accuracy)
  threshold sensitivity specificity  accuracy
1      0.25   0.8064516   0.6417910 0.6938776
2      0.50   0.3870968   0.8656716 0.7142857
3      0.75   0.1612903   1.0000000 0.7346939
threshold_sweep |>
  pivot_longer(c(sensitivity, specificity, accuracy),
               names_to = "metric", values_to = "value") |>
  ggplot(aes(x = threshold, y = value, color = metric)) +
  geom_line(linewidth = 1) +
  geom_vline(xintercept = 0.5, linetype = "dashed", color = "gray50") +
  scale_color_manual(values = c(accuracy = "gray30", sensitivity = "#0072B2",
                                specificity = "#D55E00")) +
  labs(
    x = "Threshold",
    y = NULL,
    color = NULL,
    title = "Classification metrics across thresholds"
  ) +
  theme_minimal()

Sensitivity, specificity, and accuracy of the disease model plotted against the classification threshold; sensitivity falls and specificity rises as the threshold increases.

As the threshold rises, fewer people are classified as having the disease, so sensitivity falls and specificity rises. Accuracy is fairly flat over a wide range of thresholds and is highest at a fairly high threshold. That is a consequence of the class imbalance: with more 0s than 1s, accuracy rewards predicting 0. Choosing the threshold that maximizes accuracy usually means accepting low sensitivity.

WarningThe threshold should reflect the cost of mistakes

A 0.5 threshold treats false positives and false negatives as equally bad. That may not be reasonable.

In disease screening, a false negative (a sick person who is missed) may be much worse than a false positive (a healthy person sent for a follow-up test). Then a lower threshold makes sense, because it catches more true cases at the price of more false positives.

If the predicted probabilities are accurate and a false negative costs \(c_{FN}\) while a false positive costs \(c_{FP}\), the expected cost of classifying a person as positive is \((1-\pi)c_{FP}\) and of classifying them as negative is \(\pi\,c_{FN}\). Classifying as positive is cheaper when \[ \pi > \frac{c_{FP}}{c_{FP} + c_{FN}}. \] With equal costs this gives the familiar 0.5. If false negatives cost five times as much as false positives, the threshold is \(1/6 \approx 0.17\).

Example 24.13  

ExampleChoosing a threshold with a cost rule

Suppose a false positive costs 1 and a false negative costs 5. These numbers are only for illustration. We compute the total cost at every threshold.

cost_fp <- 1
cost_fn <- 5

threshold_costs <- threshold_sweep |>
  mutate(total_cost = false_positives * cost_fp + false_negatives * cost_fn)

threshold_costs |>
  filter(threshold %in% c(0.17, 0.25, 0.50, 0.75)) |>
  select(threshold, false_positives, false_negatives, total_cost)
  threshold false_positives false_negatives total_cost
1      0.17              36               3         51
2      0.25              24               6         54
3      0.50               9              19        104
4      0.75               0              26        130
best_threshold <- threshold_costs |>
  slice_min(total_cost, n = 1, with_ties = FALSE)

best_threshold |>
  select(threshold, false_positives, false_negatives, total_cost)
  threshold false_positives false_negatives total_cost
1      0.21              28               4         48
threshold_costs |>
  ggplot(aes(x = threshold, y = total_cost)) +
  geom_line(linewidth = 1, color = "#0072B2") +
  geom_vline(xintercept = cost_fp / (cost_fp + cost_fn),
             linetype = "dashed", color = "#D55E00") +
  geom_point(data = best_threshold, size = 3, color = "#D55E00") +
  annotate("text", x = cost_fp / (cost_fp + cost_fn) + 0.01,
           y = max(threshold_costs$total_cost),
           label = "1/6", hjust = 0, color = "#D55E00") +
  labs(
    x = "Threshold",
    y = "Total cost",
    title = "Total cost when a false negative costs 5 times a false positive"
  ) +
  theme_minimal()

Total misclassification cost plotted against threshold for costs of 1 per false positive and 5 per false negative, with the minimum cost threshold and the theoretical threshold of one sixth marked.

Under this cost rule, the 0.5 threshold is far from the cheapest. The total cost is lowest at a threshold near the theoretical value of 1/6, where the model classifies many more people as positive and misses far fewer cases.

The threshold here was chosen using the same data used to fit the model and to count the errors. The cost at the chosen threshold is therefore optimistic. Choosing a threshold is a tuning decision, and like any other tuning decision it should be made with cross-validation or a validation set, then checked on separate test data.

24.7 ROC Curves

A Receiver Operating Characteristic (ROC) curve shows how sensitivity and specificity trade off across all possible thresholds. It plots sensitivity (the true positive rate) against \(1-\text{specificity}\) (the false positive rate). Each point on the curve is one threshold: a very high threshold classifies nobody as positive, which is the point (0, 0), and a very low threshold classifies everybody as positive, which is (1, 1).

The diagonal line is a classifier that assigns probabilities at random. A useful classifier has a curve that bows above the diagonal, and a perfect one goes straight up to the upper-left corner, where sensitivity and specificity are both 1.

24.7.1 Area Under the Curve (AUC)

The area under the ROC curve (AUC) summarizes the whole curve in one number:

  • AUC = 1: perfect separation,
  • AUC = 0.5: no better than random,
  • AUC < 0.5: worse than random, which usually means the event class was coded backward.

AUC has a direct interpretation. Pick one person with the disease and one without, at random. The AUC is the probability that the model gives the person with the disease the higher predicted probability (counting ties as one half). This is why AUC is called a measure of discrimination: it measures how well the model ranks cases above non-cases.

Example 24.14  

ExampleROC curve and AUC for the disease model
disease_predictions |>
  roc_curve(truth = Y, .pred_1, event_level = "second") |>
  autoplot()

ROC curve for the disease outbreak logistic regression model.

disease_predictions |>
  roc_auc(truth = Y, .pred_1, event_level = "second")
# A tibble: 1 × 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 roc_auc binary         0.776

We can check the concordance interpretation by comparing every case with every non-case.

case_probs <- disease_predictions$.pred_1[disease_predictions$Y == "1"]
noncase_probs <- disease_predictions$.pred_1[disease_predictions$Y == "0"]

pair_comparisons <- outer(case_probs, noncase_probs, "-")

tibble(
  pairs = length(pair_comparisons),
  case_higher = mean(pair_comparisons > 0),
  tied = mean(pair_comparisons == 0),
  concordance = mean(pair_comparisons > 0) + 0.5 * mean(pair_comparisons == 0)
)
# A tibble: 1 × 4
  pairs case_higher    tied concordance
  <int>       <dbl>   <dbl>       <dbl>
1  2077       0.776 0.00144       0.776

Of the 2077 case/non-case pairs, the model ranks the case higher in about 78% of them, which equals the AUC. AUC does not depend on any one threshold.

24.7.2 What AUC Does Not Measure

AUC depends only on the ranking of the predicted probabilities, not on their values. Any transformation that keeps the order of the probabilities leaves the ROC curve and the AUC unchanged, even if it makes the probabilities wildly wrong. A model can have a high AUC and still give probabilities that are systematically too high or too low.

Whether the predicted probabilities match the observed frequencies is called calibration. If a model is well calibrated, then among all the people it gives a probability of about 0.3, about 30% have the disease. A calibration plot groups the observations by predicted probability and plots the observed proportion in each group against the average predicted probability. A well-calibrated model has points near the line \(y = x\).

The Brier score summarizes calibration and discrimination together. It is the mean squared difference between the predicted probability and the 0/1 outcome, \(\frac{1}{n}\sum(\hat\pi_i - y_i)^2\). Smaller is better, and predicting the overall event rate for everyone gives a baseline of \(\bar{y}(1 - \bar{y})\).

Example 24.15  

ExampleSame AUC, different calibration

We compare the model’s predicted probabilities with a distorted version, \(\hat\pi^3\). Cubing keeps the order of the probabilities but pushes them all toward 0.

calibration_compare <- disease_predictions |>
  mutate(.pred_cubed = .pred_1^3) |>
  select(Y, Original = .pred_1, Cubed = .pred_cubed) |>
  pivot_longer(-Y, names_to = "probabilities", values_to = "p")

calibration_compare |>
  group_by(probabilities) |>
  summarize(
    auc = roc_auc_vec(Y, p, event_level = "second"),
    brier = brier_class_vec(Y, p, event_level = "second"),
    .groups = "drop"
  )
# A tibble: 2 × 3
  probabilities   auc brier
  <chr>         <dbl> <dbl>
1 Cubed         0.776 0.240
2 Original      0.776 0.174
mean_y <- mean(disease_predictions$Y == "1")
c(baseline_brier = mean_y * (1 - mean_y))
baseline_brier 
     0.2162641 
calibration_compare |>
  group_by(probabilities) |>
  mutate(bin = ntile(p, 5)) |>
  group_by(probabilities, bin) |>
  summarize(
    mean_predicted = mean(p),
    observed_rate = mean(Y == "1"),
    n = n(),
    .groups = "drop"
  ) |>
  ggplot(aes(x = mean_predicted, y = observed_rate, color = probabilities)) +
  geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
  geom_line() +
  geom_point(size = 3) +
  scale_color_manual(values = c(Cubed = "#D55E00", Original = "#0072B2")) +
  coord_equal(xlim = c(0, 1), ylim = c(0, 1)) +
  labs(
    x = "Mean predicted probability in group",
    y = "Observed disease rate in group",
    color = NULL,
    title = "Calibration plot, five groups"
  ) +
  theme_minimal()

Calibration plot with observed disease rate against mean predicted probability in five groups, for the original model and for cubed probabilities; the original points lie near the diagonal and the cubed points lie well above it.

The two sets of probabilities have exactly the same AUC. The cubed probabilities badly underestimate the risk. In the highest group they average about 0.3 where the observed disease rate is close to 0.6, and in the lower groups they are near 0 where 5% to 30% of people had the disease. The Brier score catches this, and the calibration plot shows it. The original model’s points lie close to the diagonal. They should, since these are in-sample predictions, and logistic regression with an intercept always matches the overall event rate on its own training data. Calibration on new data is the real test.

If the predictions will be used as probabilities (to tell a patient their risk, or to set a threshold from costs as above), calibration matters as much as discrimination. If they will be used only to rank people (who should be screened first), AUC is the more relevant summary.

24.7.3 Predictive Values Depend on Prevalence

Sensitivity and specificity describe the model’s behavior within the cases and within the non-cases. The predictive values also depend on how common the event is. By Bayes’ theorem, \[ \text{PPV} = \frac{\text{sens} \times p}{\text{sens} \times p + (1-\text{spec})(1-p)}, \] where \(p\) is the prevalence, the proportion of the population with the event.

Example 24.16  

ExampleThe same test in populations with different prevalence

Consider a classifier with sensitivity 0.90 and specificity 0.90, applied to populations where the disease is more or less common.

sens <- 0.90
spec <- 0.90

predictive_values <- function(prevalence) {
  tibble(
    prevalence = prevalence,
    PPV = sens * prevalence /
      (sens * prevalence + (1 - spec) * (1 - prevalence)),
    NPV = spec * (1 - prevalence) /
      (spec * (1 - prevalence) + (1 - sens) * prevalence)
  )
}

predictive_values(c(0.001, 0.01, 0.10, 0.32, 0.50))
# A tibble: 5 × 3
  prevalence     PPV   NPV
       <dbl>   <dbl> <dbl>
1      0.001 0.00893 1.000
2      0.01  0.0833  0.999
3      0.1   0.5     0.988
4      0.32  0.809   0.950
5      0.5   0.9     0.9  
prevalence_curve <- predictive_values(10^seq(-3, log10(0.9), length.out = 200))

prevalence_curve |>
  pivot_longer(c(PPV, NPV), names_to = "measure", values_to = "value") |>
  ggplot(aes(x = prevalence, y = value, color = measure)) +
  geom_line(linewidth = 1) +
  scale_x_log10(labels = scales::label_percent()) +
  scale_y_continuous(labels = scales::label_percent()) +
  scale_color_manual(values = c(NPV = "#D55E00", PPV = "#0072B2")) +
  labs(
    x = "Prevalence (log scale)",
    y = NULL,
    color = NULL,
    title = "Predictive values of a test with 90% sensitivity and specificity"
  ) +
  theme_minimal()

Positive and negative predictive values for a test with 90 percent sensitivity and specificity, plotted against prevalence on a log scale; PPV falls sharply as prevalence decreases.

At a prevalence of 32%, as in the outbreak data, about four out of five positive predictions are correct. At a prevalence of 1%, the same classifier’s positive predictions are right less than 10% of the time, because the 10% false positive rate applied to the 99% without the disease swamps the true positives. A model’s PPV from one study does not transfer to a population with a different prevalence. Sensitivity and specificity transfer better, though they too can change if the cases in the new population are milder or more severe.

24.8 Train-Test Evaluation

All of the metrics so far were computed on the same data used to fit the model. That is fine for learning the mechanics but gives an optimistic estimate of how the model will do on new people. The validation methods from Chapter 22 apply to classification just as they do to regression.

Example 24.17  

ExampleEvaluating the disease model on a testing set

We make a stratified split so that the training and testing sets have about the same proportion of disease cases.

set.seed(3386)

disease_split <- initial_split(
  disease_data,
  prop = 0.8,
  strata = Y
)

disease_train <- training(disease_split)
disease_test <- testing(disease_split)

bind_rows(
  disease_train |>
    count(Y) |>
    mutate(split = "Training"),
  disease_test |>
    count(Y) |>
    mutate(split = "Testing")
) |>
  group_by(split) |>
  mutate(proportion = n / sum(n)) |>
  ungroup()
# A tibble: 4 × 4
  Y         n split    proportion
  <fct> <int> <chr>         <dbl>
1 0        53 Training      0.688
2 1        24 Training      0.312
3 0        14 Testing       0.667
4 1         7 Testing       0.333

We fit the workflow on the training set and evaluate on the testing set.

disease_train_fit <- disease_workflow |>
  fit(data = disease_train)

disease_test_predictions <- disease_test |>
  select(Y) |>
  bind_cols(
    predict(disease_train_fit, new_data = disease_test, type = "prob"),
    predict(disease_train_fit, new_data = disease_test, type = "class")
  )

disease_test_confusion <- conf_mat(
  disease_test_predictions,
  truth = Y,
  estimate = .pred_class
)

disease_test_confusion
          Truth
Prediction  0  1
         0 13  4
         1  1  3
bind_rows(
  summary(disease_test_confusion, event_level = "second") |>
    filter(.metric %in% c("accuracy", "sens", "spec", "ppv", "npv")),
  disease_test_predictions |>
    roc_auc(truth = Y, .pred_1, event_level = "second"),
  disease_test_predictions |>
    brier_class(truth = Y, .pred_1, event_level = "second")
)
# A tibble: 7 × 3
  .metric     .estimator .estimate
  <chr>       <chr>          <dbl>
1 accuracy    binary         0.762
2 sens        binary         0.429
3 spec        binary         0.929
4 ppv         binary         0.75 
5 npv         binary         0.765
6 roc_auc     binary         0.806
7 brier_class binary         0.175

These metrics use only people who were not used to estimate the coefficients. The testing set, though, has only 21 people, 7 of them with the disease. A sensitivity computed from 7 cases can only take 8 possible values, and one person more or less changes it by about 14 percentage points.

With a dataset this small, repeated cross-validation gives a steadier estimate than one split.

Example 24.18  

ExampleRepeated cross-validation for the disease model

We use five-fold cross-validation, stratified on \(Y\) and repeated ten times. For AUC and the Brier score the choice of event level does not matter (they come out the same either way), so we can use the default metric settings.

set.seed(3386)

disease_folds <- vfold_cv(disease_data, v = 5, repeats = 10, strata = Y)

disease_cv <- fit_resamples(
  disease_workflow,
  resamples = disease_folds,
  metrics = metric_set(roc_auc, brier_class, accuracy)
)

collect_metrics(disease_cv)
# A tibble: 3 × 6
  .metric     .estimator  mean     n std_err .config        
  <chr>       <chr>      <dbl> <int>   <dbl> <chr>          
1 accuracy    binary     0.682    50 0.0121  pre0_mod0_post0
2 brier_class binary     0.196    50 0.00554 pre0_mod0_post0
3 roc_auc     binary     0.735    50 0.0156  pre0_mod0_post0
in_sample <- tibble(
  .metric = c("roc_auc", "brier_class", "accuracy"),
  .estimate = c(
    roc_auc_vec(disease_predictions$Y, disease_predictions$.pred_1,
                event_level = "second"),
    brier_class_vec(disease_predictions$Y, disease_predictions$.pred_1,
                    event_level = "second"),
    accuracy_vec(disease_predictions$Y, disease_predictions$.pred_class)
  )
)

collect_metrics(disease_cv, summarize = FALSE) |>
  ggplot(aes(x = .estimate, y = .metric)) +
  geom_jitter(height = 0.15, alpha = 0.4) +
  geom_point(data = in_sample, color = "#D55E00", size = 4, shape = 18) +
  labs(
    x = "Estimate",
    y = NULL,
    title = "Fold-level metrics from 10 repeats of 5-fold CV",
    subtitle = "Orange diamond = in-sample value"
  ) +
  theme_minimal()

Dot plot of AUC, Brier score, and accuracy from 50 cross-validation folds for the disease model, showing wide spread across folds.

The cross-validated AUC is a little lower than the in-sample AUC, and the cross-validated Brier score is a little higher, as we would expect: the model does somewhat worse on people it was not fit to. The fold-to-fold spread is large. Each fold has about 20 people and 6 cases, so any single fold, or any single train-test split, could have given a much better or much worse impression of the model.

24.9 Things to Watch Out For

WarningCommon mistakes with logistic regression and classification
  • Getting the event level wrong. glm() models the second factor level; yardstick treats the first level as the event unless event_level = "second" is set. A mistake swaps sensitivity with specificity and PPV with NPV, and turns an AUC of 0.8 into 0.2.
  • Judging a model by accuracy alone. Compare it with the no-information rate, the proportion in the larger class (Example 24.11).
  • Using 0.5 as the threshold without thinking about the costs of the two kinds of error (Example 24.13).
  • Choosing the threshold on the test data. The threshold is a tuning parameter; choose it with training data or cross-validation.
  • Treating AUC as a measure of whether the probabilities are right. AUC measures ranking only; check calibration too (Example 24.15).
  • Carrying PPV and NPV over to a population with a different prevalence (Example 24.16).
  • Reporting an odds ratio without saying what it was adjusted for, or comparing odds ratios from models with different predictors (Example 24.8).
  • Interpreting the main-effect coefficient as “the” effect of a predictor that is in an interaction (Example 24.7).
  • Fitting too many predictors for the number of events. With 31 cases, this dataset supports only a few coefficients, and adding more invites the separation problems described in the previous chapter.
  • Reading odds ratios from an observational study as causal effects.

24.10 Recap

This chapter fit, tested, and evaluated a multiple logistic regression model with tidymodels.

Idea Meaning
Multiple logistic regression Logistic regression with more than one predictor.
Linear predictor The logit-scale fitted value \(\eta_i=\mathbf{x}_i^\prime\boldsymbol{\beta}\).
Partial odds ratio The multiplicative change in odds for one predictor, holding the other predictors fixed.
Odds-ratio confidence interval A range of plausible values for a population odds ratio, found by exponentiating the interval for \(\beta_j\).
Reference group The group with all of its indicator variables equal to 0.
Empirical logit plot Logits of observed proportions in groups of a predictor, used to check linearity on the logit scale.
Likelihood ratio test Compares nested models by the drop in deviance, with a \(\chi^2\) reference distribution; the counterpart of the partial \(F\) test.
Effect plot Fitted probabilities plotted against a predictor, with separate curves for groups.
Interaction A term that lets the odds ratio for one predictor depend on another.
Non-collapsibility Odds ratios change when a predictor related to the response is added, even if it is unrelated to the other predictors.
Predicted probability The fitted probability that an observation is in the event class.
Predicted class The class assigned after applying a threshold to the predicted probability.
Confusion matrix A table of predicted classes against observed classes.
False positive / false negative Predicted event when there is none / predicted no event when there is one.
Sensitivity / specificity The proportion of events / non-events classified correctly.
PPV / NPV The proportion of predicted events / non-events that are correct; both depend on prevalence.
No-information rate The accuracy of always predicting the larger class.
Threshold The probability cutoff used to convert probabilities into classes.
Cost-based threshold \(c_{FP}/(c_{FP} + c_{FN})\), the threshold that minimizes expected cost when the probabilities are calibrated.
ROC curve Sensitivity against false positive rate across all thresholds.
AUC The probability that a random event gets a higher predicted probability than a random non-event.
Calibration Agreement between predicted probabilities and observed event rates.
Brier score The mean squared difference between predicted probabilities and 0/1 outcomes.
Event level The class treated as the event when computing classification metrics.
In-sample evaluation Evaluating predictions on the data used to fit the model; optimistic.
Train-test and cross-validation Evaluating predictions on data not used to fit the model.

24.11 Check your understanding

NoteProblems
  1. How does multiple logistic regression extend simple logistic regression?

  2. What does it mean to interpret a coefficient while holding other predictors fixed?

  3. Why is an indicator variable needed for middle socioeconomic class but not for upper socioeconomic class in this example?

  4. What is an empirical logit plot, and why is it more useful than a scatterplot of \(Y\) against age?

  5. The age coefficient is 0.0298. What is the odds ratio for a 10-year difference in age?

  6. Why does testing whether socioeconomic class matters require a test of two coefficients, and what test is used?

  7. In Example 24.6, the sector odds ratio is the same at every age. Why does the difference in probability between the sectors change with age?

  8. In a model with an age-by-sector interaction, what does \(\exp(\hat\beta_{X1})\) represent?

  9. Why might the sector odds ratio change when age and class are added to the model? Give two reasons.

  10. What is the event class in the disease outbreak example, and why must it be stated explicitly in yardstick?

  11. What do type = "raw", type = "prob", and type = "class" return?

  12. What are a false positive and a false negative in the disease outbreak example?

  13. What do sensitivity, specificity, PPV, and NPV measure?

  14. The model’s in-sample accuracy is 0.71. Why is that less impressive than it sounds?

  15. Why might we lower the classification threshold below 0.5? Why might we raise it?

  16. A false negative costs 9 times as much as a false positive. If the probabilities are well calibrated, what threshold minimizes expected cost?

  17. Why is choosing the threshold that minimizes cost on the training data optimistic?

  18. What does an ROC curve show, and what are the points (0, 0) and (1, 1) on it?

  19. Give the probability interpretation of AUC.

  20. A colleague reports AUC = 0.22. What is the most likely explanation?

  21. Two models have the same AUC. Can one still be better? Explain using calibration.

  22. A test with sensitivity and specificity of 0.90 is used to screen a population with 1% prevalence. Roughly what fraction of positive results are true positives?

  23. Why are in-sample classification metrics not a clean estimate of future performance, and why can a single small testing set be unreliable?

  24. Why should the odds-ratio interpretations in this chapter not automatically be interpreted causally?

  1. It puts several predictors in the linear predictor. The model still uses a logit link, and \(\eta_i\) can include quantitative predictors, indicators, interactions, or transformations.

  2. The odds ratio for one predictor describes its association with the odds of the event when the other predictors are kept at the same values.

  3. Upper class is the reference group. With three classes, two indicators are enough; when both are 0, the person is upper class.

  4. It groups observations by the predictor, converts each group’s observed proportion to a logit, and plots the logits against the group means. A plot of 0s and 1s shows almost nothing about the shape of the relationship, while the empirical logits show whether the log-odds change linearly with the predictor.

  5. \(\exp(10 \times 0.0298) = \exp(0.298) \approx 1.35\).

  6. Class is one variable represented by two indicators, so “class has no effect” means both coefficients are zero. The likelihood ratio (drop-in-deviance) test compares the model with and without both indicators, using a \(\chi^2\) distribution with 2 degrees of freedom.

  7. The same multiplicative change in odds corresponds to different changes in probability depending on where on the S-curve the starting probability is. Probabilities near 0 change little in absolute terms; probabilities near the middle change the most.

  8. The odds ratio for a one-year increase in age in sector 1 (where \(X_4 = 0\)). In sector 2 it is \(\exp(\hat\beta_{X1} + \hat\beta_{X1:X4})\).

  9. Confounding: if sector is related to age or class, part of the unadjusted sector difference reflects those variables. Non-collapsibility: even predictors unrelated to sector change the sector odds ratio if they are related to the response.

  10. Disease present, \(Y = 1\). yardstick treats the first factor level as the event by default, and here the first level is 0, so without event_level = "second", sensitivity and specificity (and PPV and NPV) would be swapped.

  11. "raw" returns the linear predictor on the logit scale, "prob" returns a probability column for each class, and "class" returns the predicted class using a 0.5 threshold.

  12. A false positive is a person predicted to have the disease who does not. A false negative is a person predicted not to have it who does.

  13. Sensitivity is the fraction of people with the disease who are predicted to have it. Specificity is the fraction without the disease who are predicted not to have it. PPV is the fraction of positive predictions that are correct, and NPV is the fraction of negative predictions that are correct.

  14. 68% of the people do not have the disease, so predicting “no disease” for everyone already has accuracy 0.68. The model improves on that by only a few points, and its sensitivity at 0.5 is low.

  15. Lower the threshold to catch more true cases when false negatives are costly (higher sensitivity, lower specificity). Raise it to avoid false alarms when false positives are costly.

  16. \(c_{FP}/(c_{FP} + c_{FN}) = 1/(1 + 9) = 0.10\).

  17. The threshold was chosen to fit the particular errors in those data. Like any tuning parameter chosen and evaluated on the same data, it will look better than it will perform on new data.

  18. It shows sensitivity against the false positive rate as the threshold varies. (0, 0) is a threshold so high that nothing is classified positive; (1, 1) is a threshold so low that everything is.

  19. It is the probability that a randomly chosen person with the event gets a higher predicted probability than a randomly chosen person without it, with ties counted as one half.

  20. The event level is probably reversed, so the metric is treating the probability of the non-event as the score for the event. Reversing it gives AUC = 0.78.

  21. Yes. AUC depends only on the ranking of the predictions. One model’s probabilities can match observed rates while the other’s are systematically too high or too low; a calibration plot or the Brier score shows the difference.

  22. \(\text{PPV} = (0.9 \times 0.01)/(0.9 \times 0.01 + 0.1 \times 0.99) \approx 0.083\), so fewer than 1 in 10.

  23. The model was fit to those same observations, so in-sample metrics are optimistic. A small testing set has few events, so metrics like sensitivity can change a lot with one or two observations; repeated cross-validation gives a steadier estimate.

  24. The data are observational. Logistic regression estimates associations after adjusting for the included predictors, and people in different sectors or classes may differ in ways the model does not include. Causal conclusions need a stronger design or stronger assumptions.