Question 8
library(tidyverse)
library(purrrfect)
epsilon <- 0.1
(betas_many_runs <- parameters(~theta, ~n,
c(0.2, 0.5, 0.8), seq(50, 1000, by = 50))
%>% add_trials(5000)
%>% mutate(Ysample = pmap(list(theta, n), .f = \(t, n) rbeta(n, t,3)))
%>% mutate(theta = theta)
%>% mutate(theta_hat = map_dbl(Ysample, .f = \(x) 2 * mean(x / (1 - x))))
%>% mutate(within_epsilon = ifelse(abs(theta_hat - theta) < epsilon, 1, 0))
) %>% head
# A tibble: 6 × 6
theta n .trial Ysample theta_hat within_epsilon
<dbl> <dbl> <dbl> <list> <dbl> <dbl>
1 0.2 50 1 <dbl [50]> 0.179 1
2 0.2 50 2 <dbl [50]> 0.110 1
3 0.2 50 3 <dbl [50]> 0.147 1
4 0.2 50 4 <dbl [50]> 0.112 1
5 0.2 50 5 <dbl [50]> 0.245 1
6 0.2 50 6 <dbl [50]> 0.253 1
(betas_many_runs
%>% group_by(theta,n)
%>% summarize(prob_within_eps = mean(within_epsilon), .groups = 'drop')
) -> prop_within_epsilon
(ggplot(data = prop_within_epsilon)
+ geom_line(aes(x = n, y = prob_within_eps, color = factor(theta)))
+ labs(x = 'n',
y = expression(P(abs(hat(theta) - theta) < epsilon)),
color = expression(theta)
)
+ theme_classic()
)
The graph reveals that the larger the true value of theta, the longer it takes to converge to 1.