在R中,当应用sample()时,如果replace为true,则当有超过 250 合理可能的值时,使用Walker的别名方法。有没有办法让sample()始终使用别名方法?谢谢!
答案 0 :(得分:1)
一种选择是将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