Learn R Programming

nimble (version 1.2.1)

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

Description

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.

Usage

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

Arguments

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

silent

Logical indicating whether to suppress logging information

Author

Clifford Anderson-Bergman

Details

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.

Examples

Run this code
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 2 2 3 3 3
rankSample(weights = c(1, 1, 2), size = 10000, sampInts)
table(sampInts)
#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)
	returnType(integer(1))
	return(x)
})
rSamp <- sampGen()
rSamp$run(1:4, 5)
#[1] 3 3 4 4 4

Run the code above in your browser using DataLab