sim6.1

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
library(purrrfect)

Attaching package: 'purrrfect'

The following objects are masked from 'package:base':

    replicate, tabulate
lambdas <- c(0.5, 1, 1.5, 2)

plots <- map(lambdas, \(lam) {
  sim_df <- purrrfect::replicate(10000, rexp(1, rate = lam) + rexp(1, rate = lam)) %>%
    rename(U = 2)
  
  ggplot(data = sim_df) +
    geom_histogram(
      aes(x = U, y = after_stat(density)),
      fill = "cornflowerblue",
      color = "black",
      bins = 40
    ) +
    geom_function(
      fun = \(u) dgamma(u, shape = 2, rate = lam),
      linewidth = 1
    ) +
    labs(
      title = bquote(lambda == .(lam)),
      x = "u",
      y = expression(f[U](u))
    ) +
    theme_classic(base_size = 14)
})

plots
[[1]]
Warning: Computation failed in `stat_bin()`.
Caused by error in `cut.default()`:
! 'x' must be numeric


[[2]]
Warning: Computation failed in `stat_bin()`.
Caused by error in `cut.default()`:
! 'x' must be numeric


[[3]]
Warning: Computation failed in `stat_bin()`.
Caused by error in `cut.default()`:
! 'x' must be numeric


[[4]]
Warning: Computation failed in `stat_bin()`.
Caused by error in `cut.default()`:
! 'x' must be numeric