[英]Binomial probability mass function with confidence interval
The following question we need to solve.下面的问题是我们需要解决的。
Consider the following binomial probability mass function (pmf)
:考虑以下
binomial probability mass function (pmf)
:
f(x;m,p) = (m¦x) p^x * (1-p)^(mx)
, for x = 0, 1, 2,.....,m,
and otherwise equal to 0
. f(x;m,p) = (m¦x) p^x * (1-p)^(mx)
,对于x = 0, 1, 2,.....,m,
否则等于0
. Let X_1, X_2,....,Xn
be independent and identically distributed random samples from f(x;m = 20; p = 0:45)
.设
X_1, X_2,....,Xn
是来自f(x;m = 20; p = 0:45)
独立同分布随机样本。
1) Assume n = 15 and calculate the 95% confidence interval on p using the p-hat = Σ_(i=1)^n X_i/mn
(an estimator of p). 1) 假设 n = 15 并使用
p-hat = Σ_(i=1)^n X_i/mn
(p 的估计量)计算 p 的 95% 置信区间。 Simulate these confidence intervals 10000 times and count how often the parameter value p lies within these 10000 confidence intervals.模拟这些置信区间 10000 次,并计算参数值 p 位于这 10000 个置信区间内的频率。
m <- 20
p <- 0.45
n <- 15
x <- m
nsim <- 10000
counter <- 0
for (i in 1:nsim) {
bpmf <- rbinom(x,m,p)
esti_p <- bpmf/(m*n)
var_bpmf <- var(bpmf)
CI_lower <- esti_p - qnorm(0.975)*sqrt(var_bpmf/n)
CI_upper <- esti_p + qnorm(0.975)*sqrt(var_bpmf/n)
if ((CI_lower<p) & (CI_upper>p)) counter <- counter + 1
}
It doesn't work properly and I don't see what I'm doing wrong.它不能正常工作,我看不出我做错了什么。 Is there anyone who can help me with this?
有没有人可以帮我解决这个问题?
When I run my code, I believe the answer now is right, but it gives the following sentence: "There were 50 or more warnings (use warnings() to see the first 50)" When I run this it will give:当我运行我的代码时,我相信现在的答案是正确的,但它给出了以下句子:“有 50 个或更多警告(使用警告()查看前 50 个)”当我运行它时,它会给出:
"1: In if ((CI_lower < p) & (CI_upper > p)) counter <- counter + ... :
the condition has length > 1 and only the first element will be used".
Also I don't know for sure if;我也不确定是否;
CI_lower <- esti_p - qnorm(0.975)*sqrt(var_bpmf/n)
CI_upper <- esti_p + qnorm(0.975)*sqrt(var_bpmf/n)
is the right formula to calculate the confidence interval.是计算置信区间的正确公式。
m <- 20
p <- 0.45
nsim <- 10000
bpmf <- rbinom(size=m,prob=p,n=nsim)
esti_p <- bpmf/m
var_bpmf <- esti_p*(1-esti_p)/m
CI_lower <- esti_p - qnorm(0.975)*sqrt(var_bpmf)
CI_upper <- esti_p + qnorm(0.975)*sqrt(var_bpmf)
counter <-((CI_lower<p) & (CI_upper>p))
table(counter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.