Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

summation of choose function in R

I am trying to create a function, that is identical to the pbinom in R, i.e. the density for a binomial distribution.

My code is the following:

MyPBin <- function(X,n,p) {
  choose(n,X)*p^X*(1-p)^(n-X)
}

which should match the function used in pbinom in R: p(x) = choose(n, x) p^x (1-p)^(n-x)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am, however, not getting the correct result. I think i have located the reason — if i take the sum of all the individual choose functions (n=1, n=2, n=3 etc.) i get the same result as pbinom.

So my question is: how can i sum all of the individual choose functions that the function uses, when i run it?

>Solution :

Use sum. These all give the same result. (Note that MyPBin gives the same result as dbinom.)

sum(MyPBin(0:3, 10, .3))
## [1] 0.6496107

sum(dbinom(0:3, 10, .3))
## [1] 0.6496107

pbinom(3, 10, .3)
## [1] 0.6496107
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading