问题描述
我知道这可能是初级的,但我似乎有心理障碍.假设您要计算掷一个骰子时掷出 4、5 或 6 的概率.在 R 中,这很容易:
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)
这给出了正确答案的 1/2.但是,在我的脑海中(它可能应该保留的地方)我应该能够为此使用二项式分布.我已经尝试了 pbinom 和 dbinom 的各种参数组合,但我无法得到正确的答案.
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.)
问题:我如何使用 pbinom() 或 dbinom() 来计算一次掷骰子掷出 4、5 或 6 的概率?我熟悉 prob 和 dice 包,但我真的很想使用其中一种内置发行版.
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.
谢谢.
推荐答案
正如@Alex 上面提到的,掷骰子可以用多项式概率表示.例如,掷出 4 的概率是
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
掷出 4、5 或 6 的概率是
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
这篇关于R,使用具有两种以上可能性的二项式分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!