链接中公式的下限不能为负数。但是您链接的间隔不是Agresti-Coull 间隔,而是Wilson 间隔。您链接中的公式适用于所谓的威尔逊区间,而不是Agresti-Coull 区间。Agresti 和 Coull在他们的论文中列出了链接中的公式,并将其称为分数置信区间(第 120 页)。在 Brown 等人的精彩论文中。(2001)二项式比例的区间估计,称为威尔逊区间。它通常被称为威尔逊区间。他们在他们的文章中表明,即使n很小,Wilson 区间也表现良好. 在输出中binom.confint,Wilson 区间表示为wilson并且可以通过将方法设置为 来methods="wilson"计算binom.confint。这是R(修改后的)威尔逊置信区间的代码:
n <- 5
x <- 0
alpha <- 0.05
p.hat <- x/n
upper.lim <- (p.hat + (qnorm(1-(alpha/2))^2/(2*n)) + qnorm(1-(alpha/2)) * sqrt(((p.hat*(1-p.hat))/n) + (qnorm(1-(alpha/2))^2/(4*n^2))))/(1 + (qnorm(1-(alpha/2))^2/(n)))
lower.lim <- (p.hat + (qnorm(alpha/2)^2/(2*n)) + qnorm(alpha/2) * sqrt(((p.hat*(1-p.hat))/n) + (qnorm(alpha/2)^2/(4*n^2))))/(1 + (qnorm(alpha/2)^2/(n)))
#==============================================================================
# Modification for probabilities close to boundaries
#==============================================================================
if ((n <= 50 & x %in% c(1, 2)) | (n >= 51 & n <= 100 & x %in% c(1:3))) {
lower.lim <- 0.5 * qchisq(alpha, 2 * x)/n
}
if ((n <= 50 & x %in% c(n - 1, n - 2)) | (n >= 51 & n <= 100 & x %in% c(n - (1:3)))) {
upper.lim <- 1 - 0.5 * qchisq(alpha, 2 * (n - x))/n
}
upper.lim
[1] 0.4344825
lower.lim
[1] 3.139253e-17
这里,下限显然是 0(其余为数值错误)。的输出中的威尔逊区间binom.confint可以通过设置选项来计算,methods="wilson"这与我们上面计算的相同:
library(binom)
binom.confint(x=0, n=5, methods="wilson")
method x n mean lower upper
1 wilson 0 5 0 3.139253e-17 0.4344825
该函数binom.confint实现了Wikipedia 页面上给出的 Agresti-Coull 区间公式:
n.hat <- n + qnorm(1-(alpha/2))^2
p.hat <- (1/n.hat) * (x + (1/2)*qnorm(1-(alpha/2))^2)
upper.lim2 <- p.hat + qnorm(1-(alpha/2))*sqrt((1/n.hat)*p.hat*(1-p.hat))
lower.lim2 <- p.hat - qnorm(1-(alpha/2))*sqrt((1/n.hat)*p.hat*(1-p.hat))
upper.lim2
[1] 0.4890549
lower.lim2
[1] -0.05457239
binom.confint它们与带有选项的那些相同methods="ac":
library(binom)
binom.confint(x=0, n=5, methods="ac")
method x n mean lower upper
1 agresti-coull 0 5 0 -0.05457239 0.4890549