if (FALSE) {
# Example 1: Quadratic form for successive-difference variance 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[
order(library_stsys_sample$SAMPLING_SORT_ORDER),
]
## Create a survey design object
design_obj <- svydesign(
data = library_stsys_sample,
strata = ~ SAMPLING_STRATUM,
ids = ~ 1,
fpc = ~ STRATUM_POP_SIZE
)
## Obtain quadratic form
quad_form_matrix <- get_design_quad_form(
design = design_obj,
variance_estimator = "SD2"
)
## Estimate variance of estimated population total
y <- design_obj$variables$LIBRARIA
wts <- weights(design_obj, type = 'sampling')
y_wtd <- as.matrix(y) * wts
y_wtd[is.na(y_wtd)] <- 0
pop_total <- sum(y_wtd)
var_est <- t(y_wtd) %*% quad_form_matrix %*% y_wtd
std_error <- sqrt(var_est)
print(pop_total); print(std_error)
# Compare to estimate from assuming SRS
svytotal(x = ~ LIBRARIA, na.rm = TRUE,
design = design_obj)
# Example 2: Kernel-based variance estimator ----
Q_BOSB <- get_design_quad_form(
design = design_obj,
variance_estimator = "BOSB",
aux_var_names = "SAMPLING_SORT_ORDER"
)
var_est <- t(y_wtd) %*% Q_BOSB %*% y_wtd
std_error <- sqrt(var_est)
print(pop_total); print(std_error)
# Example 3: Two-phase design (second phase is nonresponse) ----
## Estimate response propensities, separately by stratum
library_stsys_sample[['RESPONSE_PROB']] <- svyglm(
design = design_obj,
formula = I(RESPONSE_STATUS == "Survey Respondent") ~ SAMPLING_STRATUM,
family = quasibinomial('logistic')
) |> predict(type = 'response')
## Create a survey design object,
## where nonresponse is treated as a second phase of sampling
twophase_design <- twophase(
data = library_stsys_sample,
strata = list(~ SAMPLING_STRATUM, NULL),
id = list(~ 1, ~ 1),
fpc = list(~ STRATUM_POP_SIZE, NULL),
probs = list(NULL, ~ RESPONSE_PROB),
subset = ~ I(RESPONSE_STATUS == "Survey Respondent")
)
## Obtain quadratic form for the two-phase variance estimator,
## where first phase variance contribution estimated
## using the successive differences estimator
## and second phase variance contribution estimated
## using the Horvitz-Thompson estimator
## (with joint probabilities based on assumption of Poisson sampling)
get_design_quad_form(
design = twophase_design,
variance_estimator = list(
"SD2",
"Poisson Horvitz-Thompson"
)
)
}
Run the code above in your browser using DataLab