library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
sim_df <- expand_grid(lambda = c(0.5, 1, 1.5, 2),
rep = 1:10000) %>%
mutate(X = rexp(n(), rate = 1/lambda),
Y = rexp(n(), rate = 1/lambda),
U = X + Y,
f_U = dgamma(U, shape = 2, scale = lambda))
ggplot(data = sim_df) +
geom_histogram(aes(x = U, y = after_stat(density)),
fill = 'cornflowerblue',
color = 'black',
center = 0.125, binwidth = 0.25) +
geom_line(aes(x = U, y = f_U),
linewidth = 1) +
facet_wrap(~lambda, scales = 'free', labeller = label_both) +
labs(x = 'u', y = expression(f[U](u))) +
theme_classic(base_size = 14)