# \donttest{
# Example using the API data from the survey package
library(survey)
data(api) # loads apipop, apisrs, apistrat, etc.
# Build a population frame and create some binary fields used in a formula
pop <- apipop
pop$api00_bin <- as.factor(ifelse(pop$api00 >= 700, "700plus", "lt700"))
pop$growth_bin <- as.factor(ifelse(pop$growth >= 0, "nonneg", "neg"))
pop$ell_bin <- as.factor(ifelse(pop$ell >= 10, "highELL", "lowELL"))
pop$comp.imp_bin <- as.factor(ifelse(pop$comp.imp >= 50, "highComp", "lowComp"))
pop$hsg_bin <- as.factor(ifelse(pop$hsg >= 60, "highHSG", "lowHSG"))
# A calibration formula with main effects + a few interactions
cal_formula <- ~ stype + growth_bin + api00_bin + ell_bin + comp.imp_bin + hsg_bin +
api00_bin:stype + hsg_bin:stype + comp.imp_bin:stype + api00_bin:growth_bin
# (Optional) frame weights if available; here we use unweighted totals
gp <- generate_population_totals(
population_df = pop,
calibration_formula = cal_formula,
include_intercept = TRUE
)
# Named totals ready for calibration:
head(gp$population_totals)
# If you later build a respondent model matrix, reuse gp$terms to ensure alignment:
# X_resp <- model.matrix(gp$terms, data = apisrs)
# stopifnot(identical(colnames(X_resp), names(gp$population_totals)))
# }
Run the code above in your browser using DataLab