# one proportion

## estimation

### verify conditions

``````#  check sample-size conditions for CI for one proportion
#  p is unknown, so use p.hat
conditions.ci.one.proportion <- function(p.hat, n){
category <- c("p.hat * n", "(1 - p.hat) * n")
count <- c(p.hat * n, (1 - p.hat) * n)
condition <- count >= 10
results <- data.frame(category, count, condition)
return(results)
}``````
``````p.hat <- 0.21
n <- 20
conditions.ci.one.proportion(p.hat, n)``````
``````##          category count condition
## 1       p.hat * n   4.2     FALSE
## 2 (1 - p.hat) * n  15.8      TRUE``````

### confidence interval for a proportion

``````z.star <- function(alpha){
return(qnorm(1 - alpha / 2))
}``````
``````ci.proportion <- function(p.hat, n, alpha){
z.star <- qnorm(1 - alpha/2)
se <- sqrt(p.hat * (1 - p.hat) / n)
ci <- p.hat + z.star * se * c(-1, 1)
return(list(p.hat=p.hat, z.star=z.star, se=se, ci=ci))
}``````
``````p.hat <- 0.21
n <- 40
alpha <- 0.05
(ci.results <- ci.proportion(p.hat, n, alpha))``````
``````## \$p.hat
## [1] 0.21
##
## \$z.star
## [1] 1.959964
##
## \$se
## [1] 0.06440109
##
## \$ci
## [1] 0.08377619 0.33622381``````

### gg.draw.ci.proportion

``````center <- ci.results\$p.hat
ci <- ci.results\$ci
title <- "CI for a Proportion"
gg.draw.ci.proportion(center, ci, title)``````