---
title: "Lecture 15: Observational Causal Inference - Example Setup"
author: "MS&E 226"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 4)
```

## Example: Treatment and Disease Severity

This notebook sets up the motivating example for Lecture 15, demonstrating confounding and the unconfoundedness assumption.

## Model Parameters

We set up a data generating process where:
- Age $X_1$ and HBP status $X_2$ both affect treatment assignment (via propensity score) and disease severity
- Treatment $W$ reduces severity
- This creates confounding between $W$ and $Y$

```{r parameters}
# Outcome model parameters
beta0 <- 70       # Baseline severity
beta_W <- -5      # Treatment effect (ATE)
beta_X1 <- 1      # Age effect on severity (positive = older people more severe)
beta_X2 <- 10     # HBP effect on severity (positive = HBP increases severity)
sigma <- 2        # Noise standard deviation

# Propensity score parameters
# e(X1, X2) = 1 / (1 + exp(-(alpha0 + alpha1*X1 + alpha2*X2)))
alpha0 <- -4.2    # Propensity score intercept
alpha1 <- 0.12    # Age coefficient (positive = older people more likely treated)
alpha2 <- 0.2    # HBP coefficient (positive = HBP patients more likely treated)

# Covariate distributions
X1_min <- 20      # Minimum age
X1_max <- 50      # Maximum age
p_hbp <- 0.1 # P(X2 = 1)

cat("Parameter Summary:\n")
cat("β₀ =", beta0, "(baseline severity)\n")
cat("β_W =", beta_W, "(treatment effect = ATE)\n")
cat("β_X1 =", beta_X1, "(age effect)\n")
cat("β_X2 =", beta_X2, "(HBP effect)\n")
cat("α₀ =", alpha0, "(propensity intercept)\n")
cat("α₁ =", alpha1, "(age coefficient in propensity)\n")
cat("α₂ =", alpha2, "(HBP coefficient in propensity)\n")
cat("σ =", sigma, "(noise SD)\n")
```

## Define Functions

```{r functions}
# Propensity score function: e(X1, X2) = P(W=1|X1, X2)
propensity_score <- function(x1, x2) {
  1 / (1 + exp(-alpha0 - alpha1 * x1 - alpha2 * x2))
}

# CATE function (constant in this linear model)
CATE <- function(x1, x2) {
  beta_W  # Treatment effect doesn't vary with X in this model
}

# ATE
ATE <- beta_W
cat("Average Treatment Effect (ATE) =", ATE, "\n")
```

## Calculate Propensity Score Range

```{r propensity_range}
# Minimum propensity score: X1 = 20, X2 = 0
e_min <- propensity_score(20, 0)
cat(sprintf("Minimum propensity score e(20, 0) = %.3f\n", e_min))

# Maximum propensity score: X1 = 50, X2 = 1
e_max <- propensity_score(50, 1)
cat(sprintf("Maximum propensity score e(50, 1) = %.3f\n", e_max))
```

## Visualizations

```{r visualizations, message=FALSE}
library(ggplot2)
library(gridExtra)

# Generate X1 values for plotting
x1_vals <- seq(20, 50, length.out = 200)

# Plot: Propensity score for X2 = 0 (no HBP) and X2 = 1 (HBP)
df_ps <- data.frame(
  X1 = rep(x1_vals, 2),
  X2 = rep(c(0, 1), each = length(x1_vals)),
  HBP = rep(c("No HBP (X2=0)", "HBP (X2=1)"), each = length(x1_vals))
)
df_ps$e_X <- propensity_score(df_ps$X1, df_ps$X2)

p1 <- ggplot(df_ps, aes(x = X1, y = e_X, color = HBP, linetype = HBP)) +
  geom_line(linewidth = 1.2) +
  scale_color_manual(values = c("steelblue", "darkred")) +
  labs(title = "Propensity Score e(X1, X2) = P(W=1|X1, X2)",
       x = "Age (X1)",
       y = "P(W = 1 | X1, X2)",
       color = "",
       linetype = "") +
  theme_minimal() +
  ylim(0, 1) +
  theme(legend.position = "bottom")

# Display plot
p1
```

## Generate Data and Run Regressions

```{r generate_data}
# Set seed for reproducibility
set.seed(226)

# Sample size
n <- 2000

# Generate age X1 from uniform distribution
X1 <- runif(n, min = X1_min, max = X1_max)

# Generate HBP indicator X2
X2 <- rbinom(n, size = 1, prob = p_hbp)

# Generate treatment W based on propensity score
e_X <- propensity_score(X1, X2)
W <- rbinom(n, size = 1, prob = e_X)

# Generate outcome Y
Y <- beta0 + beta_W * W + beta_X1 * X1 + beta_X2 * X2 + rnorm(n, mean = 0, sd = sigma)

# Create data frame
df <- data.frame(X1 = X1, X2 = X2, W = W, Y = Y)

# Summary statistics
cat(sprintf("Number of observations: %d\n", n))
cat(sprintf("Proportion with HBP (X2=1): %.3f\n", mean(X2)))
cat(sprintf("Proportion treated (W=1): %.3f\n", mean(W)))
cat(sprintf("Mean outcome: %.2f\n", mean(Y)))
```

## Regression Analysis

All regression tables below use HC3 robust standard errors.

### Regression with W only (biased)

```{r regression_w_only}
# Regression without controlling for X1 or X2
model_w_only <- lm(Y ~ W, data = df)
lmtest::coeftest(model_w_only, vcov. = sandwich::vcovHC(model_w_only, type = "HC3"))
```

### Regression with W and covariates (unbiased)

```{r regression_w_and_x}
# Regression controlling for X1 and X2
model_w_and_x <- lm(Y ~ W + X1 + X2, data = df)
lmtest::coeftest(model_w_and_x, vcov. = sandwich::vcovHC(model_w_and_x, type = "HC3"))
```

## IPW Estimation

### Step 1: Estimate propensity scores

```{r estimate_propensity}
# Fit logistic regression to predict treatment from features
propensity_model <- glm(W ~ X1 + X2, data = df, family = binomial())

# Get predicted propensity scores
df$e_hat <- predict(propensity_model, type = "response")

# Summary of estimated propensity scores
cat("Estimated propensity score summary:\n")
summary(df$e_hat)

# Check range
cat(sprintf("\nRange: [%.3f, %.3f]\n", min(df$e_hat), max(df$e_hat)))
```

### Step 2: Compute IPW estimator

```{r compute_ipw}
# Compute IPW estimator
# E[Y(1)] estimate: weighted treated sum divided by n
E_Y1_hat <- sum(df$Y[df$W == 1] / df$e_hat[df$W == 1]) / nrow(df)

# E[Y(0)] estimate: weighted control sum divided by n
E_Y0_hat <- sum(df$Y[df$W == 0] / (1 - df$e_hat[df$W == 0])) / nrow(df)

# IPW estimate of ATE
ATE_IPW <- E_Y1_hat - E_Y0_hat

cat("IPW Estimation Results:\n")
cat(sprintf("Estimated E[Y(1)] = %.3f\n", E_Y1_hat))
cat(sprintf("Estimated E[Y(0)] = %.3f\n", E_Y0_hat))
cat(sprintf("IPW estimate of ATE = %.3f\n", ATE_IPW))
cat(sprintf("\nTrue ATE = %.3f\n", ATE))
cat(sprintf("Estimation error = %.3f\n", ATE_IPW - ATE))
```

## Diagnostic Criteria for IPW

### 1. Histogram Overlap

Check that propensity score distributions overlap between treated and control groups.

```{r histogram_overlap, message=FALSE}
library(ggplot2)

# Create data frame with propensity scores and treatment status
df_prop <- data.frame(
  e_hat = df$e_hat,
  Treatment = ifelse(df$W == 1, "Treated (W=1)", "Control (W=0)")
)

# Plot overlapping histograms
ggplot(df_prop, aes(x = e_hat, fill = Treatment)) +
  geom_histogram(alpha = 0.5, bins = 30, position = "identity") +
  scale_fill_manual(values = c("Control (W=0)" = "steelblue",
                                "Treated (W=1)" = "darkred")) +
  labs(title = "Propensity Score Distribution by Treatment Status",
       x = "Estimated Propensity Score",
       y = "Count",
       fill = "") +
  theme_minimal() +
  theme(legend.position = "bottom")

# Summary statistics by group
cat("\nPropensity score summary by treatment group:\n")
cat("\nTreated (W=1):\n")
summary(df$e_hat[df$W == 1])
cat("\nControl (W=0):\n")
summary(df$e_hat[df$W == 0])
```

### 2. Covariate Balance

Check that weighted covariate distributions are balanced between treated and control groups.

```{r covariate_balance}
# Function to calculate standardized mean difference
calc_smd <- function(x_treat, x_control, weights_treat = NULL, weights_control = NULL) {
  if (is.null(weights_treat)) weights_treat <- rep(1, length(x_treat))
  if (is.null(weights_control)) weights_control <- rep(1, length(x_control))

  mean_treat <- weighted.mean(x_treat, weights_treat)
  mean_control <- weighted.mean(x_control, weights_control)

  var_treat <- weighted.mean((x_treat - mean_treat)^2, weights_treat)
  var_control <- weighted.mean((x_control - mean_control)^2, weights_control)

  pooled_sd <- sqrt((var_treat + var_control) / 2)

  smd <- (mean_treat - mean_control) / pooled_sd
  return(smd)
}

# Calculate IPW weights
df$ipw_weight <- ifelse(df$W == 1, 1/df$e_hat, 1/(1 - df$e_hat))

# Unweighted statistics
smd_X1_unweighted <- calc_smd(df$X1[df$W == 1], df$X1[df$W == 0])
smd_X2_unweighted <- calc_smd(df$X2[df$W == 1], df$X2[df$W == 0])

# IPW-weighted statistics
smd_X1_weighted <- calc_smd(df$X1[df$W == 1], df$X1[df$W == 0],
                            df$ipw_weight[df$W == 1], df$ipw_weight[df$W == 0])
smd_X2_weighted <- calc_smd(df$X2[df$W == 1], df$X2[df$W == 0],
                            df$ipw_weight[df$W == 1], df$ipw_weight[df$W == 0])

# Create balance table
balance_table <- data.frame(
  Covariate = c("Age (X1)", "HBP (X2)"),
  SMD_Unweighted = c(smd_X1_unweighted, smd_X2_unweighted),
  SMD_Weighted = c(smd_X1_weighted, smd_X2_weighted)
)

cat("Covariate Balance Assessment:\n")
cat("(Standardized Mean Differences between treated and control)\n\n")
print(balance_table, row.names = FALSE)

cat("\nInterpretation:\n")
cat("- SMD < 0.1 is generally considered good balance\n")
cat("- IPW weighting should reduce SMD (bring closer to 0)\n")
cat("- Large weighted SMD indicates poor overlap or model misspecification\n")
```
