# Reviews multiple packages for getting confidence interval of a proportion.
# Here, we'll be computing confidence interval for gender = male

#install.packages('DescTools')
#install.packages('PropCIs')
#install.packages('plyr')
library(DescTools)
library(PropCIs)
library(plyr)
library(ggplot2)

#The program first needs you to compute frequency of success and total N

count(Wk06$gender)
##   x freq
## 1 1 10
## 2 2 18
#The binom.test and BinomCI comes from DescTools. Define group 1 (male) as "success"

binom.test(10, 28,
0.5,
alternative="two.sided",
conf.level=0.95)
## 
## Exact binomial test
##
## data: 10 and 28
## number of successes = 10, number of trials = 28, p-value = 0.1849
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
## 0.1864067 0.5593497
## sample estimates:
## probability of success
## 0.3571429
BinomCI(10,28,
conf.level = 0.95,
method = "clopper-pearson")
##            est    lwr.ci    upr.ci
## [1,] 0.3571429 0.1864067 0.5593497
#You can compute confidence intervals for both success and failure in one step

observed = c(10,18)

total = sum(observed)

BinomCI(observed, total,
conf.level = 0.95,
method = "clopper-pearson")
##           est    lwr.ci    upr.ci
## x.1 0.3571429 0.1864067 0.5593497
## x.2 0.6428571 0.4406503 0.8135933
#Alternative approaches from the PropCIs, including "exact" and "Blaker exact"

exactci(10,28,
conf.level=0.95)
## 
##
##
## data:
##
## 95 percent confidence interval:
## 0.1864067 0.5593497
blakerci(10,28, 
conf.level=0.95)
## 
##
##
## data:
##
## 95 percent confidence interval:
## 0.1924667 0.5553797