Register a user-defined sampling method so it can be used through
unequal_prob_wor(), unequal_prob_wr(), or balanced_wor() and
their associated generics.
Arguments
- name
A unique method name (character string). Must not collide with a built-in method name.
- type
"wor"(without replacement),"wr"(with replacement), or"balanced"(balanced without replacement, dispatched throughbalanced_wor()).- sample_fn
A function that draws a sample. See Contracts below.
- joint_fn
An optional function that computes joint inclusion probabilities (WOR) or joint expected hits (WR). If
NULL,joint_inclusion_prob()/joint_expected_hits()will error for this method.- fixed_size
Does this method always produce exactly
nunits? Must beTRUEfortype = "wr".- variance_family
Optional declaration of how design-based variance should be estimated for this method, for downstream packages that export designs for variance estimation. One of
"srs","pps_brewer","poisson","wr", or"unsupported"; see Variance families below.NULL(the default) means undeclared: consumers fall back on inferring a treatment fromtypeandfixed_size.- supports_prn
Does this method support permanent random numbers for sample coordination? Set to
TRUEorFALSEfor"wor"and"wr"methods.NULL(the default) resolves toFALSE. LeaveNULLfor"balanced"methods;balanced_wor()has noprnargument.- supports_aux
Does this method use auxiliary balancing variables? Set to
TRUEorFALSEfortype = "balanced".NULL(the default) resolves toTRUEfor balanced methods andFALSEotherwise. Set it toFALSEfor spread-only (spatially balanced) methods such as the local pivotal method, so that passingauxtobalanced_wor()is an error instead of being silently ignored. LeaveNULLfor"wor"and"wr"methods.- supports_strata
Does this method support stratified balanced sampling? Set to
TRUEorFALSEfortype = "balanced".NULL(the default) resolves toFALSE. WhenFALSE, passingstratatobalanced_wor()with this method is an error, andsample_fndoes not need astrataargument. LeaveNULLfor"wor"and"wr"methods.- supports_spread
Does this method support spatial spreading (well-spread / spatially balanced sampling)? Set to
TRUEorFALSEfortype = "balanced".NULL(the default) resolves toFALSE. WhenFALSE, passingspreadtobalanced_wor()with this method is an error, andsample_fndoes not need aspreadargument. LeaveNULLfor"wor"and"wr"methods.- probabilities
Where the method sits in the first-order probability taxonomy, for downstream packages that weight or record per-unit selection probabilities:
"exact": the true first-order inclusion probabilities (types"wor"and"balanced") or expected hits (type"wr") equal thepikorhitsvector passed tosample_fn, as for Sampford or the cube method."approximate":pikorhitsis the method's first-order target, achieved up to a documented approximation, as for Pareto or sequential Poisson order sampling. Design weights1/pikremain standard practice."unknown"(the default):pikorhitsis an input weight or preference only, so the corresponding first-order quantities are not known. The toy WOR sampler in the examples is such a method: successive sampling withprob = pikdoes not yield inclusion probabilities equal topik, so its design weights1/pikwould be systematically biased, not merely noisy. (The same draw with replacement does honor expected hits, so a multinomial-styletype = "wr"method declares"exact".)
The default is deliberately strict: if you have not established which tier your method is in, its selection probabilities are unknown, and downstream packages that weight estimation by
1/pikmay refuse to draw with it rather than produce biased weights. Sampling through sondage itself is unaffected: the declaration describes the method, it never disables it. Likevariance_family, the declaration is an assertion by the method author that sondage cannot verify; the package vignette shows how to check a first-order contract by simulation.
Details
Registrations last for the current R session. Registering an existing
custom method name is an error; call unregister_method() first when
deliberately replacing a method.
Contracts
sample_fn(pik, n = NULL, prn = NULL, ...) (type "wor")
pikInclusion probabilities, numeric vector of length N.
nTarget sample size, equal to
round(sum(pik)).prnPermanent random numbers (numeric vector length N, values in (0,1)), or
NULL. Supplyingprnis an error when the method is registered withsupports_prn = FALSE, andsample_fnis not called.- Returns
Integer vector of selected unit indices (1-based). Indices are distinct and have length
nfor fixed-size methods, or varying length for random-size methods. The dispatcher validates the type, range, size, and replacement rules before constructing the sample object.
sample_fn(hits, n = NULL, prn = NULL, ...) (type "wr")
hitsExpected hits, numeric vector of length N.
nTarget sample size, equal to
round(sum(hits)).prnPermanent random numbers (numeric vector length N, values in (0,1)), or
NULL. Supplyingprnis an error when the method is registered withsupports_prn = FALSE, andsample_fnis not called.- Returns
Integer vector of
nselected unit indices (1-based), with possible repeats. The dispatcher validates the type, range, size, and replacement rules before constructing the sample object.
sample_fn(pik, n = NULL, aux = NULL, ...) (type "balanced")
pikInclusion probabilities, numeric vector of length N.
nTarget sample size (integer when
fixed_size, otherwisesum(pik)).auxAuxiliary balancing matrix (N x p, double), or
NULL. Passed through as supplied tobalanced_wor()after validation; the sample-size constraint is not prepended, so add it yourself if your algorithm needs it (e.g.cbind(pik, aux)). Methods registered withsupports_aux = FALSEalways receiveaux = NULL.strataOnly when registered with
supports_strata = TRUEand the caller suppliesstrata: an integer vector (length N) of dense stratum labels1:H. Declare it asstrata = NULLin your function signature.spreadOnly when registered with
supports_spread = TRUEand the caller suppliesspread: a numeric matrix (N x d, double) of spatial coordinates (or other spreading variables). Declare it asspread = NULLin your function signature.- Returns
Integer vector of distinct selected unit indices (1-based). The dispatcher validates the returned indices.
joint_fn(pik, sample_idx = NULL, ...) (optional; types "wor"
and "balanced")
pikInclusion probabilities, numeric vector of length N.
epsIf the function explicitly declares an
epsformal, it receives the value supplied tojoint_inclusion_prob().sample_idxWhen non-NULL, an integer vector of sampled unit indices. Return only the submatrix for these units.
- Returns
Symmetric matrix of joint inclusion probabilities (N x N when
sample_idxis NULL,length(sample_idx)xlength(sample_idx)otherwise). The dispatcher validates that the matrix has the required dimensions and contains finite, symmetric numeric values.
joint_fn(hits, sample_idx = NULL, ...) (optional; type "wr")
hitsExpected hits, numeric vector of length N, renormalized to sum exactly to the integer sample size
n.nsimIf the function explicitly declares an
nsimformal, it receives the value supplied tojoint_expected_hits().sample_idxWhen non-NULL, an integer vector of distinct sampled unit indices. Return only the submatrix for these units.
- Returns
Symmetric matrix of joint expected hits (N x N when
sample_idxis NULL,length(sample_idx)xlength(sample_idx)otherwise). The dispatcher validates that the matrix has the required dimensions and contains finite, symmetric numeric values.
Variance families
variance_family names the estimator treatment a variance consumer
(such as a survey-export package) should apply to samples drawn
with this method. Selection metadata alone cannot determine it: a
fixed-size WOR method may need Brewer's unequal-probability
approximation or an SRS-style variance, and for a random-size WOR
method no safe inference exists at all, because a Poisson-type
method (independent selections) and a correlated random-size
scheme need different estimators.
"srs"Equal-probability fixed-size WOR. SRS-style variance with a finite population correction. Requires
fixed_size = TRUE."pps_brewer"Fixed-size unequal-probability WOR. Brewer's approximation from the marginal inclusion probabilities. Requires
fixed_size = TRUE."poisson"Random-size WOR with independent selections (Poisson-type). Exact Poisson linearization. Requires
type = "wor"andfixed_size = FALSE."wr"With-replacement (or minimum-replacement) selection. Hansen-Hurwitz variance, no finite population correction. Requires
type = "wr"."unsupported"No linearization treatment is valid; consumers should refuse to linearize and point to replicate methods instead. The correct declaration for correlated random-size schemes. Always allowed.
Balanced methods allow only "pps_brewer" or "unsupported":
balancing constraints couple selections across units, so
"poisson" can never hold for them.
The declaration is an assertion by the method author, not
something sondage can verify; "poisson" in particular asserts
that units are selected independently, and a wrong declaration
produces silently wrong variance estimates for every user of the
method. The package vignette
(vignette("custom-methods", package = "sondage")) shows how to
check a declared family by simulation.
Examples
# Register a toy random sampler. Successive sampling with
# prob = pik does not give first-order inclusion probabilities
# equal to pik, so its probabilities stay "unknown" (the default).
my_sampler <- function(pik, n = NULL, prn = NULL, ...) {
sample.int(length(pik), size = n, prob = pik)
}
register_method(
"toy",
type = "wor",
sample_fn = my_sampler
)
s <- unequal_prob_wor(c(0.3, 0.3, 0.4), method = "toy")
s$method
#> [1] "toy"
# Register a toy balanced sampler (ignores aux, keeps size fixed)
my_balanced <- function(pik, n = NULL, aux = NULL, ...) {
sample.int(length(pik), size = n, prob = pik)
}
register_method("toy_bal", type = "balanced", sample_fn = my_balanced)
s <- balanced_wor(c(0.3, 0.3, 0.4), method = "toy_bal")
s$method
#> [1] "toy_bal"
# Clean up
unregister_method("toy")
unregister_method("toy_bal")