# Create bootstrap data frame
bootstrap_df <- data.frame(
Iteration = 1:n_iterations,
Mean_Difference = results$mean_diff_distribution,
High_Greater = factor(results$high_greater, levels = c(0, 1))
)
# Main plot
p1 <- ggplot(bootstrap_df, aes(x = Mean_Difference)) +
geom_histogram(aes(y = ..density..), bins = 40,
fill = "steelblue", alpha = 0.7, color = "black") +
geom_density(color = "darkblue", size = 1.5) +
geom_vline(xintercept = 0, color = "red",
linetype = "dashed", size = 1.2) +
geom_vline(xintercept = observed_diff, color = "darkgreen",
linetype = "solid", size = 1.2) +
geom_vline(xintercept = ci_95[1], color = "orange",
linetype = "dotted", size = 1) +
geom_vline(xintercept = ci_95[2], color = "orange",
linetype = "dotted", size = 1) +
annotate("text", x = observed_diff,
y = max(density(bootstrap_df$Mean_Difference)$y) * 0.9,
label = "Observed\nDifference", color = "darkgreen",
fontface = "bold", hjust = -0.1, size = 4) +
annotate("text", x = 0,
y = max(density(bootstrap_df$Mean_Difference)$y) * 0.7,
label = "H₀: No\nDifference", color = "red",
fontface = "bold", hjust = 1.1, size = 4) +
annotate("rect", xmin = ci_95[1], xmax = ci_95[2],
ymin = 0, ymax = Inf, alpha = 0.1, fill = "orange") +
labs(
title = "Bootstrap Distribution of Mean Differences (High - Low Pressure)",
subtitle = sprintf("P(High > Low) = %.2f%% | 95%% CI shown in shaded region",
probability_high_better * 100),
x = "Mean Difference (High Pressure - Low Pressure)",
y = "Density",
caption = paste("Based on", n_iterations, "bootstrap iterations")
) +
theme_minimal(base_size = 14) +
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5, size = 11)
)
print(p1)
# Cumulative probability plot
bootstrap_df$Cumulative_Prop <- cumsum(bootstrap_df$High_Greater == 1) /
seq_along(bootstrap_df$High_Greater)
ggplot(bootstrap_df, aes(x = Iteration, y = Cumulative_Prop)) +
geom_line(color = "steelblue", size = 1) +
geom_hline(yintercept = probability_high_better,
color = "darkblue", linetype = "dashed", size = 1) +
geom_hline(yintercept = 0.95, color = "red",
linetype = "dotted", size = 0.8) +
geom_hline(yintercept = 0.90, color = "orange",
linetype = "dotted", size = 0.8) +
annotate("text", x = n_iterations * 0.7,
y = probability_high_better + 0.02,
label = sprintf("Final: %.2f%%", probability_high_better * 100),
color = "darkblue", fontface = "bold", size = 4) +
annotate("text", x = n_iterations * 0.1, y = 0.95 + 0.01,
label = "α = 0.05", color = "red", fontface = "bold") +
annotate("text", x = n_iterations * 0.1, y = 0.90 + 0.01,
label = "α = 0.10", color = "orange", fontface = "bold") +
labs(
title = "Convergence of Bootstrap Probability Estimate",
subtitle = "Cumulative proportion where High Pressure mean exceeds Low Pressure mean",
x = "Bootstrap Iteration",
y = "Cumulative Proportion",
caption = "Horizontal lines indicate conventional significance thresholds"
) +
scale_y_continuous(limits = c(0, 1), labels = percent) +
theme_minimal(base_size = 14) +
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5, size = 11)
)