我尝试首先提取所有值 <= -4
(将这些称为 p1
)来自 mother
正态分布。然后,随机抽取 p1
中的 50 个样本根据它们在 mother
中被选择的概率进行替换(将这些 50 称为 p2
)。例如,-4
比 -6
更有可能被选择进一步进入尾部区域。
我想知道下面的 R 代码是否正确捕获了我上面描述的内容?
mother <- rnorm(1e6)
p1 <- mother[mother <= -4]
p2 <- sample(p1, 50, replace = T) # How can I define probability of being selected here?
最佳答案
您可以使用函数sample
参数prob
。引用自 help("sample")
:
prob a vector of probability weights for obtaining the elements of the vector being sampled.
在详细信息
部分:
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.
所以你必须小心,离平均值越远,概率越小,正态分布很快就会下降到较小的概率值。
set.seed(1315) # Make the results reproducible
mother <- rnorm(1e6)
p1 <- mother[mother <= -4]
p2 <- sample(p1, 50, replace = T, prob = pnorm(p1))
您可以看到它与直方图一起工作。
hist(p2)
关于r - 从 R 中正态分布的特定部分采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814959/