library(tidyverse)Warning: package 'tidyverse' was built under R version 4.5.2
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'readr' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.2.0
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 4.0.1 ✔ 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
library(knitr)
t_val <- 5
sim_data <- parameters(~n, ~beta,
c(20, 40, 80, 160, 320, 640),
c(0.5, 2, 4)) %>%
add_trials(10000) %>%
mutate(
Ysample = pmap(list(n, beta), \(n, b) rexp(n, rate = 1/b)),
ybar = map_dbl(Ysample, mean),
true_theta = exp(-t_val / beta),
mle = exp(-t_val / ybar),
se = (t_val * exp(-t_val / ybar)) / (ybar * sqrt(n)),
lcl = mle - 1.96 * se,
ucl = mle + 1.96 * se,
covers = ifelse(lcl < true_theta & ucl > true_theta, 1, 0)
)
coverage_summary <- sim_data %>%
summarize(
coverage = mean(covers),
.by = c(n, beta)
)
kable(coverage_summary)| n | beta | coverage |
|---|---|---|
| 20 | 0.5 | 0.7468 |
| 20 | 2.0 | 0.8921 |
| 20 | 4.0 | 0.9256 |
| 40 | 0.5 | 0.8015 |
| 40 | 2.0 | 0.9168 |
| 40 | 4.0 | 0.9358 |
| 80 | 0.5 | 0.8402 |
| 80 | 2.0 | 0.9262 |
| 80 | 4.0 | 0.9401 |
| 160 | 0.5 | 0.8762 |
| 160 | 2.0 | 0.9398 |
| 160 | 4.0 | 0.9469 |
| 320 | 0.5 | 0.9029 |
| 320 | 2.0 | 0.9472 |
| 320 | 4.0 | 0.9499 |
| 640 | 0.5 | 0.9211 |
| 640 | 2.0 | 0.9515 |
| 640 | 4.0 | 0.9503 |
ggplot(data = coverage_summary) +
geom_line(aes(x = n, y = coverage)) +
geom_hline(aes(yintercept = 0.95), linetype = 2) +
theme_classic(base_size = 14) +
labs(y = 'Coverage', x = 'n') +
facet_wrap(~beta, labeller = label_both)