Learn R Programming

optimall

optimall offers a collection of functions that are designed to streamline the process of optimum sample allocation, specifically under an adaptive, multi-wave approach. Its main functions allow users to:

  • Define, split, and merge strata based on values or quantiles of other variables.

  • Calculate the optimum number of samples to allocate to each stratum in a given study in order to minimize the variance of an estimate of interest.

  • Select specific ids to sample based on a stratified sampling design.

  • Optimally allocate a fixed number of samples to an ancillary sampling wave based on results from a prior wave.

When used together, these functions can automate most of the sampling workflow.

Installation

You can install optimall from CRAN with:

# install.packages("optimall")

Or, you can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("yangjasp/optimall")

Example

Given a dataframe where each row represents one unit, optimall can define the stratum each unit belongs to:

library(optimall)
data <- split_strata(data = data, strata = "old_strata", 
                     split_var = "variable_to_split_on", 
                     type = "value", split_at = c(1,2))

If the strata or values to split at are not obvious, it may be useful to try a few different splits and observe the effects that each has on sample allocation. optimall makes this process quick and easy with a Shiny app that can be launched with optimall_shiny. This app allows users to adjust inputs to the split_strata function and view the results in real time. Once the parameters are satisfactory, the user can confirm the split and move on to further ones if desired. The app prints the code required to replicate the splits in optimall, so making the changes inside of R becomes as easy as a copy and paste!

Screenshot:

We can then use optimum_allocation to calculate the optimum allocation a fixed number of samples to our strata in order to minimize the variance of a variable of interest.

optimum_allocation(data = data, strata = "new_strata", 
                   y = "var_of_interest", nsample = 100)

optimall offers more functions that streamline adaptive, multi-wave sampling workflows. For a more detailed description, see package vignettes.

References

McIsaac MA, Cook RJ. Adaptive sampling in two‐phase designs: a biomarker study for progression in arthritis. Statistics in Medicine. 2015 Sep 20;34(21):2899-912.

Wright, T. (2014). A simple method of exact optimal sample allocation under stratification with any mixed constraint patterns. Statistics, 07.

Copy Link

Version

Install

install.packages('optimall')

Monthly Downloads

577

Version

1.2.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Jasper Yang

Last Published

July 17th, 2025

Functions in optimall (1.2.0)

get_mw

Access Slots of a Multiwave Object
merge_samples

Merge Sampled Data based on IDs
summary,Multiwave-method

Method for summary for class Multiwave
Phase-class

Phase Class for Multi-Wave Sampling Organization
split_strata

Split Strata
Wave-class

Wave Class for Multi-Wave Sampling Organization
shiny_ui

UI for Shiny App for Splitting Strata with Optimum Allocation
optimum_allocation

Optimum Allocation
multiwave_diagram

Print Summary Diagram of Multiwave Object
optimall_shiny

Run the shiny application
sample_strata

Select Sampling Units based on Stratified Random Sampling
merge_strata

Merge Strata
apply_multiwave

Apply a basic optimall function to a Multiwave Object
Multiwave-class

Multiwave Class for Multi-Wave Sampling Organization
multiwave

Initialize a Multiwave Object
MatWgt_Sim

Example Dataset: Maternal Weights
allocate_wave

Adaptive Multi-Wave Sampling
set_mw<-

Write Slots of a Multiwave Object
shiny_server

Server logic for Interactive Shiny for Optimall.