I'm trying to write an R script to simulate the repeated experiments interpretation of a 95% confidence interval. I've found that it overestimates the proportion of times in which the true population value of a proportion is contained within the sample's 95% CI. Not a big difference - about 96% vs 95% but this interested me nonetheless.
My function takes a sample samp_n from a Bernouilli distribution with probability pop_p, and then calculates a 95% confidence interval with prop.test() using continuity correction, or more exactly with binom.test(). It returns 1 if the true population proportion pop_p is contained within the 95% CI. I've written two functions, one which uses prop.test() and one which uses binom.test() and have had similar results with both:
in_conf_int_normal <- function(pop_p = 0.3, samp_n = 1000, correct = T){
## uses normal approximation to calculate confidence interval
## returns 1 if the CI contain the pop proportion
## returns 0 otherwise
samp <- rbinom(samp_n, 1, pop_p)
pt_result <- prop.test(length(which(samp == 1)), samp_n)
lb <- pt_result$conf.int[1]
ub <- pt_result$conf.int[2]
if(pop_p < ub & pop_p > lb){
return(1)
} else {
return(0)
}
}
in_conf_int_binom <- function(pop_p = 0.3, samp_n = 1000, correct = T){
## uses Clopper and Pearson method
## returns 1 if the CI contain the pop proportion
## returns 0 otherwise
samp <- rbinom(samp_n, 1, pop_p)
pt_result <- binom.test(length(which(samp == 1)), samp_n)
lb <- pt_result$conf.int[1]
ub <- pt_result$conf.int[2]
if(pop_p < ub & pop_p > lb){
return(1)
} else {
return(0)
}
}
I've found that when you repeat the expriment a few thousand times, the proportion of times when the pop_p is within the 95% CI of the sample is closer to 0.96 rather than 0.95.
set.seed(1234)
times = 10000
results <- replicate(times, in_conf_int_binom())
sum(results) / times
[1] 0.9562
My thoughts so far about why this may be the case are
- my code is wrong (but I've checked it a lot)
- I initially thought that this was due to the normal approximation issue, but then found
binom.test()
Any suggestions?
times=100000a few different times and saw the same result. I'm curious to see if anyone has an explanation for this. The code is sufficiently simple that I'm pretty certain there is no coding error. Also, one run withtimes=1000000gave.954931as the result. – Macro Jul 6 '12 at 18:00