library(tidyverse)
library(purrrfect)
theta0 <- 0.2
(many_nulls <- parameters(~n, c(5, 10, 15, 20)) %>%
add_trials(10000) %>%
mutate(
y_sample = map(n, .f = \(n) rbeta(n, shape1 = theta0, shape2 = 1)),
T1 = map_dbl(y_sample, .f = \(y) mean(log(y))),
T2 = map_dbl(y_sample, .f = \(y) mean(y))
)
) %>% head()
# A tibble: 6 × 5
n .trial y_sample T1 T2
<dbl> <dbl> <list> <dbl> <dbl>
1 5 1 <dbl [5]> -4.91 0.0401
2 5 2 <dbl [5]> -9.00 0.00398
3 5 3 <dbl [5]> -4.59 0.107
4 5 4 <dbl [5]> -2.15 0.238
5 5 5 <dbl [5]> -1.94 0.277
6 5 6 <dbl [5]> -6.49 0.0824
(crit_vals <- many_nulls %>%
group_by(n) %>%
summarize(
c_T = quantile(T1, 0.95),
c_Y = quantile(T2, 0.95),
.groups = "drop"
)
)
# A tibble: 4 × 3
n c_T c_Y
<dbl> <dbl> <dbl>
1 5 -1.95 0.379
2 10 -2.68 0.312
3 15 -3.08 0.282
4 20 -3.30 0.263
# Power on grid of theta(a)
(power_sim <- parameters(~n, ~theta, c(5, 10, 15, 20), seq(0.2, 1, l = 20)) %>%
left_join(crit_vals, by = "n") %>%
add_trials(10000) %>%
mutate(
y_sample = map2(n, theta, .f = \(n, theta) rbeta(n, shape1 = theta, shape2 = 1)),
T1 = map_dbl(y_sample, .f = \(y) mean(log(y))),
T2 = map_dbl(y_sample, .f = \(y) mean(y)),
phi1_rejects = T1 > c_T,
phi2_rejects = T2 > c_Y
) %>%
group_by(n, theta) %>%
summarize(
prob_phi1 = mean(phi1_rejects),
prob_phi2 = mean(phi2_rejects),
.groups = "drop"
)
) %>% head()
# A tibble: 6 × 4
n theta prob_phi1 prob_phi2
<dbl> <dbl> <dbl> <dbl>
1 5 0.2 0.0485 0.0483
2 5 0.242 0.0925 0.0766
3 5 0.284 0.150 0.110
4 5 0.326 0.222 0.155
5 5 0.368 0.286 0.196
6 5 0.411 0.370 0.244
# Power curves
ggplot(power_sim, aes(x = theta)) +
geom_line(aes(y = prob_phi1, color = "phi_1 (UMP)")) +
geom_line(aes(y = prob_phi2, color = "phi_2 (Mean)")) +
facet_wrap(~n, labeller = label_both) +
geom_hline(yintercept = 0.05, linetype = 2) +
scale_y_continuous(breaks = c(0.05, seq(0.25, 1, by = 0.25))) +
theme_classic() +
labs(x = expression(theta), y = "P(reject null)", color = "")