if (FALSE) {
library(survey)
# Example 1: Bootstrap based on the Yates-Grundy estimator ----
set.seed(2014)
data('election', package = 'survey')
## Create survey design object
pps_design_yg <- svydesign(
data = election_pps,
id = ~1, fpc = ~p,
pps = ppsmat(election_jointprob),
variance = "YG"
)
## Convert to generalized bootstrap replicate design
gen_boot_design_yg <- pps_design_yg |>
as_gen_boot_design(variance_estimator = "Yates-Grundy",
replicates = 1000, tau = "auto")
svytotal(x = ~ Bush + Kerry, design = pps_design_yg)
svytotal(x = ~ Bush + Kerry, design = gen_boot_design_yg)
# Example 2: Bootstrap based on the successive-difference estimator ----
data('library_stsys_sample', package = 'svrep')
## First, ensure data are sorted in same order as was used in sampling
library_stsys_sample <- library_stsys_sample |>
sort_by(~ SAMPLING_SORT_ORDER)
## Create a survey design object
design_obj <- svydesign(
data = library_stsys_sample,
strata = ~ SAMPLING_STRATUM,
ids = ~ 1,
fpc = ~ STRATUM_POP_SIZE
)
## Convert to generalized bootstrap replicate design
gen_boot_design_sd2 <- as_gen_boot_design(
design = design_obj,
variance_estimator = "SD2",
replicates = 2000
)
## Estimate sampling variances
svytotal(x = ~ TOTSTAFF, na.rm = TRUE, design = gen_boot_design_sd2)
svytotal(x = ~ TOTSTAFF, na.rm = TRUE, design = design_obj)
# Example 3: Two-phase sample ----
# -- First stage is stratified systematic sampling,
# -- second stage is response/nonresponse modeled as Poisson sampling
nonresponse_model <- glm(
data = library_stsys_sample,
family = quasibinomial('logit'),
formula = I(RESPONSE_STATUS == "Survey Respondent") ~ 1,
weights = 1/library_stsys_sample$SAMPLING_PROB
)
library_stsys_sample[['RESPONSE_PROPENSITY']] <- predict(
nonresponse_model,
newdata = library_stsys_sample,
type = "response"
)
twophase_design <- twophase(
data = library_stsys_sample,
# Identify cases included in second phase sample
subset = ~ I(RESPONSE_STATUS == "Survey Respondent"),
strata = list(~ SAMPLING_STRATUM, NULL),
id = list(~ 1, ~ 1),
probs = list(NULL, ~ RESPONSE_PROPENSITY),
fpc = list(~ STRATUM_POP_SIZE, NULL),
)
twophase_boot_design <- as_gen_boot_design(
design = twophase_design,
variance_estimator = list(
"SD2", "Poisson Horvitz-Thompson"
)
)
svytotal(x = ~ LIBRARIA, design = twophase_boot_design)
}
Run the code above in your browser using DataLab