如何计算 R 中某个比例的 95% 置信区间?
How to calculate 95% confidence interval for a proportion in R?
假设我拥有一家工厂,每天生产 150 个螺丝,错误率为 22%。现在我要估算一年(365 天)每天有多少螺丝有问题
rbinom(n = 365, size = 150, prob = 0.22)
这样生成365个值
45 31 35 31 34 37 33 41 37 37 26 32 37 38 39 35 44 36 25 27 32 25 30 33 25 37 36 31 32 32 43 42 32 33 33 38 26 24 ...................
现在,对于生成的每个值,我应该计算出每天故障螺钉比例的 95% 置信区间。
我不确定我该怎么做。是否有任何内置函数(我不应该使用任何包)或者我应该创建一个新函数?
如果每天的试验次数足够多,失败的概率不会太极端,那么可以使用正态近似https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval。
# number of failures, for each of the 365 days
f <- rbinom(365, size = 150, prob = 0.22)
# failure rates
p <- f/150
# confidence interval for the failur rate, for each day
p + 1.96*sqrt((p*(1-p)/150))
p - 1.96*sqrt((p*(1-p)/150))
假设我拥有一家工厂,每天生产 150 个螺丝,错误率为 22%。现在我要估算一年(365 天)每天有多少螺丝有问题
rbinom(n = 365, size = 150, prob = 0.22)
这样生成365个值
45 31 35 31 34 37 33 41 37 37 26 32 37 38 39 35 44 36 25 27 32 25 30 33 25 37 36 31 32 32 43 42 32 33 33 38 26 24 ...................
现在,对于生成的每个值,我应该计算出每天故障螺钉比例的 95% 置信区间。
我不确定我该怎么做。是否有任何内置函数(我不应该使用任何包)或者我应该创建一个新函数?
如果每天的试验次数足够多,失败的概率不会太极端,那么可以使用正态近似https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval。
# number of failures, for each of the 365 days
f <- rbinom(365, size = 150, prob = 0.22)
# failure rates
p <- f/150
# confidence interval for the failur rate, for each day
p + 1.96*sqrt((p*(1-p)/150))
p - 1.96*sqrt((p*(1-p)/150))