Calculates confidence intervals (CI) for proportions in binary variables. This enhanced version of DescTools::BinomCI() returns a data frame.

ci_binom(x, n, method = "modified wilson", conf.level = 0.95, ...)

Arguments

x

Number of events of interest or favorable outcomes.

n

Total number of events.

method

Calculation method: "modified wilson", "wilson", "agresti-coull", and others. See DescTools::BinomCI() documentation.

conf.level

Confidence level. Default: 0.95.

...

Additional parameters for DescTools::BinomCI(). See the documentation for that function.

Value

A data frame with columns:

  • est (<dbl>) – proportion estimate;

  • lwr.ci, upr.ci (<dbl>) – lower and upper CI bounds;

  • x (<int>) – number of events of interest;

  • n (<int>) – total number of events.

Details

Similar to DescTools::BinomCI(), but uses the modified Wilson method by default and returns a data frame instead of a vector, enabling plotting with ggplot2.

Examples

# Example 1: Survey responses
# 54 out of 80 people agree with a statement
# What is the true proportion of agreement in the population?
ci_binom(x = 54, n = 80)
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.675  0.566  0.768    54    80
# Interpretation: We're 95% confident the true proportion
# is between lwr.ci and upr.ci (roughly 0.57 to 0.78)

# Example 2: Medical treatment success
# 23 out of 30 patients recovered
ci_binom(x = 23, n = 30)
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.767  0.591  0.882    23    30

# Example 3: Coin flips
# Testing if a coin is fair: 58 heads in 100 flips
ci_binom(x = 58, n = 100)
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1  0.58  0.482  0.672    58   100
# If 0.5 is in the CI, we can't rule out the coin being fair

# Example 4: Effect of sample size
# Same proportion (54/80 is approximately 0.675) but different sample sizes
ci_binom(x = 54, n = c(80, 100, 200, 500))
#> # A tibble: 4 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.675 0.566   0.768    54    80
#> 2 0.54  0.443   0.634    54   100
#> 3 0.27  0.213   0.335    54   200
#> 4 0.108 0.0837  0.138    54   500
# Notice: Larger samples give narrower (more precise) CIs

# Example 5: Two separate groups
# Group A: 23 successes, Group B: 45 successes
successes <- c(23, 45)
ci_binom(successes, n = sum(successes))
#> # A tibble: 2 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.338  0.237  0.457    23    68
#> 2 0.662  0.543  0.763    45    68

# Example 6: Student exam pass rates
# 67 out of 85 students passed
ci_binom(x = 67, n = 85)
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.788  0.690  0.862    67    85

# Example 7: Different confidence levels
# 90% confidence (narrower interval, less confident)
ci_binom(x = 54, n = 80, conf.level = 0.90)
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.675  0.584  0.754    54    80

# 99% confidence (wider interval, more confident)
ci_binom(x = 54, n = 80, conf.level = 0.99)
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1 0.675  0.531  0.792    54    80

# Example 8: Comparing different methods
ci_binom(x = 15, n = 25, method = "wilson")
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1   0.6  0.407  0.766    15    25
ci_binom(x = 15, n = 25, method = "agresti-coull")
#> # A tibble: 1 × 5
#>     est lwr.ci upr.ci     x     n
#>   <dbl>  <dbl>  <dbl> <int> <int>
#> 1   0.6  0.407  0.766    15    25
# Different methods can give slightly different results