在R中,在应用sample()时,如果replace为true,当大于时使用Walker的别名方法。 250 合理可能的值。有没有办法让 sample() 始终使用别名方法?谢谢!
最佳答案
一种选择是复制您的 x
和 prob
向量次数足够多,结果向量长于 250 个元素。这是一个黑客,当然,但一个有趣的!
sampleWalker <- function(x, size, prob) {
nx <- length(x)
nrep <- 251 %/% nx + 1
sample(x = rep(x, nrep), size = size, replace = TRUE, prob = rep(prob, nrep))
}
sampleWalker(1:3, 10, prob = 1:3)
# [1] 3 1 2 3 3 2 2 1 2 3
# Warning message:
# In sample.int(length(x), size, replace, prob) :
# Walker's alias method used: results are different from R < 2.2.0
关于r - sample() 中的别名方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13868679/