How to weight data using R for dhs domestic violence data (Cote d'Ivoire 2011-2012)

Viewed 132

I would like to set up the survey design for Cote d'Ivoire DHS using R (2011-2012). I am focused on domestic violence.

I used this code:

wt <- ipv_studyf$d005/1000000 # creating the variable weight

ipvdesign <- svydesign(ids = ipv_studyf$v021,      # clustering- psu
                        strata = ipv_studyf$v023,   # strata variable
                        weights = wt,               # the weighting variable
                        data = ipv_studyf,          # the dataset
                        variance="HT" )             # Horvitz-Thompson estimator

However, I keep getting this error message:

**Error in na.weight(data.frame(weights)) : missing values in `weights'**

If anyone has any insights on how to set up the dataset in R and help me fix this error message, I would appreciate the help!

1 Answers

We can create a logical vector for NA, subset the data and apply the function

 i1 <- !is.na(wt)
 svydesign(ids = ipv_studyf$v021[i1], 
           strata = ipv_studyf$v023[i1], 
           weights = wt[i1], 
           data = ipv_studyf, variance = "HT") 
Related