r - 从 R 中正态分布的特定部分采样

标签 r random sampling resampling

我尝试首先提取所有值 <= -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?

enter image description 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/

相关文章:

c# - 如何从函数返回值以在每一行用新结果顺序调用它

java - 随机 "shaking"一个数组来分配新的随机点

arrays - MATLAB:比较两个不同长度的数组

c - 音频中的采样率是什么意思

r - ggplot 在只需要一个图例时生成两个图例

algorithm - 在 R 中创建长度为 40 的 bool 值向量的排列

r - 在R中按组添加观察数

Rmarkdown : spacing between parragraph and image

java - 打乱数组时出现问题 - 返回 null 而不是随机整数

hadoop - 通过抛出错误对组内的记录进行抽样