Confidence Interval for Proportion When Sample Proportion is Near 0 or 1

confidence intervalproportion;

I know there are methods to calculate a confidence interval for a proportion to keep the limits within (0, 1), however a quick Google search lead me only to the standard calculation: $\hat{p} \pm 1.96*\sqrt\frac{\hat{p}(1-\hat{p})}{N}$. I also believe there is a way to calculate the exact confidence interval using the binomial distribution (example R code would be nice). I know I can use the prop.test function to get the interval but I'm interested in working through the calculation.

Sample situations (N = number of trials, x = number of success):

N=40, x=40
N=40, x=39
N=20, x=0
N=20, x=1

Best Answer

Use a Clopper-Pearson interval?

Wikipedia discribes how to do this here: http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval

For example if you take your 39 successes in 40 trial example you get:

> qbeta(.025,39,2) #qbeta(alpha/2,x,n-x+1) x=num of successes and n=num of trials
[1] 0.8684141
> qbeta(1-.025,39,2)
[1] 0.9938864

For your 40 out of 40 you get:

> qbeta(1-.025,40,1)    
[1] 0.9993673
> qbeta(.025,40,1)
[1] 0.9119027
Related Question