The binom.test() function performs an exact test of a simple null hypothesis about the probability of success in a Bernoulli experiment from summarized data or from raw data. The mosaic binom.test provides wrapper functions around the function of the same name in stats. These wrappers provide an extended interface (including formulas).

binom.test(
  x,
  n = NULL,
  p = 0.5,
  alternative = c("two.sided", "less", "greater"),
  conf.level = 0.95,
  ci.method = c("Clopper-Pearson", "binom.test", "Score", "Wilson", "prop.test", "Wald",
    "Agresti-Coull", "Plus4"),
  data = NULL,
  success = NULL,
  ...
)

Arguments

x

count of successes, length 2 vector of success and failure counts, a formula, or a character, numeric, or factor vector containing raw data.

n

sample size (successes + failures) or a data frame (for the formula interface)

p

probability for null hypothesis

alternative

type of alternative hypothesis

conf.level

confidence level for confidence interval

ci.method

a method to use for computing the confidence interval (case insensitive and may be abbreviated). See details below.

data

a data frame (if missing, n may be a data frame)

success

level of variable to be considered success. All other levels are considered failure.

...

additional arguments (often ignored)

Value

an object of class htest

Details

binom.test() is a wrapper around stats::binom.test() from the stats package to simplify its use when the raw data are available, in which case an extended syntax for binom.test() is provided. See the examples.

Also, five confidence interval methods are provided: * "Clopper-Pearson", "binom.test": This is the interval produced when using stats::binom.test() from the stats package. It guarantees a coverage rate at least as large as the nominal coverage rate, but may produce wider intervals than some of the methods below, which may either under- or over-cover depending on the data.

  • `"Score", "Wilson", "prop.test": This is the usual method used by stats::prop.test() and is computed by inverting p-values from score tests. It is often attributed to Edwin Wilson. If specified with "prop.test", the continuity correction is applied (as is the default in prop.test()), else the continuity correction is not applied.

    • "Wald" This is the interval traditionally taught in entry level statistics courses. It uses the sample proportion to estimate the standard error and uses normal theory to determine how many standard deviations to add and/or subtract from the sample proportion to determine an interval.

    • \"Agresti-Coull"` This is the Wald method after setting \(n' = n + z^2\) and \(p'= (x + z^2/2) / n\)' and using \(x' = n' p'\) and \(n'\) in place of \(x\) and \(n\).

    • "Plus4" This is Wald after adding in two artificial success and two artificial failures. It is nearly the same as the Agresti-Coull method when the confidence level is 95%. since \(z^2\) is approximately 4 and \(z^2/2\) is approximately 2.

Note

When x is a 0-1 vector, 0 is treated as failure and 1 as success. Similarly, for a logical vector TRUE is treated as success and FALSE as failure.

Examples

# Several ways to get a confidence interval for the proportion of Old Faithful
# eruptions lasting more than 3 minutes.
data(faithful)
binom.test(faithful$eruptions > 3)
#> 
#> 
#> 
#> data:  faithful$eruptions > 3  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5832982 0.7003038
#> sample estimates:
#> probability of success 
#>              0.6433824 
#> 
binom.test(97, 272)
#> 
#> 
#> 
#> data:  97 out of 272
#> number of successes = 97, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.2996962 0.4167018
#> sample estimates:
#> probability of success 
#>              0.3566176 
#> 
binom.test(c(97, 272-97))
#> 
#> 
#> 
#> data:  c(97, 272 - 97)
#> number of successes = 97, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.2996962 0.4167018
#> sample estimates:
#> probability of success 
#>              0.3566176 
#> 
faithful$long <- faithful$eruptions > 3
binom.test(faithful$long)
#> 
#> 
#> 
#> data:  faithful$long  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5832982 0.7003038
#> sample estimates:
#> probability of success 
#>              0.6433824 
#> 
binom.test(resample(1:4, 400), p=.25)
#> 
#> 
#> 
#> data:  resample(1:4, 400)  [with success = 1]
#> number of successes = 92, number of trials = 400, p-value = 0.3865
#> alternative hypothesis: true probability of success is not equal to 0.25
#> 95 percent confidence interval:
#>  0.1896388 0.2744081
#> sample estimates:
#> probability of success 
#>                   0.23 
#> 
binom.test(~ long, data = faithful)
#> 
#> 
#> 
#> data:  faithful$long  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5832982 0.7003038
#> sample estimates:
#> probability of success 
#>              0.6433824 
#> 
binom.test(~ long, data = faithful, ci.method = "Wald")
#> 
#> 	Exact binomial test (Wald CI)
#> 
#> data:  faithful$long  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5864578 0.7003069
#> sample estimates:
#> probability of success 
#>              0.6433824 
#> 
binom.test(~ long, data = faithful, ci.method = "Plus4")
#> 
#> 	Exact binomial test (Plus 4 CI)
#> 
#> data:  faithful$long  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5847210 0.6978877
#> sample estimates:
#> probability of success 
#>              0.6433824 
#> 
with(faithful, binom.test(~long))
#> 
#> 
#> 
#> data:  NULL$long  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5832982 0.7003038
#> sample estimates:
#> probability of success 
#>              0.6433824 
#> 
with(faithful, binom.test(long))
#> 
#> 
#> 
#> data:  long  [with success = TRUE]
#> number of successes = 175, number of trials = 272, p-value = 2.609e-06
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#>  0.5832982 0.7003038
#> sample estimates:
#> probability of success 
#>              0.6433824 
#>