I am trying to calculate binomial proportion confidence interval for my data. Here I just use the iris dataset.
# break the length into different size
iris$Slen <- cut(iris$Sepal.Length,
breaks = c(4,5,6,8))
#need to compute confidence interval for binomial proportion within each group and size bin
alpha <- as.numeric(0.05)
#function to calculate conf.int.
CI <-function(n,r) {
f1=qf(1-alpha/2, 2*r, 2*(n-r+1), lower.tail=FALSE)
f2=qf(alpha/2, 2*(r+1), 2*(n-r), lower.tail=FALSE)
pl=(1+(n-r+1)/(r*f1))^(-1)
pu=(1+(n-r)/((r+1)*f2))^(-1)
}
First I calculate the number of species in each of the size bin and then calculate the relative proportion using the code below:
library(dplyr)
iris_count <- iris %>%
group_by(Slen, Species) %>%
summarise(TotalParticle = n())%>%
mutate(RelAbund = TotalParticle/sum(TotalParticle))
Now I want to calculate the CI for the iris_count data. n is the total number in each bin and r is the RelAbund*n in the CI function. Ex. for Slen (4,5] setosa n is 28+3+1 and r is 0.875*32.
How can I compute this directly and not manually for each case?