[英]Sampling from a Binomial(K, p) with unexpected result
In the help files for rbinom
, size argument is a number of trials (incl. a zero) but it doesn't say if this can also be a vector. 在
rbinom
的帮助文件中,size参数是一些试验(包括零),但它没有说明这是否也可以是一个向量。
The correct way of using this function is 使用此功能的正确方法是
table(rbinom(n = 1000, size = 1, prob = 0.2))
0 1
809 191
But what is happening here? 但是这里发生了什么?
table(rbinom(n = 1000, size = 0:1, prob = 0.2))
0 1
894 106
Argument recycling of the size argument is the prime cause. 参数回收大小参数是主要原因。
Because n
is 1000, 0:1
is recycled until you get 500 0
's and 500 1
's (alternating). 因为
n
是1000,所以回收0:1
直到你获得500 0
和500 1
(交替)。
All the 0-size ones give 0
: 所有0大小的给出
0
:
> rbinom(10,size=0,prob=0.2)
[1] 0 0 0 0 0 0 0 0 0 0
Resulting in 500 0
's + 500 Bernoulli trials with p=0.2, resulting in about 100 1
's out of 1000 elements. 导致500
0
'+ 500伯努利试验,p = 0.2,导致1000个元素中约100个1
。
[Your results didn't seem surprising to me, but argument recycling can bite if you're not looking for it, and - while there are reasons why the number of successes in 0 Bernoulli trials should be defined as 0 - it may not seem obvious at first either.] [你的结果对我来说似乎并不令人惊讶,但如果你不是在寻找它,那么论据回收就会受到影响,而且 - 虽然有理由认为0伯努利试验中的成功数量应该被定义为0 - 它可能看起来不像起初也很明显。]
Documentation bug: 文档错误:
If 'size' is not an integer, 'NaN' is returned.
如果“大小”不是一个整数,“男”返回。 [my emphasis]
[我的重点]
You are giving it more than one integer, so the documentation would imply that you would get NaN
. 你给它多个整数,所以文档意味着你会得到
NaN
。
Its confusing because it explicitly states where other arguments can be vectors but not size
. 它令人困惑,因为它明确说明了其他参数可以是向量而不是
size
。 I'd file a documentation bug with the maintainer, which in this case probably means the main R bug tracker. 我向维护者提交了一个文档错误,在这种情况下可能意味着主要的R bug跟踪器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.