简体   繁体   English

R中的置信区间(CI)模拟:如何?

[英]Confidence Interval (CI) simulation in R: How?

I was wondering how I could check via simulation in R that the 95% Confidence Interval obtained from a binomial test with 5 successes in 15 trials when TRUE p = .5 has a 95% "Coverage Probability" in the long-run? 我想知道如何通过R中的仿真检查,当TRUE p = .5从长期来看具有95%的“覆盖概率”时,从15次试验中获得5次成功的二项式测试获得的95%置信区间?

Here is the 95% CI for such a test using R (how can show that the following CI has a 95% coverage in the long-run if TRUE p = .5 ): 这是使用R进行此类测试的95%CI(如果TRUE p = .5,那么从长远来看,如何证明以下CI具有95%的覆盖率):

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?)

Something like this? 像这样吗

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()))

Note that binom.test when called with 5 successes, 15 trials and p = 0.5 will always return the same value, hence the call to rbinom . 请注意,以5次成功,15次试验和p = 0.5调用binom.test时,始终返回相同的值,因此调用rbinom The number of successes will vary. 成功次数将有所不同。 We can compute the proportion of cases when p is between Lower and Upper . 我们可以计算pLowerUpper之间的情况下的比例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM