# Full NHANES design: stratified cluster with PSU IDs nested within strata
d <- as_survey(
nhanes_2017,
ids = sdmvpsu,
weights = wtint2yr,
strata = sdmvstra,
nest = TRUE
)
# Stratified design without PSU cluster IDs
d_strat <- as_survey(nhanes_2017, weights = wtint2yr, strata = sdmvstra)
# Blood pressure analysis: filter to exam participants, use MEC weight
exam <- nhanes_2017[nhanes_2017$ridstatr == 2, ]
d_bp <- as_survey(exam, ids = sdmvpsu, weights = wtmec2yr,
strata = sdmvstra, nest = TRUE)
# c() to combine multiple columns — sketched on a synthetic two-stage frame
df <- data.frame(
psu = rep(1:5, each = 4),
ssu = 1:20,
wt = runif(20, 0.5, 2)
)
d_ms <- as_survey(df, ids = c(psu, ssu), weights = wt)
# Tidy-select helpers like starts_with() also work
d_h <- as_survey(
gss_2024,
ids = vpsu,
strata = vstrat,
weights = starts_with("wtssn"),
nest = TRUE
)
Run the code above in your browser using DataLab