# define a fake dataset of survey data
# including household and individual information
x <- data.frame(stringsAsFactors=FALSE,
cluster = c("Village A", "Village A", "Village A", "Village A",
"Village A", "Village B", "Village B", "Village B"),
household_id = c(1, 1, 1, 1, 2, 2, 2, 2),
eligibile_n = c(6, 6, 6, 6, 6, 3, 3, 3),
surveyed_n = c(4, 4, 4, 4, 4, 3, 3, 3),
individual_id = c(1, 2, 3, 4, 4, 1, 2, 3),
age_grp = c("0-10", "20-30", "30-40", "50-60", "50-60", "20-30",
"50-60", "30-40"),
sex = c("Male", "Female", "Male", "Female", "Female", "Male",
"Female", "Female"),
outcome = c("Y", "Y", "N", "N", "N", "N", "N", "Y")
)
# define a fake population data set
# including age group, sex, counts and proportions
p <- epikit::gen_population(total = 10000,
groups = c("0-10", "10-20", "20-30", "30-40", "40-50", "50-60"),
proportions = c(0.1, 0.2, 0.3, 0.4, 0.2, 0.1))
# make sure col names match survey dataset
p <- dplyr::rename(p, age_grp = groups, sex = strata, population = n)
# add weights to a stratified simple random sample
# weight based on age group and sex
add_weights_strata(x, p = p, age_grp, sex, population = population)
Run the code above in your browser using DataLab