Confidence Intervals for
Binomial

First create a data set containing the range of values for p:
p = seq(.01,.99,.01)Because R does arithmetic on the whole data set at once (called vectorised operations), we can compute the probabilities of interest for the entire list of values for p at once:
L = pbinom(12,15,p,lower.tail=F) U = pbinom(13,15,p)L is the list of probabilities P(X >= 13  p). U is the list of probabilities P(X <= 13  p). Now we plot:
plot(p,L,ylab="Probability",type="l") lines(p,U)The specification `type="l"' (that's "ell" not "one") tells the plot function to connect the successive points with line segments. The second command, "lines", adds the second curve to the existing plot.
To find a 95% confidence interval, we need to find the values of p where the two curves are at the height .025. We can do this by adding a horizontal line at that height, and then using the locator() function to pick off the corrdinates of the points of intersection.
abline(h=.025)To use the locator function, after entering the following command in R, click on the points you wish to select. The locator function returns both the "x" and "y" coordinates of the selected points; we only need the "x" coordinates. The following usage asks for the "x" coordinates of two points:
locator(2)$xYou should get something in the neighborhood of .59 and .98. Grabbing the lower right corner of the plot window and expanding the window will increase the scale and thus the precision.
To produce a 90% confidence interval, simply draw the horizontal line at height .05 instead of .025.
We need to find two values for p: the largest plausible value, and the smallest plausible value. Again, "implausible" is defined in terms of events with total probability smaller than .05. To simplify the search, note that for p=1/2, it was sufficient that the probability of observing 13, 14, or 15 successes was less than .025 (why?). Again, we need to find two p's: the smallest one for which the probability of at least 13 successes is no smaller than .025, and the largest one for which the probability of 13 or fewer successes is no smaller than .025.
> 1  pbinom(12,15,1/2) [1] 0.003692627Try p=0.6:
> 1  pbinom(12,15,.6) [1] 0.027114It's a bit too big, we want .025.
> 1  pbinom(12,15,.59) [1] 0.02270254Close! The answer is somewhere between .59 and .60:
> 1  pbinom(12,15,.595) [1] 0.02482434I'll leave it as an exercise for the reader to get the value any more precise than this.
Now, for the upper end of the confidence interval, we need to find the largest plausible p, in other words, the largest p for which P(X <= 13) is at least .025.
Let's start with the value suggested by the graphical search:
> pbinom(13,15,.98) [1] 0.03533831We are close to .025, but will need to search a bit.
> pbinom(13,15,.99) [1] 0.009629773Hence, the upper bound is somewhere between p=.98 and p=.99. Again, I'll leave it as an exercise for the reader. When you have found it to the desired accuracy, say 3 significant digits, you will have what is called a 95% confidence interval for the unknown population proportion p.
Again  the 95% CI is just the set of null hypotheses for
p that would not be rejected with pvalues less than .05.
binom.test(13,15)Try it out, and compare the result to the values we found by hand above.
To produce a 90% confidence interval, use
binom.test(13,15,conf.level=.90)