── 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 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
N <- 5000
epsilon <- 0.1
sim_results <- (
parameters( ~n, ~theta,
seq(50, 1000, by = 50), c(0.2, 0.5, 0.8)
) %>%
add_trials(N) %>%
mutate(
y_sample = pmap(list(n, theta), .f = \(n,t) rbeta(n, shape1 = t, shape2 = 3)),
theta_hat = map_dbl(y_sample, \(y) 2 * mean(y / (1 - y))),
within_eps = ifelse(abs(theta_hat - theta) < epsilon, 1, 0)
) %>%
summarize(
prob_within_eps = mean(within_eps),
.by = c(n, theta)
)
)
ggplot(sim_results, aes(x = n, y = prob_within_eps, color = as.factor(theta))) +
geom_line(linewidth = 1) +
geom_hline(yintercept = 1, linetype = "dashed", color = "black", alpha = 0.5) +
labs(
title = "Simulated Convergence of Estimator",
x = "n",
y = expression(P(abs(hat(theta) - theta) < 0.1)),
color = expression(True~theta)
) +
scale_y_continuous(limits = c(0, 1.05), breaks = seq(0, 1, by = 0.2)) +
theme_classic(base_size = 14)