在R中,当应用sample()时,如果替换为true,则在有超过250的合理可能值时使用Walker的别名方法。是否有办法使sample()始终使用别名方法?谢谢!
发布于 2012-12-14 03:34:31
一种选择是复制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.0https://stackoverflow.com/questions/13868679
复制相似问题