【发布时间】:2015-06-22 00:58:05
【问题描述】:
我猜之前有人问过,但是我对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 (?sample):
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.
问题是:为什么“概率”不需要总和为一?
任何答案将不胜感激:谢谢!
【问题讨论】:
-
为什么它们的总和必须为 1?
c(3,1)的权重与c(0.75,0.25)一样有效。而且我收集 R 只会根据rbinom(n = 5, size = 1, prob = c(1,0))将权重回收到n的长度,其中权重将为1,0,1,0,1 -
R 内部会在必要时回收并在使用前执行
prob=prob/sum(prob)