【发布时间】:2016-02-12 22:58:31
【问题描述】:
如果以前有人问过这个问题,请原谅我(我觉得它必须有,但找不到我正在寻找的确切内容)。
我是否可以使用指定元素不同机会的概率函数来绘制整数向量(从 1 到例如 10)中的一个元素。如果我想要相等的概率,我使用runif() 来获得 1 到 10 之间的数字:
ceiling(runif(1,1,10))
我如何类似地从例如得到 1 到 10 之间的数字的指数分布(使得 1 比 10 更可能),或逻辑概率函数(如果我想要一个从 1 到 10 的 sigmoid 增加概率)。
我能想到的唯一“解决方案”是首先从 say sigmoid 分布中提取 e6 数字,然后将 min 和 max 缩放为 1 和 10 - 但这看起来很笨拙。
更新:
这个尴尬的解决方案(我觉得它不是很“正确”)会这样
#Draw enough from a distribution, here exponential
x <- rexp(1e3)
#Scale probs to e.g. 1-10
scaler <- function(vector, min, max){
(((vector - min(vector)) * (max - min))/(max(vector) - min(vector))) + min
}
x_scale <- scaler(x,1,10)
#And sample once (and round it)
round(sample(x_scale,1))
周围没有更好的解决方案吗?
【问题讨论】:
-
我觉得你需要看看
sample()。你的代码相当于sample(1:10, 1),可以用prob=参数指定概率 -
是的,但是你怎么能在那里指定逻辑概率呢?
标签: r distribution sample