Demographic and Health Surveys (DHS)

Build Status Build status

The Demographic and Health Surveys collect data on population, health, HIV, and nutrition in over 90 countries.

  • Many tables, often with one row per male, per female, or per responding household.

  • A complex sample survey designed to generalize to the residents of various countries.

  • Many releases for different countries annually, since 1984.

  • Administered by the ICF International and funded by the US Agency for International Development.

Simplified Download and Importation

The R lodown package easily downloads and imports all available DHS microdata by simply specifying "dhs" with an output_dir = parameter in the lodown() function. Depending on your internet connection and computer processing speed, you might prefer to run this step overnight.

library(lodown)
lodown( "dhs" , output_dir = file.path( path.expand( "~" ) , "DHS" ) , 
    your_email = "email@address.com" , 
    your_password = "password" , 
    your_project = "project" )

lodown also provides a catalog of available microdata extracts with the get_catalog() function. After requesting the DHS catalog, you could pass a subsetted catalog through the lodown() function in order to download and import specific extracts (rather than all available extracts).

library(lodown)
# examine all available DHS microdata files
dhs_cat <-
    get_catalog( "dhs" ,
        output_dir = file.path( path.expand( "~" ) , "DHS" ) , 
        your_email = "email@address.com" , 
        your_password = "password" , 
        your_project = "project" )

# Malawi only
dhs_cat <- subset( dhs_cat , country == 'Malawi' )
# download the microdata to your local computer
lodown( "dhs" , dhs_cat , 
    your_email = "email@address.com" , 
    your_password = "password" , 
    your_project = "project" )

Analysis Examples with the survey library

Construct a complex sample survey design:

library(survey)

dhs_df <- 
    readRDS( 
        file.path( path.expand( "~" ) , "DHS" , 
        "Malawi/Standard DHS 2004/MWIR4EDT.rds" ) 
    )

# convert the weight column to a numeric type
dhs_df$weight <- as.numeric( dhs_df$v005 )

# paste the `sdist` and `v025` columns together
# into a single strata variable
dhs_df$strata <- do.call( paste , dhs_df[ , c( 'sdist' , 'v025' ) ] )
# as shown at
# http://userforum.dhsprogram.com/index.php?t=rview&goto=2154#msg_2154

dhs_design <- 
    svydesign( 
        ~ v021 , 
        strata = ~strata , 
        data = dhs_df , 
        weights = ~weight
    )

Variable Recoding

Add new columns to the data set:

dhs_design <- 
    update( 
        dhs_design , 
        
        one = 1 ,
        
        total_children_ever_born = v201 ,
        
        surviving_children = v201 - v206 - v207 ,
        
        urban_rural = factor( v025 , labels = c( 'urban' , 'rural' ) ) ,
        
        ethnicity =
            factor( v131 , levels = c( 1:8 , 96 ) , labels =
                c( "Chewa" , "Tumbuka" , "Lomwe" , "Tonga" , 
                "Yao" , "Sena" , "Nkonde" , "Ngoni" , "Other" ) ) ,
                
        no_formal_education = as.numeric( v149 == 0 )
        
    )

Unweighted Counts

Count the unweighted number of records in the survey sample, overall and by groups:

sum( weights( dhs_design , "sampling" ) != 0 )

svyby( ~ one , ~ urban_rural , dhs_design , unwtd.count )

Weighted Counts

Count the weighted size of the generalizable population, overall and by groups:

svytotal( ~ one , dhs_design )

svyby( ~ one , ~ urban_rural , dhs_design , svytotal )

Descriptive Statistics

Calculate the mean (average) of a linear variable, overall and by groups:

svymean( ~ surviving_children , dhs_design )

svyby( ~ surviving_children , ~ urban_rural , dhs_design , svymean )

Calculate the distribution of a categorical variable, overall and by groups:

svymean( ~ ethnicity , dhs_design , na.rm = TRUE )

svyby( ~ ethnicity , ~ urban_rural , dhs_design , svymean , na.rm = TRUE )

Calculate the sum of a linear variable, overall and by groups:

svytotal( ~ surviving_children , dhs_design )

svyby( ~ surviving_children , ~ urban_rural , dhs_design , svytotal )

Calculate the weighted sum of a categorical variable, overall and by groups:

svytotal( ~ ethnicity , dhs_design , na.rm = TRUE )

svyby( ~ ethnicity , ~ urban_rural , dhs_design , svytotal , na.rm = TRUE )

Calculate the median (50th percentile) of a linear variable, overall and by groups:

svyquantile( ~ surviving_children , dhs_design , 0.5 )

svyby( 
    ~ surviving_children , 
    ~ urban_rural , 
    dhs_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE ,
    keep.var = TRUE 
)

Estimate a ratio:

svyratio( 
    numerator = ~ surviving_children , 
    denominator = ~ total_children_ever_born , 
    dhs_design 
)

Subsetting

Restrict the survey design to 40-49 year old females only:

sub_dhs_design <- subset( dhs_design , v447a %in% 40:49 )

Calculate the mean (average) of this subset:

svymean( ~ surviving_children , sub_dhs_design )

Measures of Uncertainty

Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:

this_result <- svymean( ~ surviving_children , dhs_design )

coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )

grouped_result <-
    svyby( 
        ~ surviving_children , 
        ~ urban_rural , 
        dhs_design , 
        svymean 
    )
    
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )

Calculate the degrees of freedom of any survey design object:

degf( dhs_design )

Calculate the complex sample survey-adjusted variance of any statistic:

svyvar( ~ surviving_children , dhs_design )

Include the complex sample design effect in the result for a specific statistic:

# SRS without replacement
svymean( ~ surviving_children , dhs_design , deff = TRUE )

# SRS with replacement
svymean( ~ surviving_children , dhs_design , deff = "replace" )

Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See ?svyciprop for alternatives:

svyciprop( ~ no_formal_education , dhs_design ,
    method = "likelihood" )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( surviving_children ~ no_formal_education , dhs_design )

Perform a chi-squared test of association for survey data:

svychisq( 
    ~ no_formal_education + ethnicity , 
    dhs_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        surviving_children ~ no_formal_education + ethnicity , 
        dhs_design 
    )

summary( glm_result )

0.17 Analysis Examples with srvyr

The R srvyr library calculates summary statistics from survey data, such as the mean, total or quantile using dplyr-like syntax. srvyr allows for the use of many verbs, such as summarize, group_by, and mutate, the convenience of pipe-able functions, the tidyverse style of non-standard evaluation and more consistent return types than the survey package. This vignette details the available features. As a starting point for DHS users, this code replicates previously-presented examples:

library(srvyr)
dhs_srvyr_design <- as_survey( dhs_design )

Calculate the mean (average) of a linear variable, overall and by groups:

dhs_srvyr_design %>%
    summarize( mean = survey_mean( surviving_children ) )

dhs_srvyr_design %>%
    group_by( urban_rural ) %>%
    summarize( mean = survey_mean( surviving_children ) )

Replication Example