Skip to content

Commit

Permalink
Added chkDots()
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Jul 7, 2024
1 parent f9f9340 commit 647779f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sps
Title: Sequential Poisson Sampling
Version: 0.5.4
Version: 0.5.4.9001
Authors@R: c(
person("Steve", "Martin",
role = c("aut", "cre", "cph"),
Expand Down
14 changes: 7 additions & 7 deletions R/inclusion_prob.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ as_stratum <- function(strata) {
#' Calculate unconstrained inclusion probabilities
#' @noRd
unbounded_pi <- function(x, n) {
# n == 0 should be a strong zero
# n == 0 should be a strong zero.
if (n == 0L) {
rep.int(0, length(x))
} else {
Expand All @@ -25,21 +25,21 @@ unbounded_pi <- function(x, n) {
#' Find the units that belong in a TA stratum
#' @noRd
ta_units <- function(x, n, alpha) {
# partial sorting is not stable, so if x[n] == x[n + 1] after sorting then
# Partial sorting is not stable, so if x[n] == x[n + 1] after sorting then
# it is possible for the result to not resolve ties according to x
# (as documented) when alpha is large enough to make at least one unit with
# x[n] TA
# x[n] TA.
ord <- order(x, decreasing = TRUE)
s <- seq_len(n)
possible_ta <- rev(ord[s])
x_ta <- x[possible_ta] # ties are in reverse
definite_ts <- ord[seq.int(n + 1, length.out = length(x) - n)]

p <- x_ta * s / (sum(x[definite_ts]) + cumsum(x_ta))
# the sequence given by p has the following properties
# 1. if p[k] < 1, then p[k + 1] >= p[k]
# 2. if p[k] >= 1, then p[k + 1] >= 1
# consequently, if p[k] >= 1 - alpha, then p[k + m] >= 1 - alpha
# The sequence given by p has the following properties
# 1. if p[k] < 1, then p[k + 1] >= p[k],
# 2. if p[k] >= 1, then p[k + 1] >= 1,
# consequently, if p[k] >= 1 - alpha, then p[k + m] >= 1 - alpha.
possible_ta[p >= 1 - alpha]
}

Expand Down
4 changes: 2 additions & 2 deletions R/prop_allocation.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ highest_averages <- function(p, n, initial, available, ties, dist) {
p <- p[ord]
res <- res[ord]
available <- available[ord]
# the while condition could be n > sum(res), but the loop below always
# terminates after at most n steps, even if i is integer(0)
# The while condition could be n > sum(res), but the loop below always
# terminates after at most n steps, even if i is integer(0).
while (n > 0L) {
i <- which.max(p / dist(res) * (res < available))
res[i] <- res[i] + 1L
Expand Down
2 changes: 1 addition & 1 deletion R/sps.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ stratify <- function(f) {
p <- inclusion_prob_(x, n, strata, alpha, cutoff)
samp <- Map(f, p, n, split(prn, strata))
pos <- split(seq_along(prn), strata)
# strata must have at least one level, so unlist won't return NULL
# Strata must have at least one level, so unlist won't return NULL.
res <- unlist(Map(`[`, pos, samp), use.names = FALSE)
weights <- 1 / unlist(Map(`[`, p, samp), use.names = FALSE)

Expand Down
3 changes: 1 addition & 2 deletions R/sps_repweights.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ sps_repweights <- function(w, replicates = 1000L, tau = 1, dist = NULL) {

p <- 1 / w
n <- length(w) * replicates
a <- if (is.null(dist)) {
# pseudo-population method
a <- if (is.null(dist)) { # pseudo-population method
wf <- floor(w)
wr <- wf + (stats::runif(n) < w - wf)
stats::rbinom(n, wr, p) - p * wr
Expand Down
5 changes: 4 additions & 1 deletion R/sps_sample-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ levels.sps_sample <- function(x) {
#' @export
#' @importFrom stats weights
weights.sps_sample <- function(object, ...) {
chkDots(...)
attr(object, "weights")
}

Expand All @@ -40,15 +41,17 @@ print.sps_sample <- function(x, ...) {

#' @export
summary.sps_sample <- function(object, ...) {
chkDots(...)
n <- length(object)
ts <- sum(weights(object) > 1)
structure(list(n = n, ts = ts, ta = n - ts), class = "sps_sample_summary")
}

#' @export
print.sps_sample_summary <- function(x, ...) {
chkDots(...)
cat(
"Sample for", x$n, "units with", x$ta,
"Sample of", x$n, "units with", x$ta,
"take-all unit and", x$ts, "take-some units"
)
invisible(x)
Expand Down

0 comments on commit 647779f

Please sign in to comment.