Introduction to samplyr
What is samplyr?
samplyr provides a composable R grammar for specifying, executing, and documenting probability sampling designs. The package relies on sondage for the sampling algorithms in C, complements svyplan for sample size planning, and transfers the result to survey and srvyr.
Why a grammar of sampling?
In a field survey, the sampling design chains several decisions. One stratifies by domain, groups the units into clusters, draws the clusters with probability proportional to size (PPS), then selects the households at the second stage.
With samplyr, the code follows exactly this structure of the protocol, which eases the methodological review, the check of the frame, and the full reproducibility of the draw.
Installation
samplyr is hosted on GitLab, with a mirror on GitHub. The installation uses remotes.
# install.packages("remotes")
remotes::install_gitlab("dickoa/samplyr")A two-stage stratified design
The pipeline below specifies a two-stage design (Brewer PPS selection of enumeration areas within region and urban/rural strata, followed by systematic household selection) and executes stage 1 against an EA frame:
library(samplyr)
dhs_design <- sampling_design(title = "Two-stage household survey") |>
add_stage(label = "Enumeration areas") |>
stratify_by(region, urban) |>
cluster_by(ea_id) |>
draw(n = 20, method = "pps_brewer", mos = households) |>
add_stage(label = "Households") |>
draw(n = 25, method = "systematic")
ea_sample <- execute(dhs_design, ea_frame, stages = 1, seed = 2026)The resulting sample carries inclusion probabilities and design weights automatically, and converts straight into a survey design object via as_svydesign().
Going further
- Browse the samplyr reference documentation
- Work through the worked examples in Survey Sampling Design with R
- See the full package stack on the Tools page