R, use binomial distribution with more than two possibilities

不打扰是莪最后的温柔 提交于 2019-12-22 17:49:04

问题


I know this is probably elementary, but I seem to have a mental block. Let's say you want to calculate the probability of tossing a 4, 5, or 6 on a roll of one die. In R, it's easy enough:

sum(1/6, 1/6, 1/6)

This gives 1/2 which is the correct answer. However, I have in the back of my mind (where it possibly should remain) that I should be able to use the binomial distribution for this. I've tried various combinations of arguments for pbinom and dbinom, but I can't get the right answer.

With coin tosses, it works fine. Is it entirely inappropriate for situations where there are more than two possible outcomes? (I'm a programmer, not a statistician, so I'm expecting to get killed by the stat guys here.)

Question: How can I use pbinom() or dbinom() to calculate the probability of throwing a 4, 5, or 6 with one roll of a die? I'm familiar with the prob and dice packages, but I really want to use one of the built-in distributions.

Thanks.


回答1:


As @Alex mentioned above, dice-throwing can be represented in terms of multinomial probabilities. The probability of rolling a 4, for example, is

dmultinom(c(0, 0, 0, 1, 0, 0), size = 1, prob = rep(1/6, 6)) 
# [1] 0.1666667

and the probability of rolling a 4, 5, or 6 is

X <- cbind(matrix(rep(0, 9), nc = 3), diag(1, 3))
X
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    0    0    0    1    0    0
# [2,]    0    0    0    0    1    0
# [3,]    0    0    0    0    0    1
sum(apply(X, MAR = 1, dmultinom, size = 1, prob = rep(1/6, 6)))
# [1] 0.5



回答2:


Though it's not quite obvious, this can be done with pmultinom, implemented either in my pmultinom package on CRAN or this other pmultinom package on Github.

You conceptualize it as the event that it is not a 1, 2 or 3. Then, you write this probability as

P(X_1 ≤ 0, X_2 ≤ 0, X_3 ≤ 0, X_4 ≤ ∞, X_5 ≤ ∞, X_6 ≤ ∞)

where X_i is the number of occurrences of side i. All the X's together have a multinomial distribution, with a size parameter of 1, and all probabilities equal to 1/6. This probability above can be calculated (using my package) as

pmultinom(upper=c(0, 0, 0, Inf, Inf, Inf), size=1,
          probs=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6), method="exact")
# [1] 0.5

Though it's a bit of an awkward reformulation, I like it because I prefer to use a "p" function rather than take a sum of "d" functions.



来源:https://stackoverflow.com/questions/39402579/r-use-binomial-distribution-with-more-than-two-possibilities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!