Description Usage Arguments Details Author(s) Examples
Takes a set of non-negative weights
(do not need to
sum to 1) and returns a sample with size
elements of
the integers 1:length(weights)
, where the
probability of being sampled is proportional to the value
of weights
. An important note is that the output
vector will be sorted in ascending order. Also, right now
it works slightly odd syntax (see example below). Later
releases of NIMBLE will contain more natural syntax.
1 | rankSample(weights, size, output)
|
weights |
A vector of numeric weights. Does not need to sum to 1, but must be non-negative |
size |
size of sample |
output |
an R object into which the values will be placed. See example below for proper use |
If invalid weights provided (i.e. negative weights or
weights sum to 1), sets output = rep(1, size) and prints
warning. rankSample
can be used inside nimble
functions.
rankSample
first samples from the joint distribution
size
uniform(0,1) distributions by conditionally
sampling from the rank statistics. This leads to a sorted
sample of uniform(0,1)'s. Then, a cdf vector is constructed
from weights. Because the sample of uniforms is sorted,
rankSample
walks down the cdf in linear time and
fills out the sample.
Clifford Anderson-Bergman
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | set.seed(1)
sampInts = NA #sampled integers will be placed in sampInts
rankSample(weights = c(1, 1, 2), size = 10, sampInts)
sampInts
# [1] 1 1 2 2 2 3 3 3 3 3
rankSample(weights = c(1, 1, 2), size = 10000, sampInts)
table(sampInts)
#sampInts
# 1 2 3
#2429 2498 5073
#Used in a nimbleFunction
sampGen <- nimbleFunction(setup = function(){
x = 1:2
},
run = function(weights = double(1), k = integer() ){
rankSample(weights, k, x)
returnType(integer(1))
return(x)
})
rSamp <- sampGen()
cSamp <- compileNimble(rSamp)
cSamp(1:4, 5)
#[1] 1 1 4 4 4
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.