Strange precision issues in R when computing cumulative binomial probability

前端 未结 2 704
离开以前
离开以前 2021-01-20 19:37

I\'ve been running into some weird problems when using this code:

positions<-c(58256)
occurrencies<-c(30)
frequency<-c(11/5531777)
length<-c(4)

         


        
2条回答
  •  别那么骄傲
    2021-01-20 20:10

    You can avoid your for loop by doing

    prob<-0
    i    <- 0:(occurrencies-1)
    pow  <- frequency^i
    pow1 <- (1-frequency)^(positions-i)
    bin  <- choose(positions, i)
    prob <- cumsum(prob+(bin*pow*pow1))
    [1] 0.8906152 0.9937867 0.9997624 0.9999932 0.9999998 1.0000000 1.0000000 1.0000000 1.0000000
    [10] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
    [19] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
    [28] 1.0000000 1.0000000 1.0000000
    

    I don't know if this is your desired result, but surely you can avoid the for loop going this fashion.

    See @Ben Bolker's comment and take a look at pbinom function.

提交回复
热议问题