binomial <- function(nmax = 100,
thr = 0.95,
alpha = 1,
beta = 1,
p_true = 0.5,
p_0 = 0.5){
for(j in seq.int(nmax, 0)){
if(pbeta(q = p_0, shape1 = alpha + j, shape2 = beta + nmax - j, lower.tail = FALSE) < thr){
targetatnmax <- j + 1
} else {
print(
break
}
}
result <- list(Success = Success, targeratnmax = targetatnmax)
return(result)
}
res = binomial(nmax,thr,alpha,beta,p_true,p_0)
res
Run Code Online (Sandbox Code Playgroud)
在我的计划中,我试图找到超过0.95 thr所需的成功次数.我试图使用if循环使用if else语句,但是当我运行它时,我没有得到我需要的值.我知道我的价值应该是59,但我似乎无法得到这个.我知道这些代码看起来非常混乱,但这只是因为我已经玩了好几个小时了.请任何帮助
这是清理后的代码:
binomial <- function(nmax = 100,
thr = 0.95,
alpha = 1,
beta = 1,
p_true = 0.5,
p_0 = 0.5){
targetatnmax <- 0
for(j in seq.int(0,nmax)){
if(pbeta(q = p_0, shape1 = alpha + j, shape2 = beta + nmax - j, lower.tail = FALSE) < thr){
targetatnmax <- j + 1
} else {
break
}
}
result <- list(targeratnmax = targetatnmax)
return(result)
}
res = binomial()
res
#$targeratnmax
#[1] 59
Run Code Online (Sandbox Code Playgroud)
主要问题(语法错误和不存在的对象除外)是你的循环从 nmax0运行而不是arround的另一种方式.
可能有优化的潜力,但我对统计数据的理解还不足以真正解决这个问题.