National Health and Nutrition Examination Survey (NHANES)

Build Status Build status

The National Health and Nutrition Examination Survey (NHANES) is this fascinating survey where doctors and dentists accompany survey interviewers in a little mobile medical center that drives around the country. While the survey methodologists interview people, the medical professionals administer laboratory tests and conduct a thorough physical examination. The blood work and medical exam allow researchers to answer tough questions like, “how many people have diabetes but don’t know they have diabetes?”

  • Many tables containing information gathered from the various examinations, generally with one row per individual respondent.

  • A complex sample survey designed to generalize to the civilian non-institutionalized population of the United States.

  • Released biennially since 1999-2000.

  • Administered by the Centers for Disease Control and Prevention.

Simplified Download and Importation

The R lodown package easily downloads and imports all available NHANES microdata by simply specifying "nhanes" 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( "nhanes" , output_dir = file.path( path.expand( "~" ) , "NHANES" ) )

lodown also provides a catalog of available microdata extracts with the get_catalog() function. After requesting the NHANES 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 NHANES microdata files
nhanes_cat <-
    get_catalog( "nhanes" ,
        output_dir = file.path( path.expand( "~" ) , "NHANES" ) )

# 2015-2016 only
nhanes_cat <- subset( nhanes_cat , years == "2015-2016" )
# download the microdata to your local computer
lodown( "nhanes" , nhanes_cat )

Analysis Examples with the survey library

Construct a complex sample survey design:

options( survey.lonely.psu = "adjust" )

library(survey)

nhanes_demo_df <- 
    readRDS( file.path( path.expand( "~" ) , "NHANES" , "2015-2016/demo_i.rds" ) )

nhanes_tchol_df <- 
    readRDS( file.path( path.expand( "~" ) , "NHANES" , "2015-2016/tchol_i.rds" ) )

nhanes_df <- merge( nhanes_demo_df , nhanes_tchol_df , all = TRUE )

stopifnot( nrow( nhanes_df ) == nrow( nhanes_demo_df ) )

# keep only individuals who took the "mobile examination center" component
nhanes_df <- subset( nhanes_df , ridstatr %in% 2 )

nhanes_design <- 
    svydesign(
        id = ~sdmvpsu , 
        strata = ~sdmvstra ,
        nest = TRUE ,
        weights = ~wtmec2yr ,
        data = nhanes_df
    )

Variable Recoding

Add new columns to the data set:

nhanes_design <- 
    update( 
        nhanes_design , 
        
        one = 1 ,
        
        pregnant_at_interview = 
            ifelse( ridexprg %in% 1:2 , as.numeric( ridexprg == 1 ) , NA ) ,
        
        race_ethnicity = 
            factor( 
                c( 3 , 3 , 1 , 2 , 4 )[ ridreth1 ] ,
                levels = 1:4 , 
                labels = 
                    c( 'non-hispanic white' , 'non-hispanic black' , 
                        'hispanic' , 'other' )
            ) ,
        
        age_category =
            factor(
                findInterval( ridageyr , c( 20 , 40 , 60 ) ) ,
                labels = c( "0-19" , "20-39" , "40-59" , "60+" )
            )
    )

Unweighted Counts

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

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

svyby( ~ one , ~ race_ethnicity , nhanes_design , unwtd.count )

Weighted Counts

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

svytotal( ~ one , nhanes_design )

svyby( ~ one , ~ race_ethnicity , nhanes_design , svytotal )

Descriptive Statistics

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

svymean( ~ lbxtc , nhanes_design , na.rm = TRUE )

svyby( ~ lbxtc , ~ race_ethnicity , nhanes_design , svymean , na.rm = TRUE )

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

svymean( ~ riagendr , nhanes_design )

svyby( ~ riagendr , ~ race_ethnicity , nhanes_design , svymean )

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

svytotal( ~ lbxtc , nhanes_design , na.rm = TRUE )

svyby( ~ lbxtc , ~ race_ethnicity , nhanes_design , svytotal , na.rm = TRUE )

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

svytotal( ~ riagendr , nhanes_design )

svyby( ~ riagendr , ~ race_ethnicity , nhanes_design , svytotal )

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

svyquantile( ~ lbxtc , nhanes_design , 0.5 , na.rm = TRUE )

svyby( 
    ~ lbxtc , 
    ~ race_ethnicity , 
    nhanes_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE ,
    keep.var = TRUE ,
    na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ lbxtc , 
    denominator = ~ ridageyr , 
    nhanes_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to respondents aged 60 or older:

sub_nhanes_design <- subset( nhanes_design , age_category == "60+" )

Calculate the mean (average) of this subset:

svymean( ~ lbxtc , sub_nhanes_design , na.rm = TRUE )

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( ~ lbxtc , nhanes_design , na.rm = TRUE )

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

grouped_result <-
    svyby( 
        ~ lbxtc , 
        ~ race_ethnicity , 
        nhanes_design , 
        svymean ,
        na.rm = TRUE 
    )
    
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )

Calculate the degrees of freedom of any survey design object:

degf( nhanes_design )

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

svyvar( ~ lbxtc , nhanes_design , na.rm = TRUE )

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

# SRS without replacement
svymean( ~ lbxtc , nhanes_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ lbxtc , nhanes_design , na.rm = TRUE , deff = "replace" )

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

svyciprop( ~ pregnant_at_interview , nhanes_design ,
    method = "likelihood" , na.rm = TRUE )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( lbxtc ~ pregnant_at_interview , nhanes_design )

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

svychisq( 
    ~ pregnant_at_interview + riagendr , 
    nhanes_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        lbxtc ~ pregnant_at_interview + riagendr , 
        nhanes_design 
    )

summary( glm_result )

0.33 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 NHANES users, this code replicates previously-presented examples:

library(srvyr)
nhanes_srvyr_design <- as_survey( nhanes_design )

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

nhanes_srvyr_design %>%
    summarize( mean = survey_mean( lbxtc , na.rm = TRUE ) )

nhanes_srvyr_design %>%
    group_by( race_ethnicity ) %>%
    summarize( mean = survey_mean( lbxtc , na.rm = TRUE ) )

Replication Example