Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sps
Title: Sequential Poisson Sampling
Version: 0.6.3
Version: 0.6.3.9001
Authors@R: c(
person("Steve", "Martin",
role = c("aut", "cre", "cph"),
Expand All @@ -23,12 +23,11 @@ Suggests:
kit (>= 0.0.10),
knitr,
quarto,
testthat (>= 3.0.0)
Config/testthat/edition: 3
tinytest
License: MIT + file LICENSE
Encoding: UTF-8
URL: https://marberts.github.io/sps/, https://github.com/marberts/sps
BugReports: https://github.com/marberts/sps/issues
VignetteBuilder: quarto
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3
Config/roxygen2/version: 8.0.0
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## sps (development version)

- Small improvements to the documentation.

- Now using `{tinytest}`.

## sps 0.6.3

- Extra arguments to `sps_iterator()` now work correctly (#9).
Expand Down
3 changes: 1 addition & 2 deletions R/expected_coverage.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' enterprises covered within a stratum when sampling business establishments.
#'
#' @inheritParams sps
#' @param n A positive integer giving the sample size.
#' @param n `[integer(1) >= 0]` A positive integer giving the sample size.
#'
#' @returns
#' The expected number of strata covered by the sample design.
Expand All @@ -27,7 +27,6 @@
#'
#' # Should get about 7 to 8 strata in a sample on average
#' expected_coverage(x, 15, s)
#'
#' @export
expected_coverage <- function(x, n, strata, alpha = 1e-3, cutoff = Inf) {
x <- as.numeric(x)
Expand Down
13 changes: 6 additions & 7 deletions R/inclusion_prob.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#' # Use the inclusion probabilities to calculate the variance of the
#' # sample size for Poisson sampling
#' sum(pi * (1 - pi))
#'
#' @export
inclusion_prob <- function(x, n, strata = NULL, alpha = 1e-3, cutoff = Inf) {
x <- as.numeric(x)
Expand All @@ -78,12 +77,12 @@ becomes_ta <- function(x, alpha = 1e-3, cutoff = Inf) {

alpha <- as.numeric(alpha)
if (alpha < 0 || alpha > 1) {
stop("'alpha' must be between 0 and 1")
stop("`alpha` must be between 0 and 1")
}

cutoff <- as.numeric(cutoff)
if (cutoff <= 0) {
stop("'cutoff' must be greater than 0")
stop("`cutoff` must be greater than 0")
}

alpha <- alpha + .Machine$double.eps^0.5
Expand Down Expand Up @@ -158,10 +157,10 @@ pi <- function(x, n, alpha, cutoff) {
)
}
if (alpha < 0 || alpha > 1) {
stop("'alpha' must be between 0 and 1")
stop("`alpha` must be between 0 and 1")
}
if (cutoff <= 0) {
stop("'cutoff' must be greater than 0")
stop("`cutoff` must be greater than 0")
}

# Add some fuzz to avoid floating-point rounding stopping some units from
Expand Down Expand Up @@ -192,10 +191,10 @@ stratified_pi <- function(x, n, strata, alpha, cutoff) {
stop("there must be a single sample size for each stratum")
}
if (length(alpha) != 1L && length(alpha) != nlevels(strata)) {
stop("'alpha' must be a single value or have a value for each stratum")
stop("`alpha` must be a single value or have a value for each stratum")
}
if (length(cutoff) != 1L && length(cutoff) != nlevels(strata)) {
stop("'cutoff' must be a single value or have a value for each stratum")
stop("`cutoff` must be a single value or have a value for each stratum")
}
Map(pi, split(x, strata), n, alpha, cutoff)
}
Expand Down
19 changes: 10 additions & 9 deletions R/prop_allocation.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@
#' ensures that the initial allocation is feasible.
#'
#' @inheritParams sps
#' @param n A positive integer giving the sample size.
#' @param initial A positive integer vector giving the initial (or minimal)
#' @param n `[integer(1) >= 0]` A positive integer giving the sample size.
#' @param initial `[integer >= 0]` A positive integer vector giving the initial
#' (or minimal)
#' allocation for each stratum, ordered according to the levels of
#' `strata`. A single integer is recycled for each stratum using a special
#' algorithm to ensure a feasible allocation; see details. Non-integers are
#' truncated towards 0. The default allows for no units to be allocated to a
#' stratum.
#' @param divisor A function for the divisor (highest-averages)
#' @param divisor `[function]` A function for the divisor (highest-averages)
#' apportionment method. The default uses the Jefferson/D'Hondt method. See
#' details for other possible functions.
#' @param ties Either 'largest' to break ties in favor of the stratum with the
#' @param ties `[character(1)]` Either 'largest' to break ties in favor of the
#' stratum with the
#' largest size (the default), or 'first' to break ties in favor of the
#' ordering of `strata`.
#' @param name Name of the divisor function. See details.
#' @param name `[character(1)]` Name of the divisor function. See details.
#'
#' @returns
#' `prop_allocation()` returns a named integer vector of sample sizes for each
Expand Down Expand Up @@ -83,7 +85,6 @@
#'
#' # Generate an allocation
#' prop_allocation(x, 15, s, initial = 1)
#'
#' @export
prop_allocation <- function(
x,
Expand Down Expand Up @@ -148,20 +149,20 @@ divisor_method <- function(
"Imperiali" = \(a) a + 2,
"Huntington-Hill" = \(a) {
if (any(a < 1)) {
stop("'a' must be greater than or equal to 1")
stop("`a` must be greater than or equal to 1")
}
sqrt(a * (a + 1))
},
"Danish" = \(a) a + 1 / 3,
"Adams" = \(a) {
if (any(a < 1)) {
stop("'a' must be greater than or equal to 1")
stop("`a` must be greater than or equal to 1")
}
a
},
"Dean" = \(a) {
if (any(a < 1)) {
stop("'a' must be greater than or equal to 1")
stop("`a` must be greater than or equal to 1")
}
a * (a + 1) / (a + 0.5)
}
Expand Down
24 changes: 14 additions & 10 deletions R/sps.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,34 @@ stratify <- function(f) {
#' \item{Pareto sampling}{`\(x) x / (1 - x)`}
#' }
#'
#' @param x A positive and finite numeric vector of sizes for units in the
#' population (e.g., revenue for drawing a sample of businesses).
#' @param n A positive integer vector giving the sample size for each stratum,
#' @param x `[numeric >= 0]` A positive and finite numeric vector of sizes for
#' units in the population (e.g., revenue for drawing a sample of businesses).
#' @param n `[integer >= 0]` A positive integer vector giving the sample size
#' for each stratum,
#' ordered according to the levels of `strata`. A single value is recycled
#' for all strata. Non-integers are truncated towards 0.
#' @param strata A factor, or something that can be coerced into one, giving
#' @param strata `[factor]` A factor, or something that can be coerced into one,
#' giving
#' the strata associated with units in the population. The default is to place
#' all units into a single stratum.
#' @param prn A numeric vector of permanent random numbers for units in the
#' @param prn `[0 < numeric < 1]` A numeric vector of permanent random numbers
#' for units in the
#' population, distributed uniform between 0 and 1. The default does not use
#' permanent random numbers, instead generating a random vector when the
#' function is called.
#' @param alpha A numeric vector with values between 0 and 1 for each stratum,
#' @param alpha `[0 <= numeric < 1]` A numeric vector with values between 0 and
#' 1 for each stratum,
#' ordered according to the levels of `strata`. Units with inclusion
#' probabilities greater than or equal to 1 - `alpha` are set to 1 for
#' each stratum. A single value is recycled for all strata. The default is
#' slightly larger than 0.
#' @param cutoff A positive numeric vector of cutoffs for each stratum, ordered
#' @param cutoff `[numeric >= 0]` A positive numeric vector of cutoffs for each
#' stratum, ordered
#' according to the levels of `strata`. Units with `x >= cutoff` get
#' an inclusion probability of 1 for each stratum. A single value is recycled
#' for all strata. The default does not apply a cutoff.
#' @param dist A function giving the fixed order distribution shape for an order
#' sampling scheme. See details.
#' @param dist `[function]` A function giving the fixed order distribution shape
#' for an order sampling scheme. See details.
#'
#' @returns
#' `sps()` and `ps()` return an object of class `sps_sample`.
Expand Down Expand Up @@ -273,7 +278,6 @@ stratify <- function(f) {
#' order_sampling2(1)(x, 6, prn = u) # sequential Poisson
#' order_sampling2(0)(x, 6, prn = u) # successive
#' order_sampling2(-1)(x, 6, prn = u) # Pareto
#'
#' @export
sps <- stratify(order_sampling_(identity))

Expand Down
11 changes: 6 additions & 5 deletions R/sps_iterator.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#' sequential Poisson method without replacing previously sampled units.
#'
#' @inheritParams sps
#' @param n A positive integer giving the initial sample size for the iterator.
#' @param alpha A number between 0 and 1. Units with
#' @param n `[integer(1) >= 0]` A positive integer giving the initial sample
#' size for the iterator. The default is 0.
#' @param alpha `[0 <= numeric(1) < 1]` A number between 0 and 1. Units with
#' inclusion probabilities greater than or equal to 1 - `alpha` are set to 1.
#' The default is slightly larger than 0.
#' @param cutoff A numeric cutoff. Units with `x >= cutoff` get
#' an inclusion probability of 1. The default does not apply a cutoff.
#' @param cutoff `[numeric(1) >= 0]` A numeric cutoff. Units with `x >= cutoff`
#' get an inclusion probability of 1. The default does not apply a cutoff.
#'
#' @returns
#' A function that returns the next unit in the sample. It take a single
Expand Down Expand Up @@ -37,7 +38,7 @@ sps_iterator <- function(x, n = 0L, prn = NULL, alpha = 0.001, cutoff = Inf) {
prn <- as.numeric(prn)
}
if (length(x) != length(prn)) {
stop("'x' and 'prn' must be the same length")
stop("`x` and `prn` must be the same length")
}

s <- order(prn / x)
Expand Down
28 changes: 15 additions & 13 deletions R/sps_repweights.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
#' becomes \eqn{(a + \tau - 1) / \tau}. If \eqn{\tau > 1} then the resulting
#' bootstrap variance estimator should be multiplied by \eqn{\tau^2}.
#'
#' @param w A numeric vector of design (inverse probability) weights for a
#' (sequential) Poisson sample.
#' @param replicates A positive integer that gives the number of bootstrap
#' replicates (1,000 by default). Non-integers are truncated towards 0.
#' @param tau A number greater than or equal to 1 that gives the rescale factor
#' @param w `[numeric >= 1]` A numeric vector of design (inverse probability)
#' weights for a (sequential) Poisson sample.
#' @param replicates `[integer(1) >= 0]` A positive integer that gives the
#' number of bootstrap replicates (1,000 by default). Non-integers are
#' truncated towards 0.
#' @param tau `[numeric(1) >= 1 | function]` A number greater than or equal to 1
#' that gives the rescale factor
#' for the bootstrap weights. Setting to 1 does not rescale the
#' weights. This can also be a function that takes a vector of bootstrap
#' adjustments and returns a number larger than 1. The default automatically
#' picks the smallest feasible rescale factor (up to a small tolerance).
#' @param dist A function that produces random deviates with mean 0 and
#' standard deviation 1, such as [rnorm()]. The default uses the
#' @param dist `[function]` A function that produces random deviates with mean 0
#' and standard deviation 1, such as [rnorm()]. The default uses the
#' pseudo-population method from section 4.1 of Beaumont and Patak (2012); see
#' details.
#'
Expand Down Expand Up @@ -94,7 +96,6 @@
#' )
#'
#' lapply(dist, sps_repweights, w = weights(samp), replicates = 5, tau = 2)
#'
#' @export
sps_repweights <- function(
w,
Expand Down Expand Up @@ -124,26 +125,27 @@ sps_repweights <- function(
} else {
tau <- as.numeric(tau)
if (tau < 1) {
stop("'tau' must be greater than or equal to 1")
stop("`tau` must be greater than or equal to 1")
}
}
res <- w * (a + tau) / tau
if (any(res < 0)) {
warning("some replicate weights are negative; try increasing 'tau'")
warning("some replicate weights are negative; try increasing `tau`")
}
dim(res) <- c(length(w), replicates)
structure(res, tau = tau)
}

#' Automatically scale tau
#' @rdname sps_repweights
#' @param tol A non-negative number, strictly less than 1, that gives the
#' tolerance for determining the minimum feasible value of `tau`.
#' @param tol `[0 <= numeric(1) < 1]` A non-negative number, strictly less than
#' 1, that gives the tolerance for determining the minimum feasible value
#' of `tau`.
#' @export
min_tau <- function(tol) {
tol <- as.numeric(tol)
if (tol < 0 || tol >= 1) {
stop("'tol' must be in [0, 1)")
stop("`tol` must be in [0, 1)")
}
function(a) {
max(abs(a[a < 0]) / (1 - tol), 1)
Expand Down
Loading
Loading