Health and Retirement Study (HRS)

Build Status Build status

The Health and Retirement Study interviews Americans aged 50+ for their entire life. Allows for findings like, “Among Americans who were 50-74 years old in 1998, X% lived in nursing homes by 2010.”

Simplified Download and Importation

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

lodown also provides a catalog of available microdata extracts with the get_catalog() function. After requesting the HRS 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 HRS microdata files
hrs_cat <-
    get_catalog( "hrs" ,
        output_dir = file.path( path.expand( "~" ) , "HRS" ) , 
        your_username = "username" , 
        your_password = "password" )

# RAND consolidated file only
hrs_cat <- subset( hrs_cat , grepl( 'rand([a-z]+)stata\\.zip' , file_name ) )
# download the microdata to your local computer
hrs_cat <- lodown( "hrs" , hrs_cat , 
    your_username = "username" , 
    your_password = "password" )

Analysis Examples with the survey library  

Construct a complex sample survey design:

library(survey)

hrs_df <- 
    readRDS( 
        grep( 
            'rand([a-z]+)stata(.*)rds$' , 
            list.files( 
                hrs_cat$output_folder , 
                full.names = TRUE ,
                recursive = TRUE
            ) ,
            value = TRUE 
        ) 
    )
    
# RAM cleanup
keep_vars <- 
    c( "raehsamp" , "raestrat" , "r3wtresp" , 
        "r3work" , "r12work" , "h12ahous" ,
        "r3mstat" , "r12mstat" , "h4ahous" )

hrs_df <- hrs_df[ keep_vars ]
    
# community residents aged 50+ in 1996
hrs_design <- 
    svydesign(
        id = ~ raehsamp ,
        strata = ~ raestrat ,
        weights = ~ r3wtresp , 
        nest = TRUE ,
        data = subset( hrs_df , r3wtresp > 0 )
    )

Variable Recoding

Add new columns to the data set:

hrs_design <- 
    update( 
        hrs_design , 

        one = 1 ,
        
        working_in_1996 = r3work ,

        working_in_2014 = r12work ,

        marital_stat_1996 =
            factor( r3mstat , levels = 1:8 , labels =
                c( "Married" , "Married, spouse absent" ,
                "Partnered" , "Separated" , "Divorced" ,
                "Separated/divorced" , "Widowed" ,
                "Never married" ) ) ,
                
        marital_stat_2014 =
            factor( r12mstat , levels = 1:8 , labels =
                c( "Married" , "Married, spouse absent" ,
                "Partnered" , "Separated" , "Divorced" ,
                "Separated/divorced" , "Widowed" ,
                "Never married" ) )
    )

Unweighted Counts

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

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

svyby( ~ one , ~ marital_stat_1996 , hrs_design , unwtd.count )

Weighted Counts

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

svytotal( ~ one , hrs_design )

svyby( ~ one , ~ marital_stat_1996 , hrs_design , svytotal )

Descriptive Statistics

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

svymean( ~ h12ahous , hrs_design , na.rm = TRUE )

svyby( ~ h12ahous , ~ marital_stat_1996 , hrs_design , svymean , na.rm = TRUE )

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

svymean( ~ marital_stat_2014 , hrs_design , na.rm = TRUE )

svyby( ~ marital_stat_2014 , ~ marital_stat_1996 , hrs_design , svymean , na.rm = TRUE )

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

svytotal( ~ h12ahous , hrs_design , na.rm = TRUE )

svyby( ~ h12ahous , ~ marital_stat_1996 , hrs_design , svytotal , na.rm = TRUE )

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

svytotal( ~ marital_stat_2014 , hrs_design , na.rm = TRUE )

svyby( ~ marital_stat_2014 , ~ marital_stat_1996 , hrs_design , svytotal , na.rm = TRUE )

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

svyquantile( ~ h12ahous , hrs_design , 0.5 , na.rm = TRUE )

svyby( 
    ~ h12ahous , 
    ~ marital_stat_1996 , 
    hrs_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE ,
    keep.var = TRUE ,
    na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ h4ahous , 
    denominator = ~ h12ahous , 
    hrs_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to :

sub_hrs_design <- subset( hrs_design , working_in_1996 == 1 )

Calculate the mean (average) of this subset:

svymean( ~ h12ahous , sub_hrs_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( ~ h12ahous , hrs_design , na.rm = TRUE )

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

grouped_result <-
    svyby( 
        ~ h12ahous , 
        ~ marital_stat_1996 , 
        hrs_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( hrs_design )

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

svyvar( ~ h12ahous , hrs_design , na.rm = TRUE )

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

# SRS without replacement
svymean( ~ h12ahous , hrs_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ h12ahous , hrs_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( ~ working_in_2014 , hrs_design ,
    method = "likelihood" , na.rm = TRUE )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( h12ahous ~ working_in_2014 , hrs_design )

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

svychisq( 
    ~ working_in_2014 + marital_stat_2014 , 
    hrs_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        h12ahous ~ working_in_2014 + marital_stat_2014 , 
        hrs_design 
    )

summary( glm_result )

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

library(srvyr)
hrs_srvyr_design <- as_survey( hrs_design )

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

hrs_srvyr_design %>%
    summarize( mean = survey_mean( h12ahous , na.rm = TRUE ) )

hrs_srvyr_design %>%
    group_by( marital_stat_1996 ) %>%
    summarize( mean = survey_mean( h12ahous , na.rm = TRUE ) )

Replication Example