二项分布的置信区间:使用 `qbinom` 和 `binom.test` 的不同结果

问题描述

我希望使用 qbinombinom.test 计算某个比例的 CI 的结果相同,但实际上它们略有不同:

success <- 360
n <- 1226
lci <- qbinom(0.025,n,success/n)/n
uci <- qbinom(0.975,success/n)/n
c(lci,uci)

[1] 0.2683524 0.3189233

binom.test(success,success/n)$conf.int

[1] 0.2682571 0.3200123

我在这里遗漏了什么?

解决方法

binom.test 函数的源代码使用 qbeta 而不是 qbinom,因为这是 exact binomial confidence intervals 的公认公式。其中 x 是成功次数,binom.test 给出的 lci 和 uci 为:

p.L <- function(x,alpha) {
    if (x == 0) 
        0
    else qbeta(alpha,x,n - x + 1)
}
p.U <- function(x,alpha) {
    if (x == n) 
        1
    else qbeta(1 - alpha,x + 1,n - x)
}

alpha <- (1 - 0.95)/2
lci <- p.L(x,alpha)
uci <- p.U(x,alpha)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...