rankSample: Generates a weighted sample (with replacement) of ranks

View source: R/nimbleFunction_Rexecution.R

rankSampleR Documentation

Generates a weighted sample (with replacement) of ranks


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.


rankSample(weights, size, output, silent = FALSE)



A vector of numeric weights. Does not need to sum to 1, but must be non-negative


Size of sample


An R object into which the values will be placed. See example below for proper use


Logical indicating whether to suppress logging information


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


sampInts = NA	#sampled integers will be placed in sampInts
rankSample(weights = c(1, 1, 2), size = 10, sampInts)
# [1] 1 1 2 2 2 2 2 3 3 3
rankSample(weights = c(1, 1, 2), size = 10000, sampInts)
#   1    2    3 
#2434 2492 5074 

#Used in a nimbleFunction
sampGen <- nimbleFunction(setup = function(){
	x = 1:2
run = function(weights = double(1), k = integer() ){
	rankSample(weights, k, x)
rSamp <- sampGen()
rSamp$run(1:4, 5)
#[1] 3 3 4 4 4

nimble documentation built on March 18, 2022, 8:03 p.m.