library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.2 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.1.0
── 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
N <- 10000
(many_gamma_CIs <- parameters(~n, ~theta, c(5, 10, 20, 40, 80, 160,320), c(2))
%>% add_trials(N)
%>% mutate(Ysample = pmap(list(n,theta), .f = \(n,t) rgamma(n, t,rate = t/theta)))
%>% mutate(Ybar = map_dbl(Ysample, mean), S2 = map_dbl(Ysample, var))
%>% mutate(LCL_exact = Ybar/qgamma(0.975, shape = n, scale = 1/n),
UCL_exact = Ybar/qgamma(0.025, shape = n, scale = 1/n),
LCL_asymptotic = Ybar - 1.96*sqrt(S2/n),
UCL_asymptotic = Ybar + 1.96*sqrt(S2/n))
%>% mutate(exact_covers = ifelse(LCL_exact < theta & UCL_exact > theta, 1, 0))
%>%mutate(asymptotic_covers = ifelse(LCL_asymptotic < theta & UCL_asymptotic > theta, 1, 0))
)# A tibble: 70,000 × 12
n theta .trial Ysample Ybar S2 LCL_exact UCL_exact LCL_asymptotic
<dbl> <dbl> <dbl> <list> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5 2 1 <dbl [5]> 1.81 1.45 0.885 5.58 0.758
2 5 2 2 <dbl [5]> 1.73 1.82 0.845 5.33 0.547
3 5 2 3 <dbl [5]> 1.67 1.73 0.813 5.13 0.514
4 5 2 4 <dbl [5]> 2.57 3.64 1.25 7.91 0.897
5 5 2 5 <dbl [5]> 2.57 2.91 1.25 7.90 1.07
6 5 2 6 <dbl [5]> 2.36 0.678 1.15 7.26 1.64
7 5 2 7 <dbl [5]> 2.28 0.702 1.11 7.03 1.55
8 5 2 8 <dbl [5]> 3.59 3.16 1.75 11.1 2.03
9 5 2 9 <dbl [5]> 1.25 1.62 0.612 3.86 0.137
10 5 2 10 <dbl [5]> 4.01 4.12 1.96 12.3 2.23
# ℹ 69,990 more rows
# ℹ 3 more variables: UCL_asymptotic <dbl>, exact_covers <dbl>,
# asymptotic_covers <dbl>