# ============================================================================
# POL 213 — Week 3: Binary Outcomes and the Logit Model
# ============================================================================

# ---- Setup ----

library(tidyverse)
library(juanr)
library(broom)
library(modelsummary)
library(estimatr)
library(marginaleffects)

# 2020 Cooperative Election Study: binary policy outcomes + Protestant indicator
# (CES has no native "evangelical" indicator; Protestant is the closest proxy)
bot_clean = bot |>
  filter(
    sex %in% c("Male", "Female"),
    aca     %in% c("Support", "Oppose"),
    wall    %in% c("Support", "Oppose"),
    conceal %in% c("Support", "Oppose"),
    minwage %in% c("Favor", "Oppose"),
    !religion %in% c("skipped", "not asked"),
    !is.na(religion),
    !newsint %in% c("skipped", "not asked"),
    !is.na(age)
  ) |>
  mutate(
    protestant = factor(
      ifelse(religion == "Protestant", "Protestant", "Non-Protestant"),
      levels = c("Non-Protestant", "Protestant")
    ),
    # all four outcomes coded so 1 = more conservative position
    oppose_aca     = as.numeric(aca     == "Oppose"),
    wall           = as.numeric(wall    == "Support"),
    conceal        = as.numeric(conceal == "Support"),
    oppose_minwage = as.numeric(minwage == "Oppose")
  ) |>
  select(protestant, oppose_aca, wall, conceal, oppose_minwage,
         age, sex, newsint)

# take a look
bot_clean


# ============================================================================
# Estimating effects by hand
# ============================================================================

# ---- Group means ----

# for binary Y, mean = proportion = Pr(Y = 1)
group_means = bot_clean |>
  group_by(protestant) |>
  summarize(
    `Oppose ACA`      = mean(oppose_aca),
    `Support wall`    = mean(wall),
    `Support conceal` = mean(conceal),
    `Oppose min wage` = mean(oppose_minwage),
    `% of sample`     = 100 * n() / nrow(bot_clean)
  )
group_means


# ---- The difference-in-means estimator ----

# classic estimator: Pr(Y | Protestant) - Pr(Y | Non-Protestant)
# pivot_longer(): reshape wide -> long (one row per obs-variable pair)
# pivot_wider():  reshape long -> wide (spread one column's values across new columns)
# here we go long then wide to flip the Protestant/Non-Protestant rows into columns
by_hand = group_means |>
  select(-`% of sample`) |>
  pivot_longer(-protestant, names_to = "Outcome", values_to = "share") |>
  pivot_wider(names_from = protestant, values_from = share) |>
  mutate(Difference = Protestant - `Non-Protestant`)
by_hand


# ---- Standard errors by hand (two-sample formula) ----

# SE(p1 - p0) = sqrt( p1(1-p1)/n1 + p0(1-p0)/n0 )
n_P  = sum(bot_clean$protestant == "Protestant")
n_NP = sum(bot_clean$protestant == "Non-Protestant")

by_hand_se = by_hand |>
  mutate(
    SE = sqrt(Protestant * (1 - Protestant) / n_P +
              `Non-Protestant` * (1 - `Non-Protestant`) / n_NP),
    z  = Difference / SE
  )
by_hand_se


# ============================================================================
# Regression recovers the same estimate
# ============================================================================

# ---- Classical lm ----

# OLS with binary Y and binary X reproduces the difference-in-means
mod_aca = lm(oppose_aca ~ protestant, data = bot_clean)
tidy(mod_aca)

# the coefficient on protestantProtestant matches by_hand$Difference exactly;
# the SE is close but not identical (the LPM is heteroskedastic by construction)


# ---- Residual variance from the model (broom approach) ----

# sigma^2 = SSR / (n - k), the pooled residual variance that classical OLS uses
augment(mod_aca) |>
  summarize(SSR    = sum(.resid^2),
            df     = n() - 2,
            sigma2 = SSR / df)

# same number the built-in sigma() function returns
sigma(mod_aca)^2


# ---- Group-specific residual variances ----

# for binary Y, Var(Y | group) = p(1 - p), differs by group by construction
bot_clean |>
  group_by(protestant) |>
  summarize(p_hat = mean(oppose_aca),
            var_g = p_hat * (1 - p_hat),
            n_g   = n())


# ---- Robust (HC2) SE, computed by hand ----

# use each group's own variance --- this is the two-sample SE formula
bot_clean |>
  group_by(protestant) |>
  summarize(p_hat = mean(oppose_aca),
            var_g = p_hat * (1 - p_hat),
            n_g   = n()) |>
  summarize(SE_HC2 = sqrt(sum(var_g / n_g)))


# ---- HC2 via lm_robust() ----

# estimatr::lm_robust() automates HC standard errors
mod_aca_rb = lm_robust(oppose_aca ~ protestant,
                       data = bot_clean, se_type = "HC2")
tidy(mod_aca_rb)

# classical vs HC2 side by side
# bind_rows(): stack data frames vertically (like rbind() but matches columns by name)
bind_rows(
  tidy(mod_aca)    |> mutate(Method = "Classical (lm)"),
  tidy(mod_aca_rb) |> mutate(Method = "HC2 robust (lm_robust)")
) |>
  filter(term == "protestantProtestant") |>
  select(Method, estimate, std.error)


# ---- All four outcomes, side-by-side ----

# classical SEs
mods = list(
  `Oppose ACA`      = lm(oppose_aca     ~ protestant, data = bot_clean),
  `Support wall`    = lm(wall           ~ protestant, data = bot_clean),
  `Support conceal` = lm(conceal        ~ protestant, data = bot_clean),
  `Oppose min wage` = lm(oppose_minwage ~ protestant, data = bot_clean)
)
modelsummary(
  mods, stars = TRUE,
  coef_map = c("protestantProtestant" = "Protestant",
               "(Intercept)"          = "Intercept"),
  gof_omit = "IC|Log|Adj|F|RMSE|R2"
)

# robust (HC2) SEs
mods_rb = list(
  `Oppose ACA`      = lm_robust(oppose_aca     ~ protestant, data = bot_clean, se_type = "HC2"),
  `Support wall`    = lm_robust(wall           ~ protestant, data = bot_clean, se_type = "HC2"),
  `Support conceal` = lm_robust(conceal        ~ protestant, data = bot_clean, se_type = "HC2"),
  `Oppose min wage` = lm_robust(oppose_minwage ~ protestant, data = bot_clean, se_type = "HC2")
)
modelsummary(
  mods_rb, stars = TRUE,
  coef_map = c("protestantProtestant" = "Protestant",
               "(Intercept)"          = "Intercept"),
  gof_omit = "IC|Log|Adj|F|RMSE|R2"
)


# ============================================================================
# Problems with the Linear Probability Model
# ============================================================================

# ---- Binary outcomes look like two bars ----

bot_clean |>
  count(oppose_aca) |>
  mutate(prop = n / sum(n)) |>
  ggplot(aes(x = factor(oppose_aca, labels = c("0 (support)", "1 (oppose)")),
             y = prop)) +
  geom_col(fill = "#022851", width = 0.55) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(x = "oppose_aca", y = NULL,
       title = "A Bernoulli distribution: two bars, one parameter p")


# ---- Problem #1: LPM predictions can escape [0, 1] ----

# multi-predictor LPM on affairs (Fair 1978)
a = affairs |> mutate(had_affair = as.numeric(n_affairs > 0))
lpm_aff = lm(had_affair ~ marriage_rating + years_married + relig, data = a)

# how many observations get predicted Pr < 0 or > 1?
aug = augment(lpm_aff)
aug |> summarize(n_below_0 = sum(.fitted < 0),
                 n_above_1 = sum(.fitted > 1),
                 min_pred  = min(.fitted),
                 max_pred  = max(.fitted))

# histogram of fitted values; highlight the below-0 tail
aug |>
  mutate(bad = .fitted < 0) |>
  ggplot(aes(.fitted, fill = bad)) +
  geom_histogram(binwidth = 0.04, color = "white", linewidth = 0.2) +
  geom_vline(xintercept = c(0, 1), linetype = 2, color = "#022851") +
  scale_fill_manual(values = c("FALSE" = "#6c757d", "TRUE" = "#B8611A"),
                    guide = "none") +
  labs(x = "Predicted Pr(affair) from LPM", y = "Count")


# ---- Problem #2: constant slopes don't respect bounds ----

# LPM's coefficient IS the marginal effect --- one number for everyone
beta_mr = coef(lm(had_affair ~ marriage_rating, data = a))["marriage_rating"]
beta_mr

# apply this same effect to different baselines
tibble(
  `Baseline Pr` = c(0.05, 0.30, 0.50, 0.70, 0.95),
  `LPM effect`  = beta_mr,
  `New Pr`      = round(`Baseline Pr` + `LPM effect`, 2)
)

# at baseline 0.05, the LPM predicts -0.05 --- impossible
# this is problem #1 seen from a different angle: a constant slope *must*
# eventually cross 0 or 1


# ============================================================================
# Logit: a bounded linear model
# ============================================================================

# ---- The logistic (sigmoid) function ----

# maps any real number into [0, 1], via: sigma(z) = 1 / (1 + exp(-z))
# plogis() computes sigma() in R
tibble(z = seq(-6, 6, length.out = 400), p = plogis(z)) |>
  ggplot(aes(z, p)) +
  geom_hline(yintercept = c(0, 1), linetype = 2, color = "grey60") +
  geom_line(color = "#022851", linewidth = 1.3) +
  scale_y_continuous(breaks = c(0, 0.5, 1)) +
  labs(x = expression(z == X * beta), y = expression(sigma(z)))


# ---- Fit a logit model ----

# glm with family = binomial() uses the logit link by default
mod_logit = glm(wall ~ protestant, data = bot_clean, family = binomial())
tidy(mod_logit)

# coefficients are in log-odds units --- not probability changes


# ---- Reading the coefficient: odds ratio ----

# exp(beta) = the multiplicative change in odds
exp(coef(mod_logit)["protestantProtestant"])
# Protestants have ~2.2x the odds of supporting the wall


# ---- Reading the coefficient: convert to probability via a scenario ----

# set specific covariate values, push through the model
scenarios = crossing(protestant = c("Non-Protestant", "Protestant"))

augment(mod_logit, newdata = scenarios, type.predict = "response") |>
  select(protestant, `Pr(wall)` = .fitted)

# under the hood: apply plogis() to each group's X*beta
plogis(coef(mod_logit)["(Intercept)"])                                          # Non-Protestant
plogis(coef(mod_logit)["(Intercept)"] + coef(mod_logit)["protestantProtestant"]) # Protestant

# these probabilities match the by-hand group means exactly


# ============================================================================
# Marginal effects in logit
# ============================================================================

# ---- Model with a continuous control ----

# logit of wall support on Protestant + age
mod_wall = glm(wall ~ protestant + age, data = bot_clean, family = binomial())
tidy(mod_wall)


# ---- p * (1 - p) is the sigmoid's slope ----

# dP/dX = p(1-p) * beta  (chain rule: X -> z -> P)
# so the marginal effect depends on the current predicted probability
tibble(p = seq(0, 1, length.out = 200), scaling = p * (1 - p)) |>
  ggplot(aes(p, scaling)) +
  geom_line(color = "#022851", linewidth = 1.3) +
  labs(x = "current predicted probability p",
       y = expression(p %*% (1 - p)),
       title = "ME scaling factor peaks at p = 0.5, shrinks at the edges")


# ---- ME of being Protestant, across ages ----

# comparisons(): marginaleffects function that computes contrasts — here the
# difference in predicted Pr(wall) between Protestant and Non-Protestant, with SEs
me_prot = comparisons(
  mod_wall, variables = "protestant",
  newdata = datagrid(age = seq(20, 85, by = 5))
)
me_prot

ggplot(me_prot, aes(x = age, y = estimate)) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high),
              alpha = 0.2, fill = "#B8611A") +
  geom_line(color = "#B8611A", linewidth = 1.3) +
  labs(x = "Age", y = "ME of being Protestant on Pr(support wall)")


# ---- Scenarios: set covariates explicitly, predict at each ----

# same crossing() + augment() workflow as weeks 1-2
scenario = crossing(
  protestant = c("Non-Protestant", "Protestant"),
  age        = c(20, 40, 60)
)

preds = augment(mod_wall, newdata = scenario,
                type.predict = "response", se_fit = TRUE)
preds

# visualize: X = protestant, color = age
# the slope of each line = the ME of being Protestant at that age
ggplot(preds, aes(x = protestant, y = .fitted,
                  color = factor(age), group = age)) +
  geom_line(linewidth = 1.3) +
  geom_errorbar(aes(ymin = .fitted - 1.96 * .se.fit,
                    ymax = .fitted + 1.96 * .se.fit),
                width = 0.04, linewidth = 0.8) +
  geom_point(size = 3.5) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(x = NULL, y = "Predicted Pr(support wall)", color = "Age")


# ---- Average marginal effect (one-number summary) ----

# averages the per-observation ME across the whole sample
avg_comparisons(mod_wall, variables = "protestant")

# AME ~= 0.165, nearly identical to the LPM coefficient on the same model
# (the LPM IS the linear approximation of the logit, averaged over the data)


# ============================================================================
# LPM vs. logit
# ============================================================================

# ---- No controls: identical predictions ----

mod_lpm        = lm(wall  ~ protestant, data = bot_clean)
mod_logit_simp = glm(wall ~ protestant, data = bot_clean, family = binomial())

tibble(protestant = c("Non-Protestant", "Protestant")) |>
  mutate(LPM   = predict(mod_lpm,        newdata = pick(everything())),
         Logit = predict(mod_logit_simp, newdata = pick(everything()),
                         type = "response"))

# identical --- with a single binary predictor, both models compute group means


# ---- With a continuous control: close but not identical ----

mod_lpm2   = lm(wall  ~ protestant + age, data = bot_clean)
mod_logit2 = glm(wall ~ protestant + age, data = bot_clean, family = binomial())

# expand_grid(): same idea as crossing() — all combinations of supplied vectors
# (tidyr's expand_grid() is the newer, stricter cousin of crossing())
expand_grid(protestant = c("Non-Protestant", "Protestant"),
            age        = c(20, 50, 80)) |>
  mutate(LPM   = predict(mod_lpm2,   newdata = pick(everything())),
         Logit = predict(mod_logit2, newdata = pick(everything()),
                         type = "response"),
         diff  = Logit - LPM)


# ---- LPM vs. logit visualized across a wide age range ----

# fit both models on wall ~ age (no protestant --- single predictor for clarity)
mod_lpm_age   = lm(wall  ~ age, data = bot_clean)
mod_logit_age = glm(wall ~ age, data = bot_clean, family = binomial())

# predictions across an extended age range to see divergence
grid = tibble(age = seq(0, 180, length.out = 400)) |>
  mutate(LPM   = predict(mod_lpm_age,   newdata = pick(everything())),
         Logit = predict(mod_logit_age, newdata = pick(everything()),
                         type = "response")) |>
  pivot_longer(c(LPM, Logit), names_to = "Model", values_to = "p_hat")

# binned means from the actual data, as reference
binned = bot_clean |>
  mutate(age_bin = cut(age, breaks = seq(17, 92, by = 5))) |>
  group_by(age_bin) |>
  summarize(age = mean(age), wall = mean(wall), .groups = "drop")

ggplot() +
  geom_hline(yintercept = c(0, 1), linetype = 2, color = "grey60") +
  geom_point(data = binned, aes(age, wall), color = "grey30", size = 2) +
  geom_line(data = grid, aes(age, p_hat, color = Model), linewidth = 1.3) +
  scale_color_manual(values = c("LPM" = "#B8611A", "Logit" = "#022851")) +
  scale_y_continuous(breaks = c(0, 0.5, 1)) +
  labs(x = "Age (extended past the data)", y = "Pr(support wall)")

# inside the data (age 18-90): both models make essentially the same predictions
# extrapolated: the LPM straight line crosses 1; logit stays bounded

# takeaway: LPM is a first-order (linear) approximation of logit's S-curve ---
# good enough when your data sit on the flat middle of the sigmoid, breaks
# down when they don't
