draw() sets the sample size or sampling fraction, the selection method,
and whatever that method needs: a measure of size for PPS, auxiliary
variables for balanced sampling, coordinates for spatial spread. Every
stage in a sampling design ends with draw(), which closes it. A second
draw(), or a stratify_by() or cluster_by() after it, is refused, and
sampling_design() describes the stage grammar.
Usage
draw(
.data,
n = NULL,
frac = NULL,
...,
min_n = NULL,
max_n = NULL,
reserve = NULL,
method = "srswor",
mos = NULL,
prn = NULL,
aux = NULL,
spread = NULL,
round = "up",
control = NULL,
certainty_size = NULL,
certainty_prop = NULL,
certainty_overflow = "error",
on_empty = "error",
on_capped = "warn"
)Arguments
- .data
A
sampling_designobject (piped fromsampling_design(),add_stage(),stratify_by(), orcluster_by()).- n
Sample size. Can be:
A scalar: the size taken in each stratum when
stratify_by()has noalloc, or the total shared across the strata when it has one. At a later stage either reading applies within each selected parent, so a total is a total per parent, not one for the whole survey.A named vector: stratum-specific sizes (for single stratification variable). A numeric stratum is named by its number as
as.character()writes it under anyscipen, as svyplan names its sizes, and the same holds for a character column of a data frame. A name, or a data frame row, for a stratum the stage cannot reach is refused at execution (samplyr_error_alloc_unknown_strata). A named vector or a data frame gives the sizes itself, so it is refused alongsidealloc(samplyr_error_alloc_named_n_with_alloc).A data frame: stratum-specific sizes, with every stratification column of
stratify_by()and anncolumn. For a take per parent unit, stratify the stage by the parent's cluster id:add_stage() |> stratify_by(ea_id) |> draw(n = take). The table may cover every parent of the design, and a continuation on a listing of the selected parents uses only their rows. Entries for parents the sample did not select are not checked, so a mistyped identifier that matches no selected parent passes unnoticed.A svyplan size or plan object, a certainty-aware
svyplan::n_alloc_joint()plan with apsuregister included. Each stage takes its own size from the plan, as "Sizes from svyplan" in Details describes, and a precision assessment or ansvyplan::n_panel()plan is refused.
Fixed-size methods and allocation methods need whole units. For random-size methods (
bernoulli,pps_poisson, or a custom method registered withfixed_size = FALSE),nis the expected sample size, converted tofrac = n / N, and may be fractional (n = 2.6) when no allocation method is given. At a later stage it is the expected take in each parent unit, whatever the parent's size. selection_methods explains the fixed versus random distinction and the warning raised whenpps_poissonfalls short ofn.- frac
Sampling fraction: a scalar for all strata, a named vector of stratum-specific fractions, or a data frame with every stratification column and a
fraccolumn. Givenorfrac, not both.- ...
These dots are for future extensions and must be empty. Arguments after
...must be named in full, so a positional fourth argument is refused rather than taken formin_n.- min_n
Minimum sample size per stratum,
NULL(no minimum) by default. Withfrac, a stratum whose roundedN_h * fracfalls short is raised tomin_n. With an allocation method, a stratum assigned fewer units is raised tomin_nand the other strata give up the difference. For without-replacement designs amin_nabove a stratum's population makes that stratum a census.min_ncounts the stratum's whole take, certainty units included, so a stratum can hold fewer thanmin_nunits drawn outside certainty, andexecute()names one left with a single such unit (samplyr_message_singleton_pool). Allocations that give a nonempty stratum zero units are refused.min_n = 1requests positive allocations explicitly. For minimums that differ by stratum, compute the sizes and pass them as a namednor a data frame.- max_n
Maximum sample size per stratum,
NULL(no maximum) by default. Withfrac, a stratum's rounded take is capped atmax_n. With an allocation method, a stratum assigned more is capped and the surplus goes to the other strata.With
frac, the bounds follow SPSS CSPLAN (RATEwithMINSIZEandMAXSIZE) and SAS SURVEYSELECT (SAMPRATE=withNMIN=andNMAX=). Each stratum, or each parent unit at a later stage, is bounded on its own afterround, with no fixed total. A random-size method bounds its expected size. Withnand no allocation method, the bounds are ignored with a warning.Under an allocation method the bounds narrow a range the stratum population already caps. The "Population bounds and redistribution" section of
stratify_by()gives how the surplus is shared and why with-replacement methods differ.frame_summary(design, frame, detail = "pool")reports the per-stratum target each bound produces, without selecting or consuming random numbers.- reserve
Number of reserve units listed after the main take of each pool,
NULL(no reserve) by default, a whole number of at least 1, orInfto list every unit of the pool. Each pool lists its take plusreserveunits in a random order, the take first and the reserve in release order.nandfrackeep their meaning, the take to be interviewed, and a pool too small for the whole list lists what it has. A svyplan plan given asnwith a rate below 1 for the units the reserve replaces warns (samplyr_warning_reserve_plan_rates), since its take can already make up for them. A reserve needsmethod = "srswor"or"systematic"and a take of at least 2 in every pool, whichexecute()andvalidate_frame()check for a take given byfrac, an allocation or a plan, before anything is drawn. One stage of a design can carry a reserve, and panels cannot be assigned at that stage.The executed sample holds the whole list, ranked in
.reserve_kand weighted as a list, and is refused by the exports (samplyr_error_reserve_unsettled) untilexecute()settles it from the response status the field recorded. reserve_lists describes the field protocol, the list's weights, the settlement and how to size the reserve.- method
Selection method,
"srswor"by default. selection_methods lists the seventeen built-ins with what each one requires and the paper behind each."cube"balances the sample onauxso that Horvitz-Thompson estimates of auxiliary totals match or nearly match population totals, with equal or unequal (mos) inclusion probabilities, and uses the stratified cube algorithm (Chauvet 2009) when stratified. At most two stages may use a balanced-family method."balanced"is a compatibility alias for"cube".A method registered with
sondage::register_method()is named"pps_<name>"or"balanced_<name>". "Registered methods" in selection_methods gives what its registration declares and whydraw()refuses one whoseprobabilitiestier is"unknown".- mos
Measure of size, a bare column name. Required for built-in PPS methods and registered
pps_methods. Optional forcube,lpm2,scps, and registeredbalanced_methods, which use equal inclusion probabilities without it. A column name held in a variable is written.data[[v]], here and inprn,auxandspread. A quoted string is refused with classsamplyr_error_draw_string_column.- prn
Permanent random numbers for sample coordination, a bare numeric column with values in the open interval (0, 1) and no missing values. Supported by
"bernoulli","pps_poisson","pps_sps","pps_pareto", and"scps". The sample is then deterministic for a given set of PRN values, which coordinates samples across survey occasions. With"scps"it is also fixed by the order the pool's units are visited in, the frame's row order or thecontrolorder, so coordinated draws need the same order. Keeping two samples apart needs an update of the PRNs that depends on the method,1 - prnfor"pps_sps","pps_pareto"and"scps".vignette("design-semantics")gives the rules.- aux
Cube balancing declarations for
method = "cube", or ordinary balancing variables for a registered balanced method that declaressupports_aux = TRUE. Bare numeric columns, such asaux = c(income, pop_density), request approximate Horvitz-Thompson total balance. Abound()marker requests adjacent-integer count bounds for every observed category, as inaux = c(income, bound(region), bound(urban_rural)), with a separatebound()for each marginal constraint. Withcluster_by(), ordinary auxiliary values are summed to cluster level, while bound variables must be constant within each cluster.- spread
Spatial coordinates for
method = "lpm2"or"scps", or for a registered balanced method declaringsupports_spread = TRUE, which requires them. Bare numeric columns such asspread = c(longitude, latitude), finite, with no missing values, and placed on comparable scales. Withcluster_by(), coordinates must be constant within each cluster.- round
Rounding when
fracis converted to the sample size of a fixed-size method. One of:"up"(default): ceiling, the SAS SURVEYSELECT default."down": floor."nearest": nearest integer, halves up, as SPSS rounds a rate.
An
ngiven directly is not rounded. A product within floating-point error of an integer counts as that integer, sofrac = 0.07of 100 units draws 7 under every rule. After rounding, every stratum or group receives at least 1 unit. When that takes every pool whole,execute()warns withsamplyr_warning_census. A random-size method, built in or registered withfixed_size = FALSE, takes the expected sizeN * fracunrounded, which may be fractional or below 1.- control
<
data-masking> Variables for sorting the frame before selection. Can be:A single variable:
control = regionMultiple variables:
control = c(region, district)With
serp()for serpentine sorting:control = serp(region, district)With
dplyr::desc()for descending:control = c(region, desc(population))Mixed:
control = c(region, serp(district, commune), desc(size))
Character values sort by their bytes in UTF-8, as in the C locale, so the order and the sample do not depend on the session locale. Missing values sort last, also under
desc(). Ties keep the frame's order. See the "Control sorting" section.- certainty_size
For PPS methods, units with MOS >= this value are selected with certainty (probability 1), and once under a method that can take a unit more than once. A scalar for all strata, or a data frame with the stratification columns and a
certainty_sizecolumn. Certainty units are removed from the frame before probability sampling, which draws the remaining sample size. Mutually exclusive withcertainty_prop. Equivalent to SAS SURVEYSELECTCERTSIZE=.- certainty_prop
For PPS methods, units whose MOS share (MOS_i / sum(MOS)) >= this value are selected with certainty. A scalar strictly between 0 and 1, or a data frame with the stratification columns and a
certainty_propcolumn. Shares are recomputed after removing certainty units, until no new unit qualifies. Mutually exclusive withcertainty_size.- certainty_overflow
What to do when certainty units exceed the target sample size of a sampling pool (a stratum within its parent, when applicable). One of:
"error"(default): stop with an error."allow": permit a census above the target only when every unit in that pool is certain. All units in the pool get stage weight 1.
Under either setting, certainty units that exactly exhaust or exceed the target are refused if any noncertainty units remain in the pool, because those units would have zero inclusion probability.
- on_empty
What to do when this stage selects nothing or has nothing to select from: a random-size method (
bernoulli,pps_poisson, or a custom method registered withfixed_size = FALSE) that selects zero units in a stratum or the whole frame, or a unit selected at the previous stage with no rows in this stage's frame, such as a household with no eligible member. One of:"error"(default): stop with an error. Zero selections usually signal a design problem, such as a sampling fraction or a stratum that is too small."warn": warn and keep the empty selection."silent": keep the empty selection without a message, at the draw or at export.
An empty selection is a valid realization of a random-size design. It contributes zero to Horvitz-Thompson totals, so estimates over repeated executions remain unbiased. Later stages of a multi-stage design then have nothing to select from, and the sample is empty.
"warn"and"silent"are meant for simulation and replicated runs, so checknrow()before analyzing a single realization.A selected unit with no rows in the next frame is refused under
"error"(samplyr_error_frame_missing_parent) and kept with nothing under it otherwise. execution_conditions gives how such a unit is carried, and why a replicated execution with empty replicates cannot be continued.- on_capped
What to do when a fixed take
n, drawn without replacement, exceeds the units of a pool, such as a take of 12 households in an EA that lists 10. One of:"warn"(default): take every unit of the pool and warn withsamplyr_warning_size_capped(orsamplyr_warning_censuswhen every pool of the stage is exhausted). The inclusion probabilities describe that take-all rule, so the weights stay valid."error": refuse before anything is drawn (samplyr_error_size_capped), naming the stage and the short pools.
"error"is refused with a random-size method, a method with replacement, orfrac, where no fixed take can fall short. execution_conditions gives which pools the check covers and when it runs.
Details
Sizes from svyplan
A svyplan size or plan object is read through svyplan's documented
coercions, stage by stage for cluster and stratified multistage plans.
An allocation from svyplan::n_alloc() or svyplan::n_alloc_joint()
that plans PSUs gives stage 1 its whole PSU counts (n_1_int) and each
later stage k its whole take per parent unit (take_k_int), up to the
plan's stages. A plan giving one size for a stage stratified without
alloc is refused (samplyr_error_svyplan_total_per_stratum) rather
than taken in every stratum. A plan for a stage stratified by several
variables is matched on those columns of its table, which svyplan
carries from the frame's character and factor columns, and a plan
without them is refused (samplyr_error_svyplan_multivariable_strata).
A precision assessment from a prec_*() function is not a size and is
refused. So is an svyplan::n_panel() plan, which sizes a whole panel,
and the refusal names the count to pass instead. A fixed panel issues
ceiling(plan$n_issued) units. A rotating panel is issued selection by
selection, and how many units its startup takes depends on its start
and rotation, which svyplan::design_schedule() resolves. Each
selection's operational_issue in the schedule's selections table is
the count to draw, a count of PSUs when the schedule was built with
take, and with subrounds the sum of each panel's rounded issue.
rotation_program() checks the count against the schedule.
issue <- with(schedule$selections, setNames(operational_issue, selection))
startup <- sampling_design() |> draw(n = issue[["startup"]])The planning choices are covered in
vignette("joint-allocation", package = "svyplan").
A certainty-aware svyplan::n_alloc_joint() plan, solved with a psu
register carrying psu_id, is given at a clustered stage 1 stratified by
the plan's one stratum column, and again at stage 2 for the per-PSU
takes. The takes may also come lower, below stages that keep one unit of
each PSU, such as draw(n = 1) of a segment per EA, and are then taken
inside that unit. Stage 1 takes every certainty PSU and draws exactly the
plan's remainder per stratum. A plan solved with n_psu_per_zone = 2
draws two PSUs from each of its zones instead, records the zone in
.zone_1, and exports one variance stratum per zone. With
n_psu_per_zone = 1 it draws one PSU per zone, records the plan's
variance group in .variance_group_1, and exports the groups as variance
strata (variance_estimation). Stage 1's method must be
pps_systematic, pps_brewer, pps_cps or pps_sampford, with mos
equal to the register's N. The frame's PSU ids are matched to psu_id
by value, so both must be numbers or both labels, and 64-bit integer ids
(bit64) are matched exactly. A design file holds such ids only from
-2^53 to 2^53 (write_design()). The take stage selects
by srswor or systematic, and stratifying there requires an alloc
method. Arguments the plan owns (frac, certainty_size,
certainty_prop, min_n, max_n, and at stage 1 alloc) are refused
alongside it. svyplan::merge_psus() merges PSUs smaller than the take.
Certainty selection
In PPS without-replacement sampling, very large units can have
theoretical inclusion probabilities above 1. Certainty selection takes
them with probability 1 before sampling the remainder, and marks them in
the .certainty_k column described in sample_columns. It is available
for the WOR PPS methods (pps_systematic, pps_brewer, pps_cps,
pps_sampford, pps_poisson, pps_sps, pps_pareto), for the methods
that can take a unit more than once (pps_multinomial, pps_chromy,
pps_systematic_mr), and for methods registered with type = "wor" or
"wr". A pool censused under
certainty_overflow = "allow" has stage weight 1, but in a multi-stage
design the final .weight can still exceed 1 because it compounds all
stage weights.
Without a declared rule, the fixed-size methods spread n by
sondage::inclusion_prob(). A unit whose share of its pool's size is
above 1 / n gets chance 1, and the rest of the take is spread over the
other units, repeatedly until no chance exceeds 1. Such units are
selected with certainty, marked in .certainty_k, and counted in n.
execute() reports them with samplyr_message_certainty_selected.
certainty_size and certainty_prop state a rule in the design instead,
which also lets the threshold sit below 1. A unit drawn with chance 0.98
is in practice a certainty unit with an unstable weight.
A method that can take a unit more than once needs no rule, since a large
unit gets several hits, each with a subsample of its own at the next
stage. Under certainty_size or certainty_prop such a unit is taken
once instead, with weight 1, and the remaining n - n_c hits are drawn by
the method over the remaining sizes, where a unit can still be hit more
than once. A certainty PSU then has one subsample at the next stage. When
the size measure counts the units below, a take of m per hit is
self-weighting, and a certainty PSU keeps the same weight if its take is
m times its size over the remainder's interval, the remaining size
total divided by n - n_c. The export treats the certainty units as a
take-all stratum and the remaining hits as drawn with replacement.
pps_systematic_mr with certainty_size corresponds to SAS SURVEYSELECT
METHOD=PPS_SYS with CERTSIZE=, and pps_multinomial to
METHOD=PPS_WR. SAS has no CERTSIZE=P= for these methods, and no
certainty option for its Chromy method.
With pps_poisson, both n and frac give a target expected total
that includes certainty units, frac as the unrounded frac * N over
the original pool. After n_cert certainty units are
selected, the remaining probabilities are
pmin(1, (target - n_cert) * mos / sum(mos)) over the remaining units'
sizes. Thus n = 5 and frac = 0.5 on ten units give the same
probabilities, with or without a certainty rule. Clipping at one does not
redistribute the excess to other units, so the expected size can fall
below the target. frame_summary() reports both n_target and
n_expected.
Control sorting
Control sorting orders the frame before selection, giving implicit
stratification with the systematic and sequential methods (systematic,
pps_systematic, pps_systematic_mr, pps_chromy), where the sample
spreads evenly across the sorted variables. Other methods accept control but get no implicit
stratification from it. The sort can change which units a given seed
draws, but the sample does not spread over the sorted variables. A
coordinated pps_sps draw with prn, for example, keeps nothing of a
sort by district. "Spreading the sample" in selection_methods compares
the sort with the other ways to spread a sample.
Nested sorting (the default, control = c(var1, var2, var3)) sorts
ascending by each variable in turn. Serpentine sorting
(control = serp(var1, var2, var3)) alternates direction at each
hierarchy level, which minimizes the jumps between adjacent units. For a
geographic hierarchy, the last district of region 1 is then adjacent to
the last district of region 2.
With stratify_by(), sorting is applied within each stratum, combining
explicit stratification for variance control with implicit
stratification for sample spread.
See also
sampling_design() for creating designs,
stratify_by() for stratification,
cluster_by() for clustering,
execute() for running designs,
serp() for serpentine sorting,
variance_estimation for how each method's variance is estimated
Other design specification:
add_stage(),
cluster_by(),
sampling_design(),
stratify_by()
Examples
# Simple random sample of 100 EAs
sampling_design() |>
draw(n = 100) |>
execute(bfa_eas, seed = 1)
#> # A tbl_sample: 100 × 18
#> # Sampling: 1 stage | 100/44,570 units
#> # Weights: 445.7 [445.7, 445.7]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 43475 Est Gnagna Piéla Rural 971 112 8.95
#> 2 6592 Sud-Ouest Noumbiel Kpuéré Rural 88 12 7.89
#> 3 11611 Boucle du … Nayala Yaba Rural 111 15 8.97
#> 4 45236 Centre-Est Boulgou Béguédo Urban 939 201 0.32
#> 5 3549 Est Gourma Fada-N… Rural 263 32 3.4
#> 6 39095 Hauts-Bass… Kenedou… Sindo Rural 37 4 6.09
#> 7 21818 Centre-Est Kourite… Goungu… Rural 21 4 1.07
#> 8 15528 Centre Kadiogo Ouagad… Urban 1207 182 0.14
#> 9 14284 Est Gourma Matiak… Rural 178 21 8.86
#> 10 3433 Est Gourma Fada-N… Rural 285 34 8.55
#> # ℹ 90 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>
# Systematic sample of 10%
sampling_design() |>
draw(frac = 0.10, method = "systematic") |>
execute(bfa_eas, seed = 123)
#> # A tbl_sample: 4457 × 18
#> # Sampling: 1 stage | 4,457/44,570 units
#> # Weights: 10 [10, 10]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 11761 Boucle du … Bale Bagassi Rural 63 8 8.54
#> 2 11771 Boucle du … Bale Bagassi Rural 234 28 5.91
#> 3 11781 Boucle du … Bale Bagassi Rural 86 10 3.68
#> 4 11791 Boucle du … Bale Bagassi Rural 970 117 1.2
#> 5 11801 Boucle du … Bale Bagassi Rural 102 12 9.29
#> 6 11811 Boucle du … Bale Bagassi Rural 135 16 8.25
#> 7 11821 Boucle du … Bale Bagassi Rural 208 25 8.13
#> 8 11831 Boucle du … Bale Bagassi Rural 92 11 8.97
#> 9 11843 Boucle du … Bale Bagassi Rural 132 16 10.7
#> 10 11853 Boucle du … Bale Bagassi Rural 63 8 6.11
#> # ℹ 4,447 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>
# PPS sample of EAs using household count
sampling_design() |>
cluster_by(ea_id) |>
draw(n = 50, method = "pps_brewer", mos = households) |>
execute(bfa_eas, seed = 42)
#> # A tbl_sample: 50 × 19
#> # Sampling: 1 stage | 50 units
#> # Weights: 970.83 [117.62, 7498.52]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 1192 Boucle du … Bale Boromo Rural 2570 374 2.05
#> 2 36794 Boucle du … Bale Fara Rural 2115 309 1.07
#> 3 41812 Boucle du … Kossi Dokui Rural 718 99 1.24
#> 4 11459 Boucle du … Sourou Toéni Rural 461 68 0.36
#> 5 27089 Cascades Leraba Loumana Rural 491 66 9.54
#> 6 15148 Centre Kadiogo Ouagad… Urban 907 137 0.16
#> 7 15241 Centre Kadiogo Ouagad… Urban 983 148 0.14
#> 8 16887 Centre Kadiogo Ouagad… Urban 914 138 0.1
#> 9 18171 Centre Kadiogo Ouagad… Urban 1349 203 0.39
#> 10 831 Centre-Est Boulgou Bissiga Rural 368 81 3.22
#> # ℹ 40 more rows
#> # ℹ 11 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>, .certainty_1 <lgl>
# Systematic PPS on a frame sorted by province and commune, as many
# household survey protocols ask, which spreads the EAs over both
sampling_design() |>
stratify_by(region, urban_rural) |>
cluster_by(ea_id) |>
draw(n = 2, method = "pps_systematic", mos = households,
control = c(province, commune)) |>
execute(bfa_eas, seed = 7)
#> # A tbl_sample: 46 × 19
#> # Sampling: 1 stage | 46 units
#> # Weights: 1406.18 [8.25, 9844.81]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 9037 Boucle du … Bale Poura Urban 845 160 1.35
#> 2 9048 Boucle du … Bale Poura Urban 692 131 8.82
#> 3 33263 Boucle du … Kossi Nouna Rural 879 114 1.12
#> 4 34941 Boucle du … Sourou Tougan Rural 960 146 0.25
#> 5 20582 Cascades Comoe Banfora Rural 630 72 4.03
#> 6 10192 Cascades Comoe Sidéra… Rural 263 42 8.48
#> 7 26825 Centre Kadiogo Komki-… Rural 543 82 6.43
#> 8 13934 Centre Kadiogo Koubri Rural 1876 351 3.92
#> 9 15403 Centre Kadiogo Ouagad… Urban 987 149 1.65
#> 10 17199 Centre Kadiogo Ouagad… Urban 947 143 0.12
#> # ℹ 36 more rows
#> # ℹ 11 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>, .certainty_1 <lgl>
# Bernoulli sampling with frac (random sample size, expected ~5%)
sampling_design() |>
draw(frac = 0.05, method = "bernoulli") |>
execute(ken_enterprises, seed = 12345)
#> # A tbl_sample: 827 × 14
#> # Sampling: 1 stage | 827/17,004 units
#> # Weights: 20 [20, 20]
#> enterprise_id county region sector size_class employees revenue_millions
#> * <chr> <fct> <fct> <fct> <fct> <int> <dbl>
#> 1 KEN_00010 Nyandarua Central Chemic… Small 10 40.5
#> 2 KEN_00020 Kiambu Central Constr… Small 9 17.4
#> 3 KEN_00023 Kiambu Central Constr… Small 17 17.3
#> 4 KEN_00051 Kirinyaga Central Constr… Small 10 25.5
#> 5 KEN_00063 Murang'a Central Constr… Medium 28 81.9
#> 6 KEN_00065 Murang'a Central Constr… Medium 31 30.5
#> 7 KEN_00077 Murang'a Central Constr… Small 10 14.7
#> 8 KEN_00103 Nyandarua Central Constr… Small 14 18.4
#> 9 KEN_00117 Nyandarua Central Constr… Small 12 31.1
#> 10 KEN_00146 Nyeri Central Constr… Small 9 8.3
#> # ℹ 817 more rows
#> # ℹ 7 more variables: year_established <int>, exporter <lgl>, .weight <dbl>,
#> # .sample_id <int>, .stage <int>, .weight_1 <dbl>, .fpc_1 <int>
# Bernoulli sampling with expected n (converted to frac = 500/N)
sampling_design() |>
draw(n = 500, method = "bernoulli") |>
execute(bfa_eas, seed = 42)
#> # A tbl_sample: 526 × 18
#> # Sampling: 1 stage | 526/44,570 units
#> # Weights: 89.14 [89.14, 89.14]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 11781 Boucle du … Bale Bagassi Rural 86 10 3.68
#> 2 36665 Boucle du … Bale Fara Rural 518 76 8.89
#> 3 36750 Boucle du … Bale Fara Rural 107 16 4.14
#> 4 9032 Boucle du … Bale Poura Urban 432 82 4.38
#> 5 9047 Boucle du … Bale Poura Urban 181 34 4.7
#> 6 9053 Boucle du … Bale Poura Urban 436 83 3.19
#> 7 39999 Boucle du … Banwa Balavé Rural 1820 287 2.04
#> 8 34012 Boucle du … Banwa Sami Rural 135 20 5.63
#> 9 9680 Boucle du … Banwa Sanaba Rural 59 8 8.98
#> 10 23812 Boucle du … Banwa Solenzo Rural 679 91 8.85
#> # ℹ 516 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>
# Stratified with different sizes per stratum (data frame)
region_sizes <- data.frame(
region = levels(bfa_eas$region),
n = c(20, 12, 25, 18, 22, 16, 14, 15, 20, 18, 12, 10, 8)
)
sampling_design() |>
stratify_by(region) |>
draw(n = region_sizes) |>
execute(bfa_eas, seed = 123)
#> # A tbl_sample: 210 × 18
#> # Sampling: 1 stage | 210/44,570 units
#> # Weights: 212.24 [115.14, 414.4]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 33181 Boucle du … Kossi Nouna Rural 133 17 0.07
#> 2 33229 Boucle du … Kossi Nouna Rural 48 6 4.53
#> 3 21506 Boucle du … Kossi Doumba… Rural 485 83 4.82
#> 4 45264 Boucle du … Bale Pâ Rural 825 95 0.66
#> 5 26201 Boucle du … Sourou Di Rural 95 17 21.1
#> 6 26077 Boucle du … Mouhoun Dédoug… Rural 176 25 8.96
#> 7 25510 Boucle du … Kossi Bombor… Rural 246 36 0.44
#> 8 23774 Boucle du … Banwa Solenzo Rural 158 21 2.9
#> 9 8332 Boucle du … Mouhoun Ouarko… Rural 55 8 5.68
#> 10 43713 Boucle du … Mouhoun Safané Rural 97 14 6.46
#> # ℹ 200 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <dbl>
# Stratified with different rates per stratum (named vector)
sampling_design() |>
stratify_by(size_class) |>
draw(frac = c(Small = 0.02, Medium = 0.10, Large = 0.50)) |>
execute(ken_enterprises, seed = 42)
#> # A tbl_sample: 1330 × 14
#> # Sampling: 1 stage | 1,330/17,004 units
#> # Weights: 12.78 [2, 50]
#> enterprise_id county region sector size_class employees revenue_millions
#> * <chr> <fct> <fct> <fct> <fct> <int> <dbl>
#> 1 KEN_08403 Nairobi Nairobi Other… Medium 64 94.6
#> 2 KEN_14993 Siaya Nyanza… Other… Medium 73 141.
#> 3 KEN_08163 Nairobi Nairobi Other… Medium 53 68.2
#> 4 KEN_03931 Isiolo East a… Other… Medium 95 158.
#> 5 KEN_04478 Nairobi Nairobi Const… Medium 60 52.1
#> 6 KEN_02158 Tana River Coast Other… Medium 43 33.9
#> 7 KEN_06792 Nairobi Nairobi Hotel… Medium 41 81.7
#> 8 KEN_14492 Migori Nyanza… Const… Medium 54 57.8
#> 9 KEN_01181 Kilifi Coast Const… Medium 71 213.
#> 10 KEN_15690 Laikipia Rift V… Hotel… Medium 20 39.8
#> # ℹ 1,320 more rows
#> # ℹ 7 more variables: year_established <int>, exporter <lgl>, .weight <dbl>,
#> # .sample_id <int>, .stage <int>, .weight_1 <dbl>, .fpc_1 <dbl>
# Neyman allocation with minimum 2 per stratum (for variance estimation)
sampling_design() |>
stratify_by(region, alloc = "neyman", variance = bfa_eas_variance) |>
draw(n = 150, min_n = 2) |>
execute(bfa_eas, seed = 2026)
#> # A tbl_sample: 150 × 18
#> # Sampling: 1 stage | 150/44,570 units
#> # Weights: 297.13 [278.67, 332.4]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 34795 Boucle du … Sourou Tougan Rural 45 7 14.0
#> 2 44496 Boucle du … Mouhoun Tchéri… Rural 180 29 16.6
#> 3 9624 Boucle du … Banwa Sanaba Rural 177 23 6.56
#> 4 7012 Boucle du … Kossi Madouba Rural 387 48 6.18
#> 5 44420 Boucle du … Mouhoun Tchéri… Rural 63 10 22.3
#> 6 515 Boucle du … Kossi Barani Rural 651 85 1.11
#> 7 1144 Boucle du … Bale Boromo Rural 75 11 8.92
#> 8 8378 Boucle du … Bale Ouri Rural 693 98 9.26
#> 9 4909 Boucle du … Sourou Kassoum Rural 662 90 0.65
#> 10 21094 Boucle du … Kossi Bouras… Rural 25 4 5.1
#> # ℹ 140 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <dbl>
# Proportional allocation with min and max bounds
sampling_design() |>
stratify_by(region, alloc = "proportional") |>
draw(n = 200, min_n = 10, max_n = 50) |>
execute(bfa_eas, seed = 1)
#> # A tbl_sample: 200 × 18
#> # Sampling: 1 stage | 200/44,570 units
#> # Weights: 222.85 [161.2, 240.7]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 9648 Boucle du … Banwa Sanaba Rural 279 35 8.37
#> 2 11547 Boucle du … Sourou Toéni Rural 49 7 21.4
#> 3 41824 Boucle du … Kossi Dokui Rural 75 10 15.8
#> 4 11012 Boucle du … Banwa Tansila Rural 592 71 0.95
#> 5 32308 Boucle du … Sourou Lanfiè… Rural 57 8 9.49
#> 6 7017 Boucle du … Kossi Madouba Rural 599 74 0.74
#> 7 36700 Boucle du … Bale Fara Rural 402 59 6.36
#> 8 11611 Boucle du … Nayala Yaba Rural 111 15 8.97
#> 9 8342 Boucle du … Mouhoun Ouarko… Rural 94 13 8.1
#> 10 11626 Boucle du … Nayala Yaba Rural 56 7 8.31
#> # ℹ 190 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <dbl>
# 0.5% of the EAs in each province, at least 3 and at most 8 (SPSS rule)
sampling_design() |>
stratify_by(province) |>
draw(frac = 0.005, min_n = 3, max_n = 8, round = "nearest") |>
execute(bfa_eas, seed = 4)
#> # A tbl_sample: 211 × 18
#> # Sampling: 1 stage | 211/44,570 units
#> # Weights: 211.23 [105.67, 486]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 8601 Boucle du … Bale Pâ Rural 794 92 8.93
#> 2 9046 Boucle du … Bale Poura Urban 4132 784 3.25
#> 3 11829 Boucle du … Bale Bagassi Rural 133 16 8.41
#> 4 11740 Boucle du … Bale Yaho Rural 556 83 0.92
#> 5 9703 Boucle du … Banwa Sanaba Rural 272 35 7.14
#> 6 10937 Boucle du … Banwa Tansila Rural 246 30 9.82
#> 7 24033 Boucle du … Banwa Solenzo Rural 141 19 2.87
#> 8 9639 Boucle du … Banwa Sanaba Rural 300 38 6.42
#> 9 21112 Boucle du … Kossi Bouras… Rural 106 17 8.91
#> 10 12790 Boucle du … Kossi Djibas… Rural 914 130 2.91
#> # ℹ 201 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <dbl>
# Chromy's sequential PPS (SAS PPS_SEQ) with serpentine ordering, the
# order SAS gives CONTROL variables by default
sampling_design() |>
draw(n = 100, method = "pps_chromy", mos = households,
control = serp(region, province)) |>
execute(bfa_eas, seed = 2)
#> # A tbl_sample: 100 × 20
#> # Sampling: 1 stage | 100 draws
#> # Weights: 383.39 [87.45, 2142.43]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 39998 Boucle du … Banwa Balavé Rural 564 89 0.61
#> 2 40012 Boucle du … Banwa Balavé Rural 1312 207 0.82
#> 3 506 Boucle du … Kossi Barani Rural 911 119 1.07
#> 4 7012 Boucle du … Kossi Madouba Rural 387 48 6.18
#> 5 10551 Boucle du … Kossi Sônô Rural 676 101 0.23
#> 6 43817 Boucle du … Mouhoun Safané Rural 496 71 0.56
#> 7 6208 Boucle du … Nayala Kougny Rural 895 119 0.9
#> 8 34779 Boucle du … Sourou Tougan Rural 960 146 0.8
#> 9 7676 Cascades Leraba Niankô… Rural 992 136 8.52
#> 10 20626 Cascades Comoe Banfora Rural 984 113 0.49
#> # ℹ 90 more rows
#> # ℹ 12 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <dbl>, .draw_1 <int>,
#> # .certainty_1 <lgl>
# Systematic PPS with minimum replacement (SAS PPS_SYS). The largest EA
# of Poura holds more than an eighth of its households, so it expects
# 1.74 of 8 hits and is drawn once or twice, here twice. Each hit is a
# row, numbered by `.draw_1`, with weight 1 / 1.74.
poura <- subset(bfa_eas, commune == "Poura")
poura_sample <- sampling_design() |>
draw(n = 8, method = "pps_systematic_mr", mos = households) |>
execute(poura, seed = 1)
as.data.frame(poura_sample)[c("ea_id", "households", ".draw_1", ".weight")]
#> ea_id households .draw_1 .weight
#> 1 9030 107 1 4.2207944
#> 2 9035 34 2 13.2830882
#> 3 9040 128 3 3.5283203
#> 4 9044 142 4 3.1804577
#> 5 9046 784 5 0.5760523
#> 6 9046 784 6 0.5760523
#> 7 9051 13 7 34.7403846
#> 8 45243 201 8 2.2468905
# The export reads the hits as drawn with replacement, and warns that the
# frame order can make that variance far too small or too large
if (requireNamespace("survey", quietly = TRUE)) {
poura_svy <- as_svydesign(poura_sample)
}
#> Warning: A stage of this design used systematic selection: stage 1.
#> ✖ `as_svydesign()` is using a with-replacement approximation for stage 1.
#> ℹ Frame ordering or periodicity can make standard errors far too small or too
#> large. A frame whose period matches the sampling interval has been measured
#> at 0.0006 of the true variance for "systematic" and 0.003 for
#> "pps_systematic", with 95% intervals covering about 10% of the time. See
#> `?variance_estimation`.
#> ℹ On such a frame the with-replacement variance of "pps_systematic_mr" can be
#> exactly zero.
#> ℹ Use `systematic_variance = "approximate"` to accept the approximation, or
#> `"error"` to refuse it.
# Control sorting with nested (standard) ordering
sampling_design() |>
draw(n = 100, method = "systematic",
control = c(region, province)) |>
execute(bfa_eas, seed = 3)
#> # A tbl_sample: 100 × 18
#> # Sampling: 1 stage | 100/44,570 units
#> # Weights: 445.7 [445.7, 445.7]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 11833 Boucle du … Bale Bagassi Rural 35 4 7.15
#> 2 8620 Boucle du … Bale Pâ Rural 52 6 1.46
#> 3 34062 Boucle du … Banwa Sami Rural 286 43 7.68
#> 4 24046 Boucle du … Banwa Solenzo Rural 24 3 6.37
#> 5 25526 Boucle du … Kossi Bombor… Rural 152 22 0.06
#> 6 37398 Boucle du … Kossi Kombori Rural 201 25 5.89
#> 7 21002 Boucle du … Mouhoun Bondok… Rural 791 104 9.13
#> 8 5663 Boucle du … Mouhoun Kona Rural 180 28 7.94
#> 9 44432 Boucle du … Mouhoun Tchéri… Rural 502 80 23.6
#> 10 11649 Boucle du … Nayala Yaba Rural 526 69 7.66
#> # ℹ 90 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>
# Combined explicit stratification with control sorting within strata
sampling_design() |>
stratify_by(urban_rural) |>
draw(n = 50, method = "systematic",
control = serp(region, province)) |>
execute(bfa_eas, seed = 25)
#> # A tbl_sample: 100 × 18
#> # Sampling: 1 stage | 100/44,570 units
#> # Weights: 445.7 [137.66, 753.74]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 36744 Boucle du … Bale Fara Rural 565 83 9.07
#> 2 9728 Boucle du … Banwa Sanaba Rural 69 9 6.85
#> 3 25519 Boucle du … Kossi Bombor… Rural 240 35 4.62
#> 4 10541 Boucle du … Kossi Sônô Rural 738 110 0.6
#> 5 8318 Boucle du … Mouhoun Ouarko… Rural 160 22 7.76
#> 6 11674 Boucle du … Nayala Yaba Rural 1353 178 1.3
#> 7 34832 Boucle du … Sourou Tougan Rural 33 5 8.81
#> 8 20641 Cascades Comoe Banfora Rural 996 114 0.24
#> 9 15093 Cascades Comoe Niango… Rural 126 15 8.87
#> 10 10357 Cascades Comoe Sidéra… Rural 31 5 8.64
#> # ℹ 90 more rows
#> # ℹ 10 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>
# PPS with certainty selection (absolute threshold)
# Large EAs selected with certainty, rest sampled with PPS
sampling_design() |>
stratify_by(region) |>
draw(n = 100, method = "pps_brewer", mos = households,
certainty_size = 800) |>
execute(bfa_eas, seed = 3)
#> # A tbl_sample: 1300 × 19
#> # Sampling: 1 stage | 1,300/44,570 units
#> # Weights: 35.46 [1, 1097.49]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 11810 Boucle du … Bale Bagassi Rural 308 37 0.72
#> 2 11824 Boucle du … Bale Bagassi Rural 969 116 0.89
#> 3 11857 Boucle du … Bale Bagassi Rural 631 76 9.04
#> 4 1205 Boucle du … Bale Boromo Rural 4324 630 3.13
#> 5 36698 Boucle du … Bale Fara Rural 148 22 8.98
#> 6 8398 Boucle du … Bale Ouri Rural 651 92 7.01
#> 7 8401 Boucle du … Bale Ouri Rural 356 50 9.03
#> 8 8455 Boucle du … Bale Ouri Rural 57 8 5.95
#> 9 8568 Boucle du … Bale Pâ Rural 154 18 9.19
#> 10 8597 Boucle du … Bale Pâ Rural 301 35 0.42
#> # ℹ 1,290 more rows
#> # ℹ 11 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>, .certainty_1 <lgl>
# PPS with certainty selection (proportional threshold)
# EAs with >= 10% of stratum total selected with certainty
sampling_design() |>
stratify_by(region) |>
draw(n = 100, method = "pps_systematic", mos = households,
certainty_prop = 0.10) |>
execute(bfa_eas, seed = 321)
#> # A tbl_sample: 1300 × 19
#> # Sampling: 1 stage | 1,300/44,570 units
#> # Weights: 33.25 [2.16, 661.2]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 11818 Boucle du … Bale Bagassi Rural 37 4 6.38
#> 2 29503 Boucle du … Bale Bana Rural 530 69 0.45
#> 3 1136 Boucle du … Bale Boromo Rural 3613 526 4.23
#> 4 1177 Boucle du … Bale Boromo Rural 1383 201 2.06
#> 5 1207 Boucle du … Bale Boromo Rural 1465 213 1.67
#> 6 36701 Boucle du … Bale Fara Rural 638 93 0.81
#> 7 36734 Boucle du … Bale Fara Rural 1114 163 1.07
#> 8 36769 Boucle du … Bale Fara Rural 995 145 1.41
#> 9 8390 Boucle du … Bale Ouri Rural 682 97 8.14
#> 10 8435 Boucle du … Bale Ouri Rural 624 88 0.61
#> # ℹ 1,290 more rows
#> # ℹ 11 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>, .certainty_1 <lgl>
# Stratum-specific certainty thresholds (data frame)
cert_thresholds <- data.frame(
region = levels(bfa_eas$region),
certainty_size = c(700, 450, 800, 850, 750, 800, 550,
450, 700, 950, 750, 600, 480)
)
sampling_design() |>
stratify_by(region) |>
draw(n = 100, method = "pps_brewer", mos = households,
certainty_size = cert_thresholds) |>
execute(bfa_eas, seed = 424)
#> # A tbl_sample: 1300 × 19
#> # Sampling: 1 stage | 1,300/44,570 units
#> # Weights: 35.78 [1, 847.49]
#> ea_id region province commune urban_rural population households area_km2
#> * <int> <fct> <fct> <fct> <fct> <int> <int> <dbl>
#> 1 9046 Boucle du … Bale Poura Urban 4132 784 3.25
#> 2 11816 Boucle du … Bale Bagassi Rural 968 116 1.94
#> 3 11832 Boucle du … Bale Bagassi Rural 907 109 0.87
#> 4 11851 Boucle du … Bale Bagassi Rural 519 62 0.47
#> 5 29521 Boucle du … Bale Bana Rural 1008 132 0.63
#> 6 1136 Boucle du … Bale Boromo Rural 3613 526 4.23
#> 7 1177 Boucle du … Bale Boromo Rural 1383 201 2.06
#> 8 36672 Boucle du … Bale Fara Rural 771 113 8.94
#> 9 36692 Boucle du … Bale Fara Rural 901 132 8.41
#> 10 36702 Boucle du … Bale Fara Rural 335 49 6.47
#> # ℹ 1,290 more rows
#> # ℹ 11 more variables: pop_density <dbl>, longitude <dbl>, latitude <dbl>,
#> # remoteness <fct>, fieldwork_cost <int>, .weight <dbl>, .sample_id <int>,
#> # .stage <int>, .weight_1 <dbl>, .fpc_1 <int>, .certainty_1 <lgl>
# A two-stage svyplan allocation gives each stage its whole sizes: EAs
# per stratum at stage 1, households per EA at stage 2. Check that the
# search found a whole design before fieldwork.
sizes <- aggregate(cbind(N = households, N_1 = 1) ~ urban_rural,
bfa_eas, sum)
plan_frame <- data.frame(
stratum = sizes$urban_rural, N = sizes$N, N_1 = sizes$N_1,
sd = c(0.48, 0.45), mean = c(0.35, 0.28),
icc_1 = c(0.08, 0.04), cost_1 = c(600, 300), cost_2 = c(60, 40)
)
plan <- svyplan::n_alloc(plan_frame, cv = 0.03)
plan$operational$status
#> [1] "optimal"
plan$detail[c("stratum", "n_1_int", "take_2_int")]
#> stratum n_1_int take_2_int
#> 1 Rural 260 11
#> 2 Urban 78 13
two_stage <- sampling_design() |>
add_stage(label = "EA") |>
stratify_by(urban_rural) |>
cluster_by(ea_id) |>
draw(n = plan, method = "pps_brewer", mos = households) |>
add_stage(label = "Household") |>
stratify_by(urban_rural) |>
draw(n = plan)
selected_eas <- execute(two_stage, bfa_eas, stages = 1, seed = 3)
table(selected_eas$urban_rural)
#>
#> Rural Urban
#> 260 78
# A plan fitted on a register of EAs takes the EAs it holds certain, draws
# the rest, and takes each EA's own number of households
eas <- bfa_eas[
bfa_eas$province == "Oubritenga" & bfa_eas$households >= 12,
]
register <- data.frame(
psu_id = eas$ea_id, stratum = as.character(eas$urban_rural),
N = eas$households
)
register_frame <- aggregate(N ~ stratum, register, sum)
register_frame$take_2 <- 12
fit <- svyplan::n_alloc_joint(
register_frame,
data.frame(stratum = register_frame$stratum, name = "coverage",
p = 0.4, icc_1 = 0.05),
targets = data.frame(name = "coverage", cv = 0.04),
psu = register
)
fit$operational[c("n", "all_pass", "status")]
#> $n
#> [1] 1398
#>
#> $all_pass
#> [1] TRUE
#>
#> $status
#> [1] "optimal"
#>
fit$detail[c("stratum", "n_psu_certain", "n_psu_draw")]
#> stratum n_psu_certain n_psu_draw
#> 1 Rural 3 96
#> 2 Urban 1 16
sampling_design() |>
add_stage(label = "EA") |>
stratify_by(urban_rural) |>
cluster_by(ea_id) |>
draw(n = fit, method = "pps_brewer", mos = households) |>
add_stage(label = "Household") |>
draw(n = fit)
#> ── Sampling Design ─────────────────────────────────────────────────────────────
#>
#> ℹ 2 stages
#>
#> ── Stage 1: EA ─────────────────────────────────────────────────────────────────
#> • Strata: urban_rural
#> • Cluster: ea_id
#> • Draw: n = <svyplan plan, 4 certainty, 112 drawn, per stratum>, method = pps_brewer, mos = households
#>
#> ── Stage 2: Household ──────────────────────────────────────────────────────────
#> • Draw: n = <svyplan plan, 12 to 15 per ea_id>, method = srswor
#>
