R/add_weights_strata.R
add_weights_strata.Rd
Creates weight based on dividing stratified population counts from the source population by surveyed counts in the sample population.
add_weights_strata(
x,
p,
...,
population = population,
surv_weight = "surv_weight",
surv_weight_ID = "surv_weight_ID"
)
a data frame of survey data
a data frame containing population data for groups in ...
shared grouping columns across both x
and p
. These are used
to match the weights to the correct subset of the population.
the column in p
that defines the population numbers
the name of the new column to store the weights. Defaults to "surv_weight".
the name of the new ID column to be created. Defaults to "surv_weight_ID"
# 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 <- sitrep::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))
#> Error in loadNamespace(x): there is no package called ‘sitrep’
# make sure col names match survey dataset
p <- dplyr::rename(p, age_grp = groups, sex = strata, population = n)
#> Error in dplyr::rename(p, age_grp = groups, sex = strata, population = n): object 'p' not found
# 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)
#> Error in eval_select_impl(NULL, .vars, expr(c(!!!dots)), include = .include, exclude = .exclude, strict = .strict, name_spec = unique_name_spec, uniquely_named = TRUE, error_call = caller_env()): object 'p' not found