# --- Helper: stack data into Panel A (all) + Panel B (repeat-flooded) -----
build_panels <- function(df, value_col) {
dplyr::bind_rows(
df |> dplyr::mutate(panel = "A. All funded"),
df |> dplyr::filter(repeat_exp) |>
dplyr::mutate(panel = "B. Repeat-flooded funded")
) |>
dplyr::mutate(panel = factor(panel,
levels = c("A. All funded",
"B. Repeat-flooded funded"))) |>
dplyr::rename(x = {{ value_col }}) |>
dplyr::filter(!is.na(x))
}
# --- Figure: property value percentile ------------------------------------
val_data <- build_panels(funded_pair, pct_A)
val_medians <- val_data |>
dplyr::group_by(panel, mit_type) |>
dplyr::summarise(median_x = round(median(x, na.rm = TRUE), 1),
.groups = "drop")
fig_si_mit_type_value <- ggplot(val_data, aes(x = x, fill = mit_type)) +
geom_histogram(binwidth = 5, boundary = 0,
position = "identity", alpha = 0.55,
colour = "grey30", linewidth = 0.15) +
geom_vline(data = val_medians,
aes(xintercept = median_x, colour = mit_type),
linetype = "dashed", linewidth = 0.6, show.legend = FALSE) +
facet_wrap(~ panel, nrow = 1, scales = "fixed") +
scale_fill_manual(values = c("Buyout" = "#1f78b4", "Elevation" = "#e31a1c"),
name = "Mitigation type") +
scale_colour_manual(values = c("Buyout" = "#1f78b4", "Elevation" = "#e31a1c")) +
scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, 20),
labels = function(x) paste0(x, "%"),
expand = c(0.005, 0)) +
scale_y_continuous(labels = scales::label_comma()) +
labs(x = "Property value (study-area percentile)",
y = "Number of funded parcels") +
theme_minimal(base_size = 11) +
theme(
legend.position = "top",
strip.text = element_text(face = "bold", size = 10),
panel.grid.minor = element_blank(),
panel.spacing = unit(2, "lines")
)
ggsave(file.path(fig_dir, "figS2_mit_type_value.png"),
plot = fig_si_mit_type_value,
width = 8, height = 4, dpi = 300, bg = "white")
# --- Figure: % Black share ------------------------------------------------
black_data <- build_panels(funded_pair, bg_pct_black_2013)
black_medians <- black_data |>
dplyr::group_by(panel, mit_type) |>
dplyr::summarise(median_x = round(median(x, na.rm = TRUE), 1),
.groups = "drop")
fig_si_mit_type_black <- ggplot(black_data, aes(x = x, fill = mit_type)) +
geom_histogram(binwidth = 5, boundary = 0,
position = "identity", alpha = 0.55,
colour = "grey30", linewidth = 0.15) +
geom_vline(data = black_medians,
aes(xintercept = median_x, colour = mit_type),
linetype = "dashed", linewidth = 0.6, show.legend = FALSE) +
facet_wrap(~ panel, nrow = 1, scales = "fixed") +
scale_fill_manual(values = c("Buyout" = "#1f78b4", "Elevation" = "#e31a1c"),
name = "Mitigation type") +
scale_colour_manual(values = c("Buyout" = "#1f78b4", "Elevation" = "#e31a1c")) +
scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, 20),
labels = function(x) paste0(x, "%"),
expand = c(0.005, 0)) +
scale_y_continuous(labels = scales::label_comma()) +
labs(x = "Block-group share of Black residents",
y = "Number of funded parcels") +
theme_minimal(base_size = 11) +
theme(
legend.position = "top",
strip.text = element_text(face = "bold", size = 10),
panel.grid.minor = element_blank(),
panel.spacing = unit(2, "lines")
)
ggsave(file.path(fig_dir, "figS3_mit_type_black.png"),
plot = fig_si_mit_type_black,
width = 8, height = 4, dpi = 300, bg = "white")
# --- KS tests (Buyout vs Elevation) per panel -----------------------------
ks_by_panel <- function(panel_data, panel_name) {
d <- panel_data |> dplyr::filter(panel == panel_name)
x1 <- d$x[d$mit_type == "Buyout"]
x2 <- d$x[d$mit_type == "Elevation"]
if (length(x1) < 2 || length(x2) < 2) return(NULL)
k <- suppressWarnings(ks.test(x1, x2))
list(D = unname(k$statistic), p = k$p.value,
n_buyout = length(x1), n_elevation = length(x2))
}
ks_val_a <- ks_by_panel(val_data, "A. All funded")
ks_val_b <- ks_by_panel(val_data, "B. Repeat-flooded funded")
ks_blk_a <- ks_by_panel(black_data, "A. All funded")
ks_blk_b <- ks_by_panel(black_data, "B. Repeat-flooded funded")
# --- Bundle for index_inputs.rds ------------------------------------------
mit_type_analysis <- list(
counts = type_counts,
value_medians = val_medians,
black_medians = black_medians,
ks_value_all = ks_val_a,
ks_value_repeat = ks_val_b,
ks_black_all = ks_blk_a,
ks_black_repeat = ks_blk_b
)
cat("\n=== KS tests (Buyout vs Elevation) ===\n")