R 中的置信区间 (CI) 模拟:怎么样?

Confidence Interval (CI) simulation in R: How?

我想知道如何通过 R 中的模拟检查在 TRUE p = .5[ 时 15 次试验中 5 次成功的二项式检验获得的 95% 置信区间=19=] 有 95% "Coverage Probability" 在 long-运行?

这是使用 R 进行此类测试的 95% CI(如果 真 p = .5):

as.numeric(binom.test(x = 5, n = 15, p = .5)[[4]])
# > [1] 0.1182411 0.6161963 (in the long-run 95% of the time, ".5" is contained within these
#                            two numbers, how to show this in R?)

是这样的吗?

fun <- function(n = 15, p = 0.5){
    x <- rbinom(1, size = n, prob = p)
    res <- binom.test(x, n, p)[[4]]
    c(Lower = res[1], Upper = res[2])
}

set.seed(3183)
R <- 10000
sim <- t(replicate(R, fun()))

请注意 binom.test 当调用 5 次成功时,15 次试验和 p = 0.5 将始终 return 相同的值,因此调用 rbinom。成功的次数会有所不同。我们可以计算 pLowerUpper 之间的情况的比例。

cov <- mean(sim[,1] <= .5 & .5 <= sim[,2])