10.3

q8

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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
alpha <- 0.05
n_values <- c(5, 10, 15, 20)
sigma2_grid <- seq(1, 10, length.out = 100)

q8_results <- expand_grid(
  n = n_values,
  sigma2_a = sigma2_grid
) %>%
  mutate(
    sigma_a = sqrt(sigma2_a),

    chi_cutoff = qchisq(1 - alpha, df = n),
    power_phi1 = 1 - pchisq(chi_cutoff / sigma2_a, df = n),

    z_cutoff = qnorm(1 - alpha),
    power_phi2 = 1 - pnorm(z_cutoff / sigma_a)
  ) %>%
  pivot_longer(
    cols = c(power_phi1, power_phi2),
    names_to = "test",
    values_to = "rejection_rate"
  )

ggplot(q8_results, aes(x = sigma2_a, y = rejection_rate, color = test)) +
  geom_line(linewidth = 1) +
  facet_wrap(~ n) +
  labs(
    title = "Analytic Rejection Rates for Question 5",
    x = expression(sigma[a]^2),
    y = "Rejection Rate",
    color = "Test"
  ) +
  theme_minimal()

q9

library(tidyverse)


alpha <- 0.05
n_values <- c(5, 10, 15, 20)
theta_grid <- seq(0.2, 1, length.out = 20)
B <- 10000


cutoffs <- map_dfr(n_values, function(n) {
  
  y_null <- matrix(rbeta(B * n, shape1 = 0.2, shape2 = 1), nrow = B)
  
  T1_null <- rowMeans(log(y_null))
  T2_null <- rowMeans(y_null)
  
  tibble(
    n = n,
    cutoff_phi1 = quantile(T1_null, probs = 1 - alpha),
    cutoff_phi2 = quantile(T2_null, probs = 1 - alpha)
  )
})

cutoffs
# A tibble: 4 × 3
      n cutoff_phi1 cutoff_phi2
  <dbl>       <dbl>       <dbl>
1     5       -1.94       0.376
2    10       -2.68       0.309
3    15       -3.05       0.282
4    20       -3.32       0.263