merge_psus() recodes primary sampling unit (PSU) ids so that every PSU
holds at least min_size units, merging small PSUs with their neighbours
in the current row order. A PSU register for n_alloc() must not hold a
PSU smaller than its stratum's take, and merging small enumeration areas
before the register is built is the usual remedy.
It is a vector function. Apply it within each stratum, after sorting the frame so that neighbours in row order are neighbours on the ground, and build the register from the merged ids, so the register and the frame agree.
Value
A vector like id, each row's merged PSU id. A merged PSU takes
the id of its first PSU in row order, so its type and class are those of
id and every merged PSU traces to one source PSU.
Details
PSUs are taken in order of first appearance. A PSU of at least
min_size stays as it is. A run of smaller PSUs between two of them is
merged in order, closing a merged PSU as soon as its size reaches
min_size, and a remainder below min_size at the end of the run joins
the merged PSU before it. A run that is below min_size in total joins
the PSU before it, or the one after it when it opens the stratum. A
stratum below min_size in total becomes one PSU, which n_alloc()
refuses as smaller than its take unless the register flags it as
certainty.
Merging is by order only, so neighbours in row order are neighbours on the ground only as far as the sort makes them so. Valliant, Dever and Kreuter (2018, sec. 10.3) form PSUs to a minimum size from contiguous areas.
Called on a whole frame, merge_psus() sees one stratum and can merge
PSUs of different strata. Apply it per stratum, with split() or a
grouped dplyr::mutate(). A merge that crosses strata gives one id to
PSUs of two strata, which n_alloc() refuses as a duplicated psu_id.
References
Valliant, R., Dever, J. A., & Kreuter, F. (2018). Practical Tools for Designing and Weighting Survey Samples (2nd ed.). Springer. Section 10.3.
See also
n_alloc() for the register the merged ids feed.
Examples
listing <- data.frame(
stratum = rep(c("urban", "rural"), c(7, 6)),
ea = c(sprintf("U%d", 1:7), sprintf("R%d", 1:6)),
households = c(120, 6, 4, 95, 110, 3, 80, 70, 8, 60, 5, 2, 90)
)
# One row per enumeration area, already in geographic order
by_stratum <- split(listing, listing$stratum)
listing$psu_id <- unsplit(lapply(by_stratum, function(d) {
merge_psus(d$ea, min_size = 10, size = d$households)
}), listing$stratum)
listing
#> stratum ea households psu_id
#> 1 urban U1 120 U1
#> 2 urban U2 6 U2
#> 3 urban U3 4 U2
#> 4 urban U4 95 U4
#> 5 urban U5 110 U5
#> 6 urban U6 3 U5
#> 7 urban U7 80 U7
#> 8 rural R1 70 R1
#> 9 rural R2 8 R1
#> 10 rural R3 60 R3
#> 11 rural R4 5 R3
#> 12 rural R5 2 R3
#> 13 rural R6 90 R6
# The register n_alloc() reads
aggregate(households ~ stratum + psu_id, listing, sum)
#> stratum psu_id households
#> 1 rural R1 78
#> 2 rural R3 67
#> 3 rural R6 90
#> 4 urban U1 120
#> 5 urban U2 10
#> 6 urban U4 95
#> 7 urban U5 113
#> 8 urban U7 80