Inclusion probabilities: Stratified and Clustered Random Sampling
Source:R/strata_and_cluster_rs.R
strata_and_cluster_rs_probabilities.RdReturns each unit's probability of being sampled when clusters are drawn within strata. Probabilities vary across strata and are constant within a cluster.
Usage
strata_and_cluster_rs_probabilities(
strata = NULL,
clusters = NULL,
prob = NULL,
prob_unit = NULL,
n = NULL,
n_unit = NULL,
strata_n = NULL,
strata_prob = NULL,
check_inputs = TRUE
)Arguments
- strata
A vector of length N indicating which stratum each unit belongs to. Every unit in a cluster must belong to the same stratum. (required)
- clusters
A vector of length N indicating which cluster each unit belongs to. (required)
- prob
Use for a design in which either
floor(N_clusters_stratum*prob)orceiling(N_clusters_stratum*prob)clusters are sampled within each stratum. Which of the two is used is itself random: the ceiling is drawn with probability equal to the fractional part ofN_clusters_stratum*proband the floor otherwise, which makes each cluster's probability of inclusion exactlyprob. Must be a real number between 0 and 1 inclusive. (optional)- prob_unit
Must be of length N.
tapply(prob_unit, strata, unique)will be passed tostrata_prob, so it must be constant within each stratum. (optional)- n
Use for a design in which the scalar
ngives the fixed number of clusters to sample in every stratum. This count does not vary across strata. (optional)- n_unit
Must be of length N.
tapply(n_unit, strata, unique)will be passed tostrata_n, so it must be constant within each stratum. (optional)- strata_n
Use for a design in which
strata_ngives the number of clusters to sample within each stratum. Must be as long as the number of strata, in the same order assort(unique(strata)). (optional)- strata_prob
Use for a design in which
strata_probgives the probability of being sampled within each stratum. Must be in the same order assort(unique(strata)). Differs fromprobin that the probability of being sampled can vary across strata. (optional)- check_inputs
Logical. Whether to verify before sampling that the arguments are internally consistent: that clusters nest within strata, that counts do not exceed the number of clusters in a stratum, that probabilities lie between 0 and 1, and so on. Defaults to
TRUE. Set toFALSEto skip the checks when drawing many samples from arguments that have already been verified; declaring the design once withdeclare_rs()and drawing from it withdraw_rs()does this for you. (optional)
Value
A numeric vector of length N giving each unit's probability of being included in the sample. Every unit in a cluster shares one probability.
Details
These are the quantities inverse-probability weights are built from: weight
each sampled unit by the reciprocal of its inclusion probability, which
obtain_inclusion_probabilities() extracts for you.
Examples
# Twelve clusters, of sizes 1 through 12, nested in four strata of three
clusters <- rep(letters[1:12], times = 1:12)
strata <- rep(NA, length(clusters))
strata[clusters %in% letters[1:3]] <- "stratum_1"
strata[clusters %in% letters[4:6]] <- "stratum_2"
strata[clusters %in% letters[7:9]] <- "stratum_3"
strata[clusters %in% letters[10:12]] <- "stratum_4"
table(strata, clusters)
#> clusters
#> strata a b c d e f g h i j k l
#> stratum_1 1 2 3 0 0 0 0 0 0 0 0 0
#> stratum_2 0 0 0 4 5 6 0 0 0 0 0 0
#> stratum_3 0 0 0 0 0 0 7 8 9 0 0 0
#> stratum_4 0 0 0 0 0 0 0 0 0 10 11 12
probs <- strata_and_cluster_rs_probabilities(strata = strata,
clusters = clusters)
table(probs, strata)
#> strata
#> probs stratum_1 stratum_2 stratum_3 stratum_4
#> 0.5 6 15 24 33
table(probs, clusters)
#> clusters
#> probs a b c d e f g h i j k l
#> 0.5 1 2 3 4 5 6 7 8 9 10 11 12
probs <- strata_and_cluster_rs_probabilities(clusters = clusters,
strata = strata,
prob = 0.5)
table(probs, clusters)
#> clusters
#> probs a b c d e f g h i j k l
#> 0.5 1 2 3 4 5 6 7 8 9 10 11 12
table(probs, strata)
#> strata
#> probs stratum_1 stratum_2 stratum_3 stratum_4
#> 0.5 6 15 24 33
probs <- strata_and_cluster_rs_probabilities(clusters = clusters,
strata = strata,
strata_n = c(1, 2, 1, 2))
table(probs, clusters)
#> clusters
#> probs a b c d e f g h i j k l
#> 0.333333333333333 1 2 3 0 0 0 7 8 9 0 0 0
#> 0.666666666666667 0 0 0 4 5 6 0 0 0 10 11 12
table(probs, strata)
#> strata
#> probs stratum_1 stratum_2 stratum_3 stratum_4
#> 0.333333333333333 6 0 24 0
#> 0.666666666666667 0 15 0 33
probs <- strata_and_cluster_rs_probabilities(clusters = clusters,
strata = strata,
strata_prob = c(0.2, 0.4, 0.6, 0.8))
table(probs, clusters)
#> clusters
#> probs a b c d e f g h i j k l
#> 0.2 1 2 3 0 0 0 0 0 0 0 0 0
#> 0.4 0 0 0 4 5 6 0 0 0 0 0 0
#> 0.6 0 0 0 0 0 0 7 8 9 0 0 0
#> 0.666666666666667 0 0 0 0 0 0 0 0 0 10 11 12
table(probs, strata)
#> strata
#> probs stratum_1 stratum_2 stratum_3 stratum_4
#> 0.2 6 0 0 0
#> 0.4 0 15 0 0
#> 0.6 0 0 24 0
#> 0.666666666666667 0 0 0 33