# ============================================================================
# POL 213 — Week 9: Causal Inference with MLE & ML
# ============================================================================
#
# Two methods on simulated data with a known true effect (+0.5 SD):
#   - Propensity-score matching for the ATE
#   - Causal forest for HTE


# ---- Setup ----

library(tidyverse)
library(juanr)
library(broom)
library(modelsummary)
library(socviz)         # gss_sm
library(cobalt)         # bal.tab(), love.plot()
library(MatchIt)        # matchit()
library(grf)            # causal_forest()


# ---- Simulated "JobTrain 2024" data ----
#
# True ATE = +0.5. Selection: younger/less-educated/female are more likely
# to enroll. Same variables also drive baseline Y → confounding.

set.seed(213)
gss_train = gss_sm |>
  filter(!is.na(age), !is.na(degree), !is.na(sex), !is.na(race)) |>
  mutate(
    deg_num = as.numeric(degree),                # 1 = Lt HS … 5 = Graduate
    female  = as.integer(sex == "Female"),
    eta_T   = 1.5 + 0.7 * female - 0.04 * age - 0.6 * deg_num,
    p_T     = plogis(eta_T),
    T_prog  = rbinom(n(), 1, p_T),
    Y0      = 1.2 * (deg_num - 3) - 0.5 * female + 0.02 * (age - 45) +
              rnorm(n(), 0, 0.5),
    Y1      = Y0 + 0.5,
    Y       = T_prog * Y1 + (1 - T_prog) * Y0)




# ============================================================================
# PART 1: NAIVE ESTIMATE
# ============================================================================

mod_naive = lm(Y ~ T_prog, data = gss_train)
tidy(mod_naive, conf.int = TRUE) |> filter(term == "T_prog")
# Naive coefficient is strongly negative — truth is +0.5.

# Why? Treated/control aren't comparable.
gss_train |>
  group_by(T_prog) |>
  summarise(n = n(), mean_age = mean(age),
            pct_female = mean(female), mean_degree = mean(deg_num))




# ============================================================================
# PART 2: BALANCE AND PROPENSITY SCORES
# ============================================================================

# Pre-match balance diagnostics
bt = bal.tab(T_prog ~ age + female + deg_num + race,
             data = gss_train, estimand = "ATT")
print(bt)

love.plot(bt, threshold = 0.1, abs = FALSE)


# Propensity-score logit
mod_ps = glm(T_prog ~ age + female + deg_num + race,
             data = gss_train, family = binomial)

gss_train = gss_train |>
  mutate(pscore = predict(mod_ps, type = "response"))


# Overlap check
ggplot(gss_train, aes(pscore, fill = factor(T_prog))) +
  geom_histogram(bins = 40, alpha = 0.7, position = "identity") +
  labs(x = "Propensity score π̂(X)", y = "count", fill = "T") +
  theme_minimal()




# ============================================================================
# PART 3: PROPENSITY-SCORE MATCHING
# ============================================================================

# ---- 3a. Logit propensity → 1:1 nearest-neighbor match ----

m_psm = matchit(T_prog ~ age + female + deg_num + race,
                data = gss_train,
                method = "nearest", distance = "glm", ratio = 1)

love.plot(m_psm, threshold = 0.1, abs = FALSE)   # post-match balance

matched = match.data(m_psm)
mod_psm = lm(Y ~ T_prog, data = matched)

modelsummary(list("Naive (full sample)"  = mod_naive,
                  "PSM (matched sample)" = mod_psm),
             stars = TRUE, gof_omit = "IC|Log|Adj|F|RMSE")


# ---- 3b. Random-forest propensity ----
#
# Swap distance = "glm" → "randomforest". Same pipeline.
# install.packages("randomForest")  # one-time if missing

m_rf = matchit(T_prog ~ age + female + deg_num + race,
               data = gss_train,
               method = "nearest", distance = "randomforest", ratio = 1)

mod_psm_rf = lm(Y ~ T_prog, data = match.data(m_rf))


# ---- 3c. Coarsened exact matching ----

m_cem   = matchit(T_prog ~ age + female + deg_num + race,
                  data = gss_train, method = "cem")
mod_cem = lm(Y ~ T_prog, data = match.data(m_cem))


# Compare across methods
modelsummary(list("Naive"     = mod_naive,
                  "PSM logit" = mod_psm,
                  "PSM RF"    = mod_psm_rf,
                  "CEM"       = mod_cem),
             stars = TRUE, gof_omit = "IC|Log|Adj|F|RMSE")




# ============================================================================
# PART 4: MATCHING VS CONTROLLING
# ============================================================================

mod_regression     = lm(Y ~ T_prog + age + female + deg_num + race,
                        data = gss_train)
mod_match_then_reg = lm(Y ~ T_prog + age + female + deg_num + race,
                        data = matched)

modelsummary(list("Naive"               = mod_naive,
                  "OLS with controls"   = mod_regression,
                  "PSM only"            = mod_psm,
                  "PSM + controls (DR)" = mod_match_then_reg),
             stars = TRUE, gof_omit = "IC|Log|Adj|F|RMSE",
             coef_omit = "^(?!T_prog)")          # show only the treatment row




# ============================================================================
# IN-CLASS ACTIVITY: your turn (see slides)
# ============================================================================
#
# Walk the full PSM pipeline on juanr::affairs:
#
#   - Outcome: n_affairs
#   - Treatment: kids (1 = has children, 0 = none)
#   - Confounders: age, years_married, marriage_rating, educ, male, relig
#
# Steps: pre-match balance → match → post-match balance → ATE (naive vs matched)




# ============================================================================
# PART 5: HETEROGENEOUS TREATMENT EFFECTS (CAUSAL FOREST)
# ============================================================================

# grf wants atomic vectors for Y and W
X_mat = model.matrix(~ age + female + deg_num + race - 1, data = gss_train)
Y_vec = as.numeric(gss_train$Y)
W_vec = as.integer(gss_train$T_prog)

set.seed(213)
cf = causal_forest(X = X_mat, Y = Y_vec, W = W_vec, num.trees = 2000)

# ATE from the forest (matches PSM)
average_treatment_effect(cf)

# Per-observation predicted treatment effect
gss_train$tau_hat = predict(cf)$predictions
summary(gss_train$tau_hat)


# Distribution of τ̂(X)
ggplot(gss_train, aes(tau_hat)) +
  geom_histogram(bins = 40, fill = "#022851", color = "white") +
  geom_vline(xintercept = 0.5, color = "#B8611A", linetype = "dashed") +
  labs(x = expression(hat(tau)~"(X)"), y = "count") +
  theme_minimal()


# τ̂ as a function of a covariate (flat = no heterogeneity)
ggplot(gss_train, aes(deg_num, tau_hat)) +
  geom_jitter(width = 0.15, alpha = 0.3) +
  geom_smooth(method = "loess") +
  geom_hline(yintercept = 0.5, linetype = "dashed", color = "#B8611A") +
  labs(x = "Degree", y = expression(hat(tau)~"(X)")) +
  theme_minimal()


# Variable importance
tibble(var        = colnames(X_mat),
       importance = as.numeric(variable_importance(cf))) |>
  arrange(desc(importance))
