Confidence Intervals for Proportions in R and Excel

Suppose one conducts a survey of $n = 120$ people and $81$ of them say that they will vote in the next election. Equivalently, of the $120$ surveyed, the proportion that said they would vote in the next election is $\widehat{p} = 0.675$.

Note that the requisite assumptions seem to be satisfied, as $81 \ge 5$ and $120 - 81 = 39 \ge 5$. Thus, we know that a $95\%$ confidence interval for the proportion of people that will vote in the next election has bounds $$\widehat{p} \pm z_{\alpha/2} \sqrt{\frac{\widehat{p}\widehat{q}}{n}}$$ We can quickly compute these bounds with either R or Excel.

In R, we can use something similar to:

> p.hat = 81/120
> n = 120
> p.hat + c(-1,1)*qnorm(0.975)*sqrt(p.hat*(1-p.hat)/n)
[1] 0.5911986 0.7588014

If desired, one can use similar calculations to make a function to calculate the confidence interval for a proportion, as seen below:

> conf.int.prop = function (x,n,conf) {
    p.hat = x/n
    q.hat = 1 - x/n
    z = qnorm(conf+(1.0-conf)/2.0)
    sd = sqrt(p.hat*q.hat/n)
    return (p.hat + c(-1,1)*z*sd)
  }
> conf.int.prop(x=81,n=120,conf=0.95)
[1] 0.5911986 0.7588014

In Excel, we instead can use the following two similar formulas:

=81/120-NORM.INV(0.975,0,1)*SQRT((81/120)*(39/120)/120)
=81/120+NORM.INV(0.975,0,1)*SQRT((81/120)*(39/120)/120)