A **confidence interval for a binomial probability** is calculated using the following formula:

**Confidence Interval = p**** +/- z*(√p(1-p) / n)**

where:

**p:**proportion of “successes”**z:**the chosen z-value**n:**sample size

The z-value that you will use is dependent on the confidence level that you choose. The following table shows the z-value that corresponds to popular confidence level choices:

Confidence Level |
z-value |
---|---|

0.90 | 1.645 |

0.95 | 1.96 |

0.99 | 2.58 |

For example, suppose we want to estimate the proportion of residents in a county that are in favor of a certain law. We select a random sample of 100 residents and find that 56 of them are in favor of the law.

This tutorial explains three different ways to calculate a confidence interval for the true proportion of residents in the entire county that support the law.

**Method 1: Use the prop.test() function**

One way to calculate the 95% binomial confidence interval is to use the **prop.test()** function in base R:

#calculate 95% confidence interval prop.test(x=56, n=100, conf.level=.95, correct=FALSE) 1-sample proportions test without continuity correction data: 56 out of 100, null probability 0.5 X-squared = 1.44, df = 1, p-value = 0.2301 alternative hypothesis: true p is not equal to 0.5 95 percent confidence interval: 0.4622810 0.6532797 sample estimates: p 0.56

The 95% C.I. for the true proportion of residents in the county that support the law is **[.46228, .65328]**.

**Method 2: Use the binconf() function**

Another way to calculate the confidence interval is to use the **binconf()** function from the **Hmisc** package:

library(Hmisc) #calculate 95% confidence interval binconf(x=56, n=100, alpha=.05) PointEst Lower Upper 0.56 0.462281 0.6532797

Notice that this confidence interval matches the one calculated in the previous example.

**Method 3: Calculate the Confidence Interval Manually**

Another way to calculate the 95% binomial confidence interval in R is to do it manually:

#define proportion p <- 56/100 #define significance level a <- .05 #calculate 95% confidence interval p + c(-qnorm(1-a/2), qnorm(1-a/2))*sqrt((1/100)*p*(1-p)) [1] 0.4627099 0.6572901

Learn more about the **qnorm()** function here: A Guide to dnorm, pnorm, qnorm, and rnorm in R

**Additional Resources**

How to Perform a Binomial Test in R

How to Plot a Binomial Distribution in R