# ── Libraries (same as textbook) ─────────────────────────────────────────────
library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.4.1 ──
## ✔ broom        1.0.12     ✔ recipes      1.3.1 
## ✔ dials        1.4.2      ✔ rsample      1.3.2 
## ✔ dplyr        1.1.4      ✔ tailor       0.1.0 
## ✔ ggplot2      4.0.2      ✔ tidyr        1.3.1 
## ✔ infer        1.1.0      ✔ tune         2.0.1 
## ✔ modeldata    1.5.1      ✔ workflows    1.3.0 
## ✔ parsnip      1.4.1      ✔ workflowsets 1.1.1 
## ✔ purrr        1.2.1      ✔ yardstick    1.3.2
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
## ✖ recipes::step()  masks stats::step()
library(ISLR2)  # Default dataset is in ISLR2

# ── Make sure default is a factor (same reason as textbook for Direction) ─────
Default <- Default %>%
  mutate(default = factor(default),
         student = factor(student))

# ── Model Specification (copy of textbook style) ──────────────────────────────
lr_spec <- logistic_reg() %>%
  set_engine("glm") %>%
  set_mode("classification")

# ── Fit models for Table 4.1, 4.2, 4.3 (same as textbook fit style) ──────────

# Table 4.1 - balance
lr_fit1 <- lr_spec %>%
  fit(default ~ balance, data = Default)

# Table 4.2 - student
lr_fit2 <- lr_spec %>%
  fit(default ~ student, data = Default)

# Table 4.3 - balance + income + student
lr_fit3 <- lr_spec %>%
  fit(default ~ balance + income + student, data = Default)

# ── ROC Curve (using augment + roc_curve + autoplot, same as textbook) ────────
augment(lr_fit1, new_data = Default) %>%
  roc_curve(truth = default, .pred_Yes) %>%
  autoplot()

augment(lr_fit2, new_data = Default) %>%
  roc_curve(truth = default, .pred_Yes) %>%
  autoplot()

augment(lr_fit3, new_data = Default) %>%
  roc_curve(truth = default, .pred_Yes) %>%
  autoplot()