# 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),
eligible_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 dataset of cluster listings
# including cluster names and number of households
cl <- tibble::tribble(
~cluster, ~n_houses,
"Village A", 23,
"Village B", 42,
"Village C", 56,
"Village D", 38
)
# add weights to a cluster sample
# include weights for cluster, household and individual levels
add_weights_cluster(x, cl = cl,
eligible = eligible_n,
interviewed = surveyed_n,
cluster_cl = cluster, household_cl = n_houses,
cluster_x = cluster, household_x = household_id,
ignore_cluster = FALSE, ignore_household = FALSE)
# add weights to a cluster sample
# ignore weights for cluster and household level (set equal to 1)
# only include weights at individual level
add_weights_cluster(x, cl = cl,
eligible = eligible_n,
interviewed = surveyed_n,
cluster_cl = cluster, household_cl = n_houses,
cluster_x = cluster, household_x = household_id,
ignore_cluster = TRUE, ignore_household = TRUE)
Run the code above in your browser using DataLab