To repeat the experiment 100 times and check how many times the true
value of 0.3 is included within the confidence limits, you can use a
loop:
set.seed(81282)
experm <- vector()
count_of_experm <- vector()
p_true <- 0.3
n <- 10000
num_experiments <- 100
count <- 0 # keep track of how many times the true value is within the confidence limits
#for (i in 1:num_experiments) {
for (i in seq(1,num_experiments,1)){
successes <- rbinom(n, 1, p_true)
prop_success <- sum(successes) / n
se <- sqrt(prop_success * (1 - prop_success) / n)
z <- qnorm(0.975)
lower_ci <- prop_success - z * se
upper_ci <- prop_success + z * se
if (lower_ci <= p_true && p_true <= upper_ci) {
count <- count + 1
}
experm[i]<-prop_success
}
cat("True value included within the confidence limits", count, "times out of", num_experiments, "experiments.")
## True value included within the confidence limits 94 times out of 100 experiments.
results_df <-as.data.frame(experm)
ggplot(results_df,aes(x=experm))+
geom_density()+
geom_vline(aes(xintercept=lower_ci,color='green'))+
geom_vline(aes(xintercept=upper_ci,color='green'))+
labs(title = 'Bernoulli Simulation', subtitle = "10,000 Trials")+
theme(plot.title = element_text(hjust = 0.5))+
geom_text(aes(x=round(lower_ci,5), label=paste0("Lower CI Bound\n",round(lower_ci,5),collapse = ''), y=40), colour="purple", angle=90,check_overlap=T) +
geom_text(aes(x=round(upper_ci,5), label=paste0("Upper CI Bound\n",round(upper_ci,5),collapse = ''), y=40), colour="purple", angle=90, check_overlap=T)

#x = TERM, y = `MEAN TEST SCORE`