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, ...)count of successes, length 2 vector of success and failure counts, a formula, or a character, numeric, or factor vector containing raw data.
sample size (successes + failures) or a data frame (for the formula interface)
probability for null hypothesis
type of alternative hypothesis
confidence level for confidence interval
a method to use for computing the confidence interval (case insensitive and may be abbreviated). See details below.
a data frame (if missing, n may be a data frame)
level of variable to be considered success. All other levels are considered failure.
additional arguments (often ignored)
an object of class htest
binom.test is a wrapper around binom.test() from the base
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:
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.
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.
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.
This is the Wald method after setting
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
# NOT RUN {
# 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)
binom.test(97, 272)
binom.test(c(97, 272-97))
faithful$long <- faithful$eruptions > 3
binom.test(faithful$long)
binom.test(resample(1:4, 400), p=.25)
binom.test(~ long, data = faithful)
binom.test(~ long, data = faithful, ci.method = "Wald")
binom.test(~ long, data = faithful, ci.method = "Plus4")
with(faithful, binom.test(~long))
with(faithful, binom.test(long))
# }
Run the code above in your browser using DataLab