Sequential Poisson sampling is a variation of Poisson sampling for drawing probability-proportional-to-size samples with a given number of units. It’s a fast, simple, and flexible method for sampling units proportional to their size, and is often used for drawing a sample of businesses. The purpose of this vignette is to give an example of how the functions in this package can be used to easily draw a sample using the sequential Poisson method. More details can be found on the help pages for the functions used in this vignette.

Consider the problem of drawing a sample of businesses in order to measure the value of sales for the current quarter. The frame is a business register that gives an enumeration of all businesses in operation, along with the revenue of each business from the previous year and the region in which they are headquartered.

```
library(sps)
set.seed(123654)
<- data.frame(
frame revenue = round(rlnorm(1e3) * 1000),
region = sample(1:3, 1e3, prob = c(0.2, 0.3, 0.5), replace = TRUE)
)
head(frame)
#> revenue region
#> 1 2676 1
#> 2 2158 1
#> 3 2046 3
#> 4 537 2
#> 5 266 3
#> 6 1991 1
```

Associated with each business is a value for their sales for the current quarter, although these values are not observable for all businesses. The purpose of drawing a sample is to observe sales for a subset of businesses, and extrapolate the value of sales from the sample of business to the entire population. Sales are positively correlated with last year’s revenue, and this is the basis for sampling businesses proportional to revenue.

`<- round(frame$revenue * runif(1e3, 0.5, 2)) sales `

Budget constraints mean that it’s feasible to draw a sample of 100 businesses. Businesses operate in different regions, and so the sample will be stratified by region. This requires determining how the total sample size of 100 is allocated across regions. A common approach is to do this allocation proportional to the total revenue in each region.

```
<- with(frame, prop_allocation(revenue, 100, region))
allocation
allocation#> 1 2 3
#> 19 32 49
```

With the sample size for each region in hand, it’s now time to draw a sample and observe the value of sales for these businesses. In practice this is usually the result of a survey that’s administered to the sampled units.

```
<- with(frame, sps(revenue, allocation, region))
sample
<- cbind(frame[sample, ], sales = sales[sample])
survey
head(survey)
#> revenue region sales
#> 8 1422 3 1571
#> 25 2741 2 1843
#> 31 580 1 897
#> 37 4508 2 8659
#> 38 1007 3 1804
#> 42 2380 3 4740
```

An important piece of information from the sampling process is the design weights, as these enable estimating the value of sales in the population with the usual Horvitz-Thompson estimator.

```
$weight <- weights(sample)
survey
head(survey)
#> revenue region sales weight
#> 8 1422 3 1571 11.254659
#> 25 2741 2 1843 6.070412
#> 31 580 1 897 27.752969
#> 37 4508 2 8659 3.690994
#> 38 1007 3 1804 15.892875
#> 42 2380 3 4740 6.724422
```

```
<- with(survey, sum(sales * weight))
ht
ht#> [1] 2039582
```

The Horvitz-Thompson estimator is (asymptotically) unbiased under sequential Poisson sampling, so it should be no surprise that the estimate is fairly close the true (but unknown) value of sales among all businesses.

```
/ sum(sales) - 1
ht #> [1] 0.01325931
```

But in practice it’s not possible to determine how far an estimate is from the true value in the population. Instead, a common measure of the quality of a estimate is the coefficient of variation, and this requires estimating the variance of the Horvitz-Thompson estimator.

A general approach for estimating the variance of the Horvitz-Thompson estimator is to construct bootstrap replicate weights from the design weights for the sample, compute a collection of estimates for the total based on these replicate weights, and then compute the variance of this collection of estimates.

```
<- sps_repweights(weights(sample), tau = 2)
repweights
<- attr(repweights, "tau")^2 *
var mean((colSums(survey$sales * repweights) - ht)^2)
sqrt(var) / ht
#> [1] 0.09666341
```

There is also an analytic estimator for the variance of the Horvitz-Thompson estimator under sequential Poisson sampling. It’s less flexible than the bootstrap estimator, but is more precise.

```
<- function(y, w) {
sps_var <- y[w > 1]
y <- w[w > 1]
w <- length(y)
n <- sum(y * w)
Y / (n - 1) * sum((1 - 1 / w) * (w * y - Y / n)^2)
n
}
<- with(
var
survey,mapply(sps_var, split(sales, region), split(weight, region))
)
sqrt(sum(var)) / ht
#> [1] 0.03067625
```

Suppose another sample of businesses from the same frame is needed for a purpose other than measuring the value of sales. It is often desirable to negatively coordinate these samples so that the same businesses are not inundated with responding to surveys, without affecting the statistical properties of the sample. This sort of coordination is easily done by associating to each business a permanent random number, and suitably “rotating” them to reduce the overlap between both samples.

```
$prn <- runif(1000)
frame
head(frame)
#> revenue region prn
#> 1 2676 1 0.72614569
#> 2 2158 1 0.30042829
#> 3 2046 3 0.09393538
#> 4 537 2 0.31786097
#> 5 266 3 0.23796557
#> 6 1991 1 0.80298366
```

Permanent random numbers can be used with methods other than sequential Poisson—the procedure is the same for any order sampling scheme (including simple random sampling).

```
<- order_sampling(\(x) x / (1 - x))
pareto
<- with(frame, sps(revenue, allocation, region, prn))
sample
<- with(frame, pareto(revenue, allocation, region, (prn - 0.5) %% 1))
parsample
length(intersect(sample, parsample)) / 100
#> [1] 0.09
```

Although there is still a meaningful overlap between the units in both samples, this is roughly half of what would be expected without using permanent random numbers.

```
replicate(1000, {
<- with(frame, pareto(revenue, allocation, region))
s length(intersect(sample, s)) / 100
|>
}) summary()
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.1200 0.2000 0.2300 0.2275 0.2500 0.3500
```

The sequential part of sequential Poisson sampling means that it’s easy to grow a sample. Suppose that there is a need to sample 10 more businesses in region 1 after the sample is drawn. Simply adding 10 units to the allocation for region 1 results in a new sample that includes all the previously sampled units, so the extra units can be surveyed without discarding previously-collected data or affecting the statistical properties of the sample.

```
<- with(frame, sps(revenue, allocation, region, prn))
sample
<- with(frame, sps(revenue, allocation + c(10, 0, 0), region, prn))
sample_tu
all(sample %in% sample_tu)
#> [1] TRUE
```

As with any proportional-to-size sampling scheme, there is a critical sample size after which some units become take-all units. If these units are not already in the sample then they can “bump” previously sampled units, requiring a larger sample size to keep all the previously sampled units in the new sample.

```
<- function(x, alpha = 1e-3) {
becomes_ta <- rev(order(x, decreasing = TRUE))
ord <- x[ord]
x <- ceiling(cumsum(x) / x * (1 - alpha)) + length(x) - seq_along(x)
res order(ord)]
res[
}
Map(\(x) head(becomes_ta(x)), split(frame$revenue, frame$region))
#> $`1`
#> [1] 86 98 102 174 161 160
#>
#> $`2`
#> [1] 278 157 261 254 110 284
#>
#> $`3`
#> [1] 283 500 500 344 449 482
```

But this is rare in practice. For this example there is no point at which increasing the sample size drops a unit that was previously included in the sample. Seeing this in action requires different data.

```
set.seed(13026)
<- rlnorm(10)
x <- runif(10)
u
becomes_ta(x)
#> [1] 10 4 5 5 5 9 8 10 9 9
<- sps(x, 4, prn = u)
sample
%in% sps(x, 5, prn = u)
sample #> [1] TRUE TRUE TRUE FALSE
```

The solution to this problem is to simply increase the size of the sample until all previously sampled units are included.

```
%in% sps(x, 6, prn = u)
sample #> [1] TRUE TRUE TRUE TRUE
```

Despite it’s simplicity, sequential Poisson sampling is only asymptotically proportional to size. This means that the Horvitz-Thompson estimator can be biased in small samples, although this bias is usually negligible for real-world sample sizes.

```
<- replicate(1000, {
sampling_distribution <- with(frame, sps(revenue, allocation, region))
sample sum(sales[sample] * weights(sample))
})
summary(sampling_distribution / sum(sales) - 1)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -0.1043517 -0.0208040 0.0016497 0.0005927 0.0215847 0.1426320
```