Calculating weighted prevalence for DHS dataset

Hi, I am trying to run code to calculate weighted prevalence for tobacco smoking from DHS dataset. I have managed to come up with the variables to using code below
MRdata <- MRdata %>%
mutate(rc_tobc_cig = case_when(
(mv464a > 0 & mv464a <= 888) | (mv464b > 0 & mv464b <= 888) | (mv464c > 0 & mv464c <= 888) |
(mv484a > 0 & mv484a <= 888) | (mv484b > 0 & mv484b <= 888) | (mv484c > 0 & mv484c <= 888) ~ 1,
TRUE ~ 0),
rc_tobc_cig = add_labels(rc_tobc_cig, labels = c("No"=0, "Yes"=1)),
rc_tobc_cig = set_label(rc_tobc_cig, label = "Smokes cigarettes"))

MRdata <- MRdata %>%
mutate(rc_tobc_other = case_when(
(mv464d > 0 & mv464d <= 888) | (mv464e > 0 & mv464e <= 888) | (mv464f > 0 & mv464f <= 888) | (mv464g > 0 & mv464g <= 888) |
(mv484d > 0 & mv484d <= 888) | (mv484e > 0 & mv484e <= 888) | (mv484f > 0 & mv484f <= 888) | (mv484g > 0 & mv484g <= 888) ~ 1,
TRUE ~ 0),
rc_tobc_other = add_labels(rc_tobc_other, labels = c("No"=0, "Yes"=1)),
rc_tobc_other = set_label(rc_tobc_other, label = "Smokes other type of tobacco"))

my challenge now is if I try run the weighted prevalence using the survey command everything is coming out with frequencies of zero. I am stuck. The survey command is below

svy_design <- svydesign(ids = ~1, strata = ~mv022, weights = ~mv005, data = MRdata)

Define the prevalence function using svytable() to handle missing values

calculate_prevalence <- function(data, variable) {
svytable(as.formula(paste("~", variable)), design = data) / sum(data$weights) * 100
}

List of tobacco variables

tobacco_vars <- c("rc_tobc_cig", "rc_tobc_other", "rc_tobc_smk_any", "rc_tobc_snuffm", "rc_tobc_snuffn", "rc_tobc_chew", "rc_tobv_betel", "rc_tobc_osmkless", "rc_tobc_anysmkless", "rc_tobc_any")

Calculate the overall weighted prevalence for each tobacco variable

prevalence_overall <- lapply(tobacco_vars, function(var) calculate_prevalence(svy_design, var))

Print the results

for (i in seq_along(prevalence_overall)) {
print(paste("Overall Weighted Prevalence for", tobacco_vars[i]))
print(prevalence_overall[[i]])
cat("\n")
}

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.