简体   繁体   中英

Bayesian R Programming

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

In my program I am trying to find the number of successes needed to exceed 0.95 thr. I am trying to use a for loop with if else statements but when I run it I don't get the value I need. I know my value should be 59 but I cannot seem to get this. I know the code seems really messy but its only because I have been playing around with it for hours. PLEASE ANY HELP

Here is your code after clean-up:

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

The main problem (other than the syntax errors and not existent objects) was that your loop ran from nmax to 0 instead of the other way arround.

There is probably potential for optimization, but my understanding of the statistics is not good enough to really tackle that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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