Write a computer program to simulate 10,000 Bernoulli trials with probability .3 for success on each trial. Have the program compute the 95 percent confidence interval for the probability of success based on the proportion of successes. Repeat the experiment 100 times and see how many times the true value of .3 is included within the confidence limits
p = 0.3
n <- 100
m <- 10000
Successes <- vector(length = n)
for(i in 1:n){
Simul <- runif(m)
Success <- length(Simul[which(Simul<p)])
Successes[i]<- Success
}
Successes <- Successes/m
hist(Successes)
To calculate the 95% confidence limit,
\[p\pm1.96*\sqrt{\frac{pq}{n}}\]
\[0.3\pm1.96*\sqrt{\frac{0.3*0.7}{10,000}}\]
delt <- 1.96*sqrt(0.3*0.7/10000)
lower <- 0.3 - delt
upper <- 0.3 + delt
Of the 100 test performed, how many fall within these limits?
test_limits <- Successes >= lower & Successes <= upper
table(test_limits)
## test_limits
## FALSE TRUE
## 7 93
Though the confidence interval was 95%, for a sample size of 100 trials, that number will not always be reflected. As the number of trials increases, The number will approach 95%.
Below is a sample showing the same calculation for the trials being repeated 1000 times.
p = 0.3
n <- 1000
m <- 10000
Successes2 <- vector(length = n)
for(i in 1:n){
Simul <- runif(m)
Success <- length(Simul[which(Simul<p)])
Successes2[i]<- Success
}
Successes2 <- Successes2/m
test_limits2 <- Successes2 >= lower & Successes2 <= upper
length(test_limits2[which(test_limits2==TRUE)])/length(test_limits2)
## [1] 0.94