# \donttest{
# Example 1: A cluster sample
data('library_multistage_sample', package = 'svrep')
replicate_factors <- make_doubled_half_bootstrap_weights(
num_replicates = 5,
samp_unit_ids = library_multistage_sample$PSU_ID,
strata_ids = rep(1, times = nrow(library_multistage_sample)),
samp_unit_sel_probs = library_multistage_sample$PSU_SAMPLING_PROB,
output = "factors"
)
# Example 2: A single-stage sample selected with unequal probabilities, without replacement
## Load an example dataset of U.S. counties states with 2004 Presidential vote counts
data("election", package = 'survey')
pps_wor_design <- svydesign(data = election_pps,
pps = "overton",
fpc = ~ p, # Inclusion probabilities
ids = ~ 1)
## Create bootstrap replicate weights
set.seed(2022)
bootstrap_replicate_weights <- make_doubled_half_bootstrap_weights(
num_replicates = 5000,
samp_unit_ids = pps_wor_design$cluster[,1],
strata_ids = pps_wor_design$strata[,1],
samp_unit_sel_probs = pps_wor_design$prob
)
## Create a replicate design object with the survey package
bootstrap_rep_design <- svrepdesign(
data = pps_wor_design$variables,
repweights = bootstrap_replicate_weights,
weights = weights(pps_wor_design, type = "sampling"),
type = "bootstrap"
)
## Compare std. error estimates from bootstrap versus linearization
data.frame(
'Statistic' = c('total', 'mean'),
'SE (bootstrap)' = c(SE(svytotal(x = ~ Bush, design = bootstrap_rep_design)),
SE(svymean(x = ~ I(Bush/votes),
design = bootstrap_rep_design))),
'SE (Overton\'s PPS approximation)' = c(SE(svytotal(x = ~ Bush,
design = pps_wor_design)),
SE(svymean(x = ~ I(Bush/votes),
design = pps_wor_design))),
check.names = FALSE
)
# }
Run the code above in your browser using DataLab