# 1. Simulate 10,000 (X, Y) realizations using purrrfect::replicate()
sim_xy <- replicate(10000, {
x <- rexp(1, rate = 1)
y <- x + rexp(1, rate = 1)
c(x = x, y = y)
}) |>
unnest_wider(-.trial)
# 2. Scatterplot with analytic and simulated conditional mean functions
ggplot(sim_xy, aes(x = x, y = y)) +
geom_point(alpha = 0.15, size = 0.8) +
geom_smooth(
aes(color = "Simulated E(Y|X)"),
method = "lm",
se = FALSE,
linewidth = 1
) +
geom_abline(
aes(intercept = 1, slope = 1, color = "Analytic E(Y|X) = 1 + X"),
linetype = "dashed",
linewidth = 1
) +
scale_color_manual(
name = "Conditional Mean",
values = c("Simulated E(Y|X)" = "blue", "Analytic E(Y|X) = 1 + X" = "red")
) +
labs(
title = "Simulated Joint Distribution of (X, Y) with Conditional Mean E(Y|X)",
subtitle = "10,000 realizations from f(x, y) = exp(-y), 0 <= x < y",
x = "X",
y = "Y"
) +
theme_bw()