library(survey)
# Example 1: A multistage sample with two stages of SRSWOR
## Load an example dataset from a multistage sample, with two stages of SRSWOR
data("mu284", package = 'survey')
multistage_srswor_design <- svydesign(data = mu284,
ids = ~ id1 + id2,
fpc = ~ n1 + n2)
## Convert the survey design object to a bootstrap design
set.seed(2022)
bootstrap_rep_design <- as_bootstrap_design(multistage_srswor_design,
replicates = 500)
## Compare std. error estimates from bootstrap versus linearization
data.frame(
'Statistic' = c('total', 'mean', 'median'),
'SE (bootstrap)' = c(SE(svytotal(x = ~ y1, design = bootstrap_rep_design)),
SE(svymean(x = ~ y1, design = bootstrap_rep_design)),
SE(svyquantile(x = ~ y1, quantile = 0.5,
design = bootstrap_rep_design))),
'SE (linearization)' = c(SE(svytotal(x = ~ y1, design = multistage_srswor_design)),
SE(svymean(x = ~ y1, design = multistage_srswor_design)),
SE(svyquantile(x = ~ y1, quantile = 0.5,
design = multistage_srswor_design))),
check.names = FALSE
)
# Example 2: A multistage-sample,
# first stage selected with unequal probabilities without replacement
# second stage selected with simple random sampling without replacement
data("library_multistage_sample", package = "svrep")
multistage_pps <- svydesign(data = library_multistage_sample,
ids = ~ PSU_ID + SSU_ID,
fpc = ~ PSU_SAMPLING_PROB + SSU_SAMPLING_PROB,
pps = "brewer")
bootstrap_rep_design <- as_bootstrap_design(
multistage_pps, replicates = 500,
samp_method_by_stage = c("PPSWOR", "SRSWOR")
)
## Compare std. error estimates from bootstrap versus linearization
data.frame(
'Statistic' = c('total', 'mean'),
'SE (bootstrap)' = c(
SE(svytotal(x = ~ TOTCIR, na.rm = TRUE,
design = bootstrap_rep_design)),
SE(svymean(x = ~ TOTCIR, na.rm = TRUE,
design = bootstrap_rep_design))),
'SE (linearization)' = c(
SE(svytotal(x = ~ TOTCIR, na.rm = TRUE,
design = multistage_pps)),
SE(svymean(x = ~ TOTCIR, na.rm = TRUE,
design = multistage_pps))),
check.names = FALSE
)
Run the code above in your browser using DataLab