Get a binomial confidence interval for each row in a Data Frame

Hi there.

I have a data frame like this with n binomial samples:

df<-data.frame(success=c(1,2,3,2),total=c(2,4,5,3))

Where I have to get a confidence interval for each row with the lowest value and the highest. Something like this: "0.25 - 0.39"

I would have to apply for each row the funcion binom.confint from "binom" library. But I am having some troubles to apply that function to each row.

If i could, I would need something like this to get the lowest, with the "exact" method:

a<-binom.confint(df$success[1],df$total[1],0.95)[5,5]

And something like this to get the highest:

b<-binom.confint(vpip$played[1],vpip$total[1],0.95)[5,6]

Then i would have to paste them into something like this with each row:

final<-paste0(a,"-",b) 

There is any way to get the binom.confint function for each row, to be able to do this?

is missing, so all that can be offered is

library(binom)
DF <- data.frame(success=c(1,2,3,2),total=c(2,4,5,3))

find_bci <- function(x,y) binom.confint(x$success[y],x$total[y],0.95)[5,5]

receiver <- c()
for (i in seq_along(DF[,1])) receiver[i] <- find_bci(DF,i)
#> Warning in stats::prop.test(x[i], n[i]): Chi-squared approximation may be
#> incorrect

#> Warning in stats::prop.test(x[i], n[i]): Chi-squared approximation may be
#> incorrect

#> Warning in stats::prop.test(x[i], n[i]): Chi-squared approximation may be
#> incorrect

#> Warning in stats::prop.test(x[i], n[i]): Chi-squared approximation may be
#> incorrect
receiver
#> [1] 0.01257912 0.06758599 0.14663280 0.09429932
1 Like

picking the results out by position seems an awkward choice, if you know for example that you want 'exact' method, and you want the lower bounds, why not just use that ?
i.e. rather than

a<-binom.confint(df$success[1],df$total[1],0.95)[5,5]

prefer

a<-binom.confint(df$success[1],df$total[1],0.95,methods="exact")$lower

if you didnt choose only the first elements of success and total, you would get the outputs for each row.

a<-binom.confint(df$success,df$total,0.95,methods="exact")$lower
> a
[1] 0.01257912 0.06758599 0.14663280 0.09429932
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.