Title: | Binomial Confidence Intervals for Several Parameterizations |
---|---|
Description: | Constructs confidence intervals on the probability of success in a binomial experiment via several parameterizations. |
Authors: | Sundar Dorai-Raj <[email protected]> |
Maintainer: | Sundar Dorai-Raj <[email protected]> |
License: | GPL |
Version: | 1.1-1.1 |
Built: | 2024-12-11 07:23:26 UTC |
Source: | CRAN |
Uses a beta prior on the probability of success for a binomial distribution, determines a two-sided confidence interval from a beta posterior. A plotting function is also provided to show the probability regions defined by each confidence interval.
binom.bayes(x, n, conf.level = 0.95, type = c("highest", "central"), prior.shape1 = 0.5, prior.shape2 = 0.5, tol = .Machine$double.eps^0.5, maxit = 1000, ...) binom.bayes.densityplot(bayes, npoints = 500, fill.central = "lightgray", fill.lower = "steelblue", fill.upper = fill.lower, alpha = 0.8, ...)
binom.bayes(x, n, conf.level = 0.95, type = c("highest", "central"), prior.shape1 = 0.5, prior.shape2 = 0.5, tol = .Machine$double.eps^0.5, maxit = 1000, ...) binom.bayes.densityplot(bayes, npoints = 500, fill.central = "lightgray", fill.lower = "steelblue", fill.upper = fill.lower, alpha = 0.8, ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
type |
The type of confidence interval (see Details). |
prior.shape1 |
The value of the first shape parameter to be used in the prior beta. |
prior.shape2 |
The value of the second shape parameter to be used in the prior beta. |
tol |
A tolerance to be used in determining the highest probability density interval. |
maxit |
Maximum number of iterations to be used in determining the highest probability interval. |
bayes |
The output |
npoints |
The number of points to use to draw the density curves. Higher numbers give smoother densities. |
fill.central |
The color for the central density. |
fill.lower , fill.upper
|
The color(s) for the upper and lower density. |
alpha |
The alpha value for controlling transparency. |
... |
Ignored. |
Using the conjugate beta prior on the distribution of p (the probability of success) in a binomial experiment, constructs a confidence interval from the beta posterior. From Bayes theorem the posterior distribution of p given the data x is:
p|x ~ Beta(x + prior.shape1, n - x + prior.shape2)
The default prior is Jeffrey's prior which is a Beta(0.5, 0.5)
distribution. Thus the posterior mean is (x + 0.5)/(n + 1)
.
The default type of interval constructed is "highest" which computes the highest probability density (hpd) interval which assures the shortest interval possible. The hpd intervals will achieve a probability that is within tol of the specified conf.level. Setting type to "central" constructs intervals that have equal tail probabilities.
If 0 or n successes are observed, a one-sided confidence interval is returned.
For binom.bayes
, a data.frame
containing the observed
proportions and the lower and upper bounds of the confidence interval.
For binom.bayes.densityplot
, a ggplot
object that can
printed to a graphics device, or have additional layers added.
Sundar Dorai-Raj ([email protected])
Gelman, A., Carlin, J. B., Stern, H. S., and Rubin, D. B. (1997) Bayesian Data Analysis, London, U.K.: Chapman and Hall.
binom.confint
, binom.cloglog
,
binom.logit
, binom.probit
# Example using highest probability density. hpd <- binom.bayes( x = 0:10, n = 10, type = "highest", conf.level = 0.8, tol = 1e-9) print(hpd) binom.bayes.densityplot(hpd) # Remove the extremes from the plot since they make things hard # to see. binom.bayes.densityplot(hpd[hpd$x != 0 & hpd$x != 10, ]) # Example using central probability. central <- binom.bayes( x = 0:10, n = 10, type = "central", conf.level = 0.8, tol = 1e-9) print(central) binom.bayes.densityplot(central) # Remove the extremes from the plot since they make things hard # to see. binom.bayes.densityplot(central[central$x != 0 & central$x != 10, ])
# Example using highest probability density. hpd <- binom.bayes( x = 0:10, n = 10, type = "highest", conf.level = 0.8, tol = 1e-9) print(hpd) binom.bayes.densityplot(hpd) # Remove the extremes from the plot since they make things hard # to see. binom.bayes.densityplot(hpd[hpd$x != 0 & hpd$x != 10, ]) # Example using central probability. central <- binom.bayes( x = 0:10, n = 10, type = "central", conf.level = 0.8, tol = 1e-9) print(central) binom.bayes.densityplot(central) # Remove the extremes from the plot since they make things hard # to see. binom.bayes.densityplot(central[central$x != 0 & central$x != 10, ])
Uses the complementary log (cloglog) parameterization on the observed proportion to construct confidence intervals.
binom.cloglog(x, n, conf.level = 0.95, ...)
binom.cloglog(x, n, conf.level = 0.95, ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
... |
ignored |
For derivations see doc/binom.pdf.
A data.frame
containing the observed
proportions and the lower and upper bounds of the confidence
interval.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
,
binom.logit
, binom.probit
, binom.coverage
binom.cloglog(x = 0:10, n = 10)
binom.cloglog(x = 0:10, n = 10)
Uses eight different methods to obtain a confidence interval on the binomial probability.
binom.confint(x, n, conf.level = 0.95, methods = "all", ...)
binom.confint(x, n, conf.level = 0.95, methods = "all", ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
methods |
Which method to use to construct the interval. Any
combination of |
... |
Additional arguments to be passed to |
Nine methods are allowed for constructing the confidence interval(s):
exact
- Pearson-Klopper method. See also
binom.test
.
asymptotic
- the text-book definition for confidence
limits on a single proportion using the Central Limit Theorem.
agresti-coull
- Agresti-Coull method. For a 95% confidence
interval, this method does not use the concept of "adding 2
successes and 2 failures," but rather uses the formulas explicitly
described in the following link:
http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Agresti-Coull_Interval.
wilson
- Wilson method.
prop.test
- equivalent to prop.test(x = x, n = n,
conf.level = conf.level)$conf.int
.
bayes
- see binom.bayes
.
logit
- see binom.logit
.
cloglog
- see binom.cloglog
.
probit
- see binom.probit
.
profile
- see binom.profile
.
By default all eight are estimated for each value of x
and/or
n
. For the "logit", "cloglog", "probit", and "profile"
methods, the cases where x == 0
or x == n
are treated
separately. Specifically, the lower bound is replaced by
(alpha/2)^n
and the upper bound is replaced by (1-alpha/2)^n
.
A data.frame
containing the observed proportions and
the lower and upper bounds of the confidence interval for all the
methods in "methods"
.
Sundar Dorai-Raj ([email protected])
A. Agresti and B.A. Coull (1998), Approximate is better than "exact" for interval estimation of binomial proportions, American Statistician, 52:119-126.
R.G. Newcombe, Logit confidence intervals and the inverse sinh transformation (2001), American Statistician, 55:200-202.
L.D. Brown, T.T. Cai and A. DasGupta (2001), Interval estimation for a binomial proportion (with discussion), Statistical Science, 16:101-133.
Gelman, A., Carlin, J. B., Stern, H. S., and Rubin, D. B. (1997) Bayesian Data Analysis, London, U.K.: Chapman and Hall.
binom.bayes
, binom.logit
,
binom.probit
, binom.cloglog
,
binom.coverage
, prop.test
,
binom.test
for comparison to method
"exact"
binom.confint(x = c(2, 4), n = 100, tol = 1e-8)
binom.confint(x = c(2, 4), n = 100, tol = 1e-8)
Determines the probability coverage for a binomial confidence interval.
binom.coverage(p, n, conf.level = 0.95, method = "all", ...)
binom.coverage(p, n, conf.level = 0.95, method = "all", ...)
p |
The (true) probability of success in a binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
method |
Either a character string to be passed to
|
... |
Additional parameters to be passed to
|
Derivations are based on the results given in the references. Methods
whose coverage probabilities are consistently closer to 0.95 are more
desireable. Thus, Wilson's, logit, and cloglog appear to be good for
this sample size, while Jeffreys, asymptotic, and prop.test are
poor. Jeffreys is a variation of Bayes using prior shape parameters of
0.5 and having equal probabilities in the tail. The Jeffreys'
equal-tailed interval was created using binom.bayes using (0.5,0.5) as
the prior shape parameters and type = "central"
.
A data.frame
containing the "method"
used, "n"
, "p"
,
and the coverage probability, C(p,n)
.
Sundar Dorai-Raj ([email protected])
L.D. Brown, T.T. Cai and A. DasGupta (2001), Interval estimation for a binomial proportion (with discussion), Statistical Science, 16:101-133.
L.D. Brown, T.T. Cai and A. DasGupta (2002), Confidence Intervals for a Binomial Proportion and Asymptotic Expansions, Annals of Statistics, 30:160-201.
binom.coverage(p = 0.5, n = 50)
binom.coverage(p = 0.5, n = 50)
Determines the expected length for a binomial confidence interval.
binom.length(p, n, conf.level = 0.95, method = "all", ...)
binom.length(p, n, conf.level = 0.95, method = "all", ...)
p |
The (true) probability of success in a binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
method |
Either a character string to be passed to
|
... |
Additional parameters to be passed to
|
Derivations are based on the results given in the references. Methods
whose length probabilities are consistently closer to 0.95 are more
desireable. Thus, Wilson's, logit, and cloglog appear to be good for
this sample size, while Jeffreys, asymptotic, and prop.test are
poor. Jeffreys is a variation of Bayes using prior shape parameters of
0.5 and having equal probabilities in the tail. The Jeffreys'
equal-tailed interval was created using binom.bayes using (0.5,0.5) as
the prior shape parameters and type = "central"
.
A data.frame
containing the "method"
used, "n"
, "p"
,
and the average length, L(p,n)
.
Sundar Dorai-Raj ([email protected])
L.D. Brown, T.T. Cai and A. DasGupta (2001), Interval estimation for a binomial proportion (with discussion), Statistical Science, 16:101-133.
L.D. Brown, T.T. Cai and A. DasGupta (2002), Confidence Intervals for a Binomial Proportion and Asymptotic Expansions, Annals of Statistics, 30:160-201.
binom.length(p = 0.5, n = 50)
binom.length(p = 0.5, n = 50)
Uses the logistic (logit) parameterization on the observed proportion to construct confidence intervals.
binom.logit(x, n, conf.level = 0.95, ...)
binom.logit(x, n, conf.level = 0.95, ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
... |
ignored |
For derivations see doc/binom.pdf.
A data.frame
containing the observed
proportions and the lower and upper bounds of the confidence
interval.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
,
binom.logit
, binom.probit
, binom.coverage
binom.logit(x = 0:10, n = 10)
binom.logit(x = 0:10, n = 10)
Uses the lrt likelihood on the observed proportion to construct confidence intervals.
binom.lrt(x, n, conf.level = 0.95, bayes = FALSE, conf.adj = FALSE, plot = FALSE, ...)
binom.lrt(x, n, conf.level = 0.95, bayes = FALSE, conf.adj = FALSE, plot = FALSE, ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
bayes |
logical; if |
conf.adj |
logical; if |
plot |
logical; if |
... |
ignored |
Confidence intervals are based on profiling the binomial deviance in the
neighbourhood of the MLE. If x == 0
or x == n
and
bayes
is TRUE
, then a Bayesian adjustment is made to move
the log-likelihood function away from Inf
. Specifically, these
values are replaced by (x + 0.5)/(n + 1)
, which is the posterier
mode of f(p|x)
using Jeffrey's prior on p
. Furthermore, if
conf.adj
is TRUE
, then the upper (or lower) bound uses
a 1 - alpha
confidence level. Typically, the
observed mean will not be inside the estimated confidence interval.
If bayes
is FALSE
, then the Clopper-Pearson exact method
is used on the endpoints. This tends to make confidence intervals at the
end too conservative, though the observed mean is guaranteed to be
within the estimated confidence limits.
A data.frame
containing the observed
proportions and the lower and upper bounds of the confidence
interval.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
, binom.cloglog
,
binom.logit
, binom.probit
, binom.coverage
,
confint
in package MASS,
family
, glm
binom.lrt(x = 0:10, n = 10)
binom.lrt(x = 0:10, n = 10)
Uses optimization to minimize the integrated mean squared error between the calculated coverage and the desired confidence level for a given binomial confidence interval.
binom.optim(n, conf.level = 0.95, method = binom.lrt, k = n%/%2 + 1, p0 = 0, transform = TRUE, plot = FALSE, tol = .Machine$double.eps^0.5, start = NULL, ...)
binom.optim(n, conf.level = 0.95, method = binom.lrt, k = n%/%2 + 1, p0 = 0, transform = TRUE, plot = FALSE, tol = .Machine$double.eps^0.5, start = NULL, ...)
n |
The number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
method |
The method used to estimate the confidence interval. |
k |
See Details. |
p0 |
The minimum probability of success to allow in the optimization. See Details. |
transform |
logical; If |
plot |
logical; If |
tol |
The minimum significance level to allow in the optimization. See Details. |
start |
A starting value on the optimal confidence level. |
... |
Additional arguments to pass to |
This function minimizes the squared error between the expected coverage probability and the desired confidence level.
The optimizer will adjust confidence intervals for all x
=
0
to n
depending on the value of k
provided. If
k
is one, only the confidence levels for x
= 0
and
n
are adjusted. If k
= [n/2]
then all confidence
intervals are adjusted. This assumes the confidence intervals are the
same length for x
= x[k]
and x[n - k + 1]
, which is
the case for all methods provided in this package except
binom.cloglog
.
A list
with the following elements:
par |
Final confidence levels. The length of this vector is
|
value |
The final minimized value from |
counts |
The number of function and gradient calls from
|
convergence |
Convergence code from |
message |
Any message returned by the L-BFGS-B or BFGS optimizer. |
confint |
A |
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.plot
,
binom.coverage
, optim
binom.optim(10, k = 1) ## determine optimal significance for x = 0, 10 only binom.optim(3, method = binom.wilson) ## determine optimal significance for all x
binom.optim(10, k = 1) ## determine optimal significance for x = 0, 10 only binom.optim(3, method = binom.wilson) ## determine optimal significance for all x
Constructs coverage plots for binomial confidence intervals.
binom.plot(n, method = binom.lrt, np = 500, conf.level = 0.95, actual = conf.level, type = c("xyplot", "levelplot"), tol = .Machine$double.eps^0.5, ...)
binom.plot(n, method = binom.lrt, np = 500, conf.level = 0.95, actual = conf.level, type = c("xyplot", "levelplot"), tol = .Machine$double.eps^0.5, ...)
n |
The number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
np |
Number of points to use in the plot. |
method |
The method used to estimate the confidence interval. |
actual |
The actual confidence interval used in the confidence interval. See Details. |
type |
See Details. |
tol |
The minimum probability of success to use in the plot. |
... |
Additional arguments to pass to
|
If type
is "xyplot"
, a line plot is created with
coverage on the y-axis and binomial probability on the x-axis.
A separate panel for every n
is provided. If actual
is
provided then a horizontal reference line is added to the plot. This
is only useful when actual
is different from conf.level
,
as is the case when calling binom.optim
.
If type
is "levelplot"
, a image plot is created with
x
= 0
to n
on the vertical axis and binomial
probability on the horizontal axis. Each row in the plot will be the
confidence level for a given x
. The color of the confidence
interval is determined by the coverage probability. The argument
n
must only be of length one. If not, only the first n
will be used and a warning is issued.
In either plot type, the number of points at which the coverage
probability is determined is specified by np
. Increasing
np
gives a finer granularity but performance will suffer.
An object of class trellis
.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.optim
,
xyplot
, levelplot
binom.plot(5, type = "levelplot") binom.plot(c(3, 5, 10, 25), type = "xyplot")
binom.plot(5, type = "levelplot") binom.plot(c(3, 5, 10, 25), type = "xyplot")
Uses Wald statistics to compute power curves for several parameterizations.
binom.power(p.alt, n = 100, p = 0.5, alpha = 0.05, phi = 1, alternative = c("two.sided", "greater", "less"), method = c("cloglog", "logit", "probit", "asymp", "lrt", "exact"))
binom.power(p.alt, n = 100, p = 0.5, alpha = 0.05, phi = 1, alternative = c("two.sided", "greater", "less"), method = c("cloglog", "logit", "probit", "asymp", "lrt", "exact"))
p.alt |
A vector of success probabilities under the alternative hypothesis. |
n |
A vector representing the number of independent trials in the binomial experiment. |
p |
A vector of success probabilities under the null hypothesis. |
alpha |
A vector of type-I error rates. |
phi |
A vector determining the overdispersion parameter for each binomial experiment. |
alternative |
Type of alternative hypothesis. |
method |
The method used to compute power. |
For derivations see doc/binom.pdf. p.alt
, n
,
p
, alpha
, and phi
can all be vectors. The length
of each argument will be expanded to the longest length. The function
assumes the lengths are equal or can be wrapped for multiple values.
The estimated probability of detecting the difference between
p.alt
and p
.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
,
binom.logit
, binom.probit
, binom.coverage
binom.power(0.95, alternative = "greater")
binom.power(0.95, alternative = "greater")
Uses the probit parameterization on the observed proportion to construct confidence intervals.
binom.probit(x, n, conf.level = 0.95, ...)
binom.probit(x, n, conf.level = 0.95, ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
... |
ignored |
For derivations see doc/binom.pdf.
A data.frame
containing the observed
proportions and the lower and upper bounds of the confidence
interval.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
,
binom.probit
, binom.logit
, binom.coverage
binom.probit(x = 0:10, n = 10)
binom.probit(x = 0:10, n = 10)
Uses the profile likelihood on the observed proportion to construct confidence intervals.
binom.profile(x, n, conf.level = 0.95, maxsteps = 50, del = zmax/5, bayes = TRUE, plot = FALSE, ...)
binom.profile(x, n, conf.level = 0.95, maxsteps = 50, del = zmax/5, bayes = TRUE, plot = FALSE, ...)
x |
Vector of number of successes in the binomial experiment. |
n |
Vector of number of independent trials in the binomial experiment. |
conf.level |
The level of confidence to be used in the confidence interval. |
maxsteps |
The maximum number of steps to take in the profiles. |
del |
The size of the step to take |
bayes |
logical; if |
plot |
logical; if |
... |
ignored |
Confidence intervals are based on profiling the binomial deviance in the
neighbourhood of the MLE. If x == 0
or x == n
and
bayes
is TRUE
, then a Bayesian adjustment is made to move
the log-likelihood function away from Inf
. Specifically, these
values are replaced by (x + 0.5)/(n + 1)
, which is the posterier
mode of f(p|x)
using Jeffrey's prior on p
. Typically, the
observed mean will not be inside the estimated confidence interval.
If bayes
is FALSE
, then the Clopper-Pearson exact method
is used on the endpoints. This tends to make confidence intervals at the
end too conservative, though the observed mean is guaranteed to be
within the estimated confidence limits.
A data.frame
containing the observed
proportions and the lower and upper bounds of the confidence
interval.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
, binom.cloglog
,
binom.logit
, binom.probit
, binom.coverage
,
confint
in package MASS,
family
, glm
binom.profile(x = 0:10, n = 10)
binom.profile(x = 0:10, n = 10)
Simulates binomial data for testing confidence interval coverage.
binom.sim(M = 200, n = 100, p = 0.5, conf.level = 0.95, methods = "all", ...)
binom.sim(M = 200, n = 100, p = 0.5, conf.level = 0.95, methods = "all", ...)
M |
Number of simulations to create. |
n |
Vector of number of independent trials in the binomial experiment. |
p |
Probability of success under the null hypothesis. |
conf.level |
The level used in computing the confidence interval. |
methods |
The method used to compute power. |
... |
Additional arguments to pass to |
M
binomial observations are created using rbinom(M, n,
p)
. The average number of times a confidence interval covers
p
is returned.
The estimated coverage based on which method is requested.
Sundar Dorai-Raj ([email protected])
binom.confint
, binom.bayes
,
binom.logit
, binom.probit
, binom.coverage
binom.sim(p = 0.95, method = "lrt")
binom.sim(p = 0.95, method = "lrt")
Power and sample size for a binomial proportion using the cloglog parameterization.
cloglog.sample.size(p.alt, n = NULL, p = 0.5, power = 0.8, alpha = 0.05, alternative = c("two.sided", "greater", "less"), exact.n = FALSE, recompute.power = FALSE, phi = 1)
cloglog.sample.size(p.alt, n = NULL, p = 0.5, power = 0.8, alpha = 0.05, alternative = c("two.sided", "greater", "less"), exact.n = FALSE, recompute.power = FALSE, phi = 1)
p.alt |
The alternative proportion in a one-sample test. |
n |
The sample size in a one-sample test. |
p |
The null proportion in a one-sample test. Default is 0.5. |
power |
The desired power level. Default is 0.80. |
alpha |
The desired alpha level - probability of a Type I error. Default is 0.05. |
alternative |
Nature of alternative hypothesis. One of "two.sided", "greater", "less". |
exact.n |
logical; If |
recompute.power |
logical; If |
phi |
Dispersion parameter by which to inflate ( |
This function can be used to calculate sample size, power or minimum
detectable difference. It determines what to compute base on the
arguments provided. If p.alt
is given, but n
is not, then
sample size is computed. If p.alt
is given along with n
,
then the power is computed. If only n
is provided, the minimum
detectable difference is computed using the default power of 0.80.
A data.frame
containing the power, sample size and all of the
input which was used to perform the computations.
Sundar Dorai-Raj ([email protected])
cloglog.sample.size(p.alt = 0.8) cloglog.sample.size(n = 20) cloglog.sample.size(n = 20, power = 0.9)
cloglog.sample.size(p.alt = 0.8) cloglog.sample.size(n = 20) cloglog.sample.size(n = 20, power = 0.9)
A Tcl/Tk graphics wrapper for binom.power
.
tkbinom.power()
tkbinom.power()
A wrapper for binom.power
that creates power curves
based on user input.
Sundar Dorai-Raj ([email protected])
binom.power
, binom.confint
, binom.bayes
,
binom.logit
, binom.probit
, binom.coverage
binom.power(0.95, alternative = "greater")
binom.power(0.95, alternative = "greater")