我想之前已经问过了,但我对R中的“sample”和“rbinom”函数仍然有点生疏,并想问下面两个简单的问题:
a)假设我们有:
rbinom(n = 5, size = 1, prob = c(0.9,0.2,0.3))
所以“n”= 5,但“prob”仅表示其中三个。 R为这两个n分配了什么值?
b)假设我们有:
sample(x = 1:3, size = 1, prob = c(.5,0.2,0.9))
根据R-help(?样本):
The optional prob argument can be used to give a vector of weights
for obtaining the elements of the vector being sampled.
They need not sum to one, but they should be non-negative and not all zero.
问题是:为什么“概率”不需要求和?
任何答案都将非常感谢:谢谢!
答案 0 :(得分:4)
除n之外的数字参数将被循环到结果的长度。
这意味着在您的示例中,您传入的prob
向量将被回收,直到达到所需的长度(大概为5)。所以将使用的向量是:
c(0.9, 0.2, 0.3, 0.9, 0.2)
至于sample
函数,因为@thelatemail指出概率不必总和为1.看起来prob
向量在内部被归一化为1。