---
title: "Lecture 10: Bootstrap Examples"
author: "MS&E 226"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(tidyverse)
library(boot)
set.seed(226)
```

## Example 1 & 2: Flight Delays (Mean and Median)

First, we load the flight delays data and take a sample of 500 flights.

```{r flight-delays-sample}
# Sample size
n_sample = 500

# Load flight delays data
flights_raw <- readRDS("../../data/flights_2024/flights_2024_top_15_raw.rds")

# Process the data:
flights_processed <- flights_raw %>%
  mutate(
    cancelled = is.na(arr_time),
    valid = !is.na(arr_time) & !is.na(arr_delay)
  )
valid_flights <- flights_processed %>%
  filter(valid)

flight_sample <- sample(valid_flights$arr_delay, size = n_sample, replace = TRUE)

# Sample statistics
sample_mean <- mean(flight_sample)
sample_sd <- sd(flight_sample)
sample_median <- median(flight_sample)

cat("Sample size:", n_sample, "\n")
cat("Sample mean:", round(sample_mean, 2), "\n")
cat("Sample SD:", round(sample_sd, 2), "\n")
cat("Sample median:", round(sample_median, 2), "\n")
```

### Bootstrap for the sample mean

```{r bootstrap-mean}
# Using the boot package
mean_boot <- function(data, indices) {
  return(mean(data[indices]))
}

# Run bootstrap with B = 10000
boot_mean_result <- boot(flight_sample, mean_boot, R = 10000)

# Bootstrap SE
bootstrap_se_mean <- sd(boot_mean_result$t)

# Compare to CLT approximation
clt_se_mean <- sample_sd / sqrt(n_sample)

cat("Bootstrap SE for mean:", round(bootstrap_se_mean, 3), "\n")
cat("CLT SE for mean:", round(clt_se_mean, 3), "\n")

# Plot bootstrap distribution
boot_mean_df <- data.frame(boot_mean = boot_mean_result$t)

ggplot(boot_mean_df, aes(x = boot_mean)) +
  geom_histogram(bins = 50, fill = "steelblue", alpha = 0.7) +
  geom_vline(xintercept = sample_mean, color = "red", linewidth = 1) +
  labs(title = "Bootstrap Distribution of Sample Mean (Flight Delays)",
       x = "Sample Mean (minutes)",
       y = "Count") +
  theme_minimal()
```

### Bootstrap for the sample median

```{r bootstrap-median}
# Bootstrap function for median
median_boot <- function(data, indices) {
  return(median(data[indices]))
}

# Run bootstrap
boot_median_result <- boot(flight_sample, median_boot, R = 10000)

# Bootstrap SE
bootstrap_se_median <- sd(boot_median_result$t)

cat("Bootstrap SE for median:", round(bootstrap_se_median, 3), "\n")
cat("Sample median:", round(sample_median, 2), "\n")

# Plot bootstrap distribution
boot_median_df <- data.frame(boot_median = boot_median_result$t)

ggplot(boot_median_df, aes(x = boot_median)) +
  geom_histogram(bins = 50, fill = "darkgreen", alpha = 0.7) +
  geom_vline(xintercept = sample_median, color = "red", linewidth = 1) +
  labs(title = "Bootstrap Distribution of Sample Median (Flight Delays)",
       x = "Sample Median (minutes)",
       y = "Count") +
  theme_minimal()
```

## Example 3: Funnels (Survival Analysis)

Simulate checkout flow data with three stages.

```{r funnels}
# Simulate funnel data
n_customers <- 1000

# Probabilities:
# Stage 1 -> 2: 0.7
# Stage 2 -> 3: 0.5

simulate_funnel <- function(n) {
  stage1_to_2 <- rbinom(n, 1, 0.7)
  stage2_to_3 <- rbinom(sum(stage1_to_2), 1, 0.5)

  # Create outcome: 1, 2, or 3 (latest stage completed)
  outcome <- rep(1, n)
  outcome[stage1_to_2 == 1] <- 2
  outcome[stage1_to_2 == 1][1:sum(stage2_to_3)] <- 3

  return(outcome)
}

funnel_data <- simulate_funnel(n_customers)

# Estimate conversion rates
gamma_1_hat <- sum(funnel_data >= 2) / sum(funnel_data >= 1)
gamma_2_hat <- sum(funnel_data == 3) / sum(funnel_data >= 2)

cat("Estimated gamma_1 (Stage 1 -> 2):", round(gamma_1_hat, 3), "\n")
cat("Estimated gamma_2 (Stage 2 -> 3):", round(gamma_2_hat, 3), "\n")

# Bootstrap function for conversion rates
funnel_boot <- function(data, indices) {
  d <- data[indices]
  g1 <- sum(d >= 2) / sum(d >= 1)
  g2 <- sum(d == 3) / sum(d >= 2)
  return(c(g1, g2))
}

# Run bootstrap
boot_funnel_result <- boot(funnel_data, funnel_boot, R = 10000)

cat("\nBootstrap SE for gamma_1:", round(sd(boot_funnel_result$t[,1]), 4), "\n")
cat("Bootstrap SE for gamma_2:", round(sd(boot_funnel_result$t[,2]), 4), "\n")

# Plot bootstrap distributions
par(mfrow = c(1, 2))
hist(boot_funnel_result$t[,1], breaks = 50, main = "Bootstrap: gamma_1",
     xlab = "gamma_1", col = "steelblue")
abline(v = gamma_1_hat, col = "red", lwd = 2)

hist(boot_funnel_result$t[,2], breaks = 50, main = "Bootstrap: gamma_2",
     xlab = "gamma_2", col = "steelblue")
abline(v = gamma_2_hat, col = "red", lwd = 2)
par(mfrow = c(1, 1))
```

## Example 4: Block Bootstrap (Clustered Data)

Simulate Netflix viewing sessions with clustering by user.

```{r block-bootstrap}
# Simulate clustered data
n_users <- 1000
sessions_per_user <- 10

# Generate user-level means from Exp(1/1000)
user_means <- rexp(n_users, rate = 1/1000)

# Generate session-level data: each user's sessions ~ N(user_mean, 1)
session_data <- data.frame(
  user_id = rep(1:n_users, each = sessions_per_user),
  session_rate = as.vector(sapply(user_means, function(mu) {
    rnorm(sessions_per_user, mean = mu, sd = 1)
  }))
)

# Overall mean bit rate
mu_hat <- mean(session_data$session_rate)
cat("Estimated mean bit rate:", round(mu_hat, 2), "\n")

# Naive SE (ignoring clustering)
naive_se <- sd(session_data$session_rate) / sqrt(nrow(session_data))
cat("Naive SE (ignoring clustering):", round(naive_se, 2), "\n")

# Split session rates by user
sessions_by_user <- split(session_data$session_rate, session_data$user_id)

# Block bootstrap function
block_boot <- function(data, user_indices) {
  resampled_sessions <- unlist(data[as.character(user_indices)], use.names = FALSE)
  return(mean(resampled_sessions))
}

# Manual block bootstrap
B <- 10000
boot_means <- numeric(B)

for (b in 1:B) {
  # Sample users with replacement
  sampled_users <- sample(1:n_users, n_users, replace = TRUE)
  boot_means[b] <- block_boot(sessions_by_user, sampled_users)
}

# Block bootstrap SE
block_se <- sd(boot_means)
cat("Block bootstrap SE:", round(block_se, 2), "\n")
cat("\nRatio of block SE / naive SE:", round(block_se / naive_se, 2), "\n")

# Plot
hist(boot_means, breaks = 50, main = "Block Bootstrap Distribution",
     xlab = "Mean Bit Rate", col = "steelblue")
abline(v = mu_hat, col = "red", lwd = 2)
```

## Example 5: Heteroskedastic Regression

Bootstrap for regression with heteroskedastic errors.

```{r heteroskedastic}
# Generate heteroskedastic data
n <- 100
X <- rnorm(n, 0, 1)
epsilon <- rnorm(n, 0, abs(X)^2)  # Variance depends on X
Y <- X + epsilon

df <- data.frame(X = X, Y = Y)

# Fit OLS
lm_fit <- lm(Y ~ 1 + X, data = df)
summary(lm_fit)

# R's reported SE (assumes homoskedasticity)
r_se <- summary(lm_fit)$coefficients["X", "Std. Error"]
cat("\nR's SE for coefficient on X:", round(r_se, 3), "\n")

# Bootstrap function for regression coefficients
coef_boot <- function(data, indices) {
  fit <- lm(Y ~ X, data = data[indices, ])
  return(coef(fit))
}

# Run bootstrap
boot_reg_result <- boot(df, coef_boot, R = 10000)

# Bootstrap SE for X coefficient
bootstrap_se_X <- sd(boot_reg_result$t[, 2])
cat("Bootstrap SE for coefficient on X:", round(bootstrap_se_X, 3), "\n")
cat("\nRatio of bootstrap SE / R's SE:", round(bootstrap_se_X / r_se, 2), "\n")

# Plot bootstrap distribution for X coefficient
boot_coef_df <- data.frame(coef_X = boot_reg_result$t[, 2])

ggplot(boot_coef_df, aes(x = coef_X)) +
  geom_histogram(bins = 50, fill = "coral", alpha = 0.7) +
  geom_vline(xintercept = coef(lm_fit)["X"], color = "red", linewidth = 1) +
  labs(title = "Bootstrap Distribution of X Coefficient (Heteroskedastic Data)",
       x = "Coefficient on X",
       y = "Count") +
  theme_minimal()

# Scatter plot with fitted line
ggplot(df, aes(x = X, y = Y)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(title = "Heteroskedastic Data: Y = X + epsilon, where Var(epsilon) = X^4",
       x = "X", y = "Y") +
  theme_minimal()
```

