Skip to content

Qadra/Rsampl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rsampl

Build Status codecov

Implementation of Weighted Random Sampling algorithms for R.

The current implementation of Weighted Random Sampling in R is an algorithm, this repository currently contains two algorithms for weighted random sampling:

Binary solution

Uses a simple binary tree with

  • Sampling time: O(k*log(n))
  • Preprocessing time: O(n)

Can be accessed using method='binary'.

RSTree Solution

A more complicated tree with with the following

  • Sampling time: O(k * log(log(n))) expected
  • Preprocessing time: O(n)

Can be accessed using method='rstree'.

Installation

When the repository has been cloned you can use R CMD INSTALL . in the cloned directory to install the package in your user R installation.

Usage

To load the package execute library(Rsampl). Usage examples can be found using e.g. ?Rsample::wrs_sample. The following are some basic usage examples.

Using without preprocessing

Weighted Random Sampling without preprocessing:

library(Rsampl)

# The number of indices we sample from
n <- 100
# The number of indices we sample on each sample. Precondition: k <= n
k <- 10

# Indexes we sample from
I <- 1:n
# The weights on which elements are selected
W <- runif(n)
W <- W/sum(W) # Normalize input!

# Sampling using a binary tree
samples <- wrs(I, W, k)
# is equal to
samples <- wrs(I, W, k=k, method="binary")

# The same for the RSTree
samples <- wrs(I, W, k=k, method="rstree")

Usage with preprocessing

This library supports preparing the data structure as a separate step from the actual sampling. If many samples need to be drawn from the same data distribution, this leads to performance improvements.

library(Rsampl)

# The number of indices we sample from
n <- 100
# The number of indices we sample on each sample. Precondition: k <= n
k <- 10

# Indexes we sample from
I <- 1:n
# The weights on which elements are selected
W <- runif(n)
W <- W/sum(W) # Normalize input!

# Generate the data structure to sample from:
binary_structure <- wrs_preprocessing(I, W, k, method='binary')

# Draw samples using the structure
samples <- wrs(k=k, struct=binary_structure)

Development

The following packages are required: devtools, roxygen2.

Start R in the base of the package directory load devtools and run unit tests using test() (or devtools::test()).

After writing code you can refresh the package using R CMD INSTALL . followed by reload() from the devtools package.

Before committing packages be sure to run roxygenize() followed by check(). Warnings may be treated as build errors by Travis. The roxygenize() command creates manuals and the NAMESPACE file from comments in the included R code.

To view code coverage use the package covr and any of the following:

# For just percentages
package_coverage()

# For browsable webinterface with code coverage
report(package_coverage())

FAQ

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published