Changes to this function will be annoucned via email and the courses Facebook page https://www.facebook.com/groups/930301587096169/?ref=bookmarks
This function calcualtes Wilson’s (1927) confidence interval for a binomial proportion. It is called the score interval by Agresti-Coull (1998). This interval has good statistical properties.
(Unlike Agresti and Coull’s approximation of the Wilson interval, the extact interavl coded below will not produce values less than zero or great than one. The approximation is advocated in a common biometrics text book (Whitlock and Shulter Analysis of Biological Data) and suppossedly performs well away from boundaries.)
The core code for this function is from: http://stats.stackexchange.com/questions/59733/can-agresti-coull-binomial-confidence-intervals-be-negative
and was written by http://stats.stackexchange.com/users/21054/coolserdash
More information is available at https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
Copy and paste the code below to make the function.
# FUNCTION BEGINS HERE
# FUNCTION BEGINS HERE
# FUNCTION BEGINS HERE
# FUNCTION BEGINS HERE
# FUNCTION BEGINS HERE
binom.CI <- function(events, #events = outcomes
trials, #number of individuals, test, etc
alpha = 0.05){
n <- trials
x <- events
p.hat <- x/n
# Calculate upper and lower limit
upper.lim <- (p.hat + (qnorm(1-(alpha/2))^2/(2*n)) + qnorm(1-(alpha/2)) * sqrt(((p.hat*(1-p.hat))/n) + (qnorm(1-(alpha/2))^2/(4*n^2))))/(1 + (qnorm(1-(alpha/2))^2/(n)))
lower.lim <- (p.hat + (qnorm(alpha/2)^2/(2*n)) + qnorm(alpha/2) * sqrt(((p.hat*(1-p.hat))/n) + (qnorm(alpha/2)^2/(4*n^2))))/(1 + (qnorm(alpha/2)^2/(n)))
# Modification for probabilities close to boundaries
if ((n <= 50 & x %in% c(1, 2)) | (n >= 51 & n <= 100 & x %in% c(1:3))) {
lower.lim <- 0.5 * qchisq(alpha, 2 * x)/n
}
if ((n <= 50 & x %in% c(n - 1, n - 2)) | (n >= 51 & n <= 100 & x %in% c(n - (1:3)))) {
upper.lim <- 1 - 0.5 * qchisq(alpha, 2 * (n - x))/n
}
out <- c(lower.lim,upper.lim)
names(out) <- c("lower.CI","upper.CI")
return(out)
#The core code for this function is from
#stats.stackexchange.com/questions/59733/can-agresti-coull-binomial#-confidence-intervals-be-negative
#and was written by stats.stackexchange.com/users/21054/coolserdash
#for more info see
# wikipedia.org/wiki/Binomial_proportion_confidence_interval
}
# FUNCTION ENDS HERE
# FUNCTION ENDS HERE
# FUNCTION ENDS HERE
# FUNCTION ENDS HERE
# FUNCTION ENDS HERE
<>br
When probabilities are close to zero, the Wilson Interval (aka score interval) produces zero as its lower bound.
#total number of trials (tests, experiments, individuals)
n <- 5
#total number of events
x <- 0
binom.CI(events = x, trials = n)
## lower.CI upper.CI
## 0.0000000 0.4344825
When probabilities are close to 1, the Interval produces 1 as its upper bound
n <- 5
x <- 5
binom.CI(events = x, trials = n)
## lower.CI upper.CI
## 0.5655175 1.0000000