Health and Retirement Study (HRS)

Local Testing Badge

This detailed longitudinal study of the elderly in the United States allows for findings such as, “Among community residents aged 55-64 years old in 1998, what share lived in nursing homes by 2020?”


Please skim before you begin:

  1. Getting Started with the Health and Retirement Study

  2. RAND HRS Longitudinal File 2020 (V1) Documentation

  3. This human-composed haiku or a bouquet of artificial intelligence-generated limericks

# sankey diagram
# comes alive at fifty five
# till death? you respond

Download, Import, Preparation

  1. Register at the HRS Data Portal at https://hrsdata.isr.umich.edu/user/register.

  2. Choose RAND HRS Longitudinal File 2020 Latest release: Mar 2023 (V1).

  3. Download the STATA dataset randhrs1992_2020v1_STATA.zip dated 04/05/2023:

library(haven)

hrs_fn <- file.path( path.expand( "~" ) , "randhrs1992_2020v1.dta" )

hrs_tbl <- read_dta( hrs_fn )

hrs_df <- data.frame( hrs_tbl )

names( hrs_df ) <- tolower( names( hrs_df ) )

Save locally  

Save the object at any point:

# hrs_fn <- file.path( path.expand( "~" ) , "HRS" , "this_file.rds" )
# saveRDS( hrs_df , file = hrs_fn , compress = FALSE )

Load the same object:

# hrs_df <- readRDS( hrs_fn )

Survey Design Definition

Construct a complex sample survey design:

This design generalizes to residents of the United States that were living in the community in 1996 (wave 3) and also still alive (and participating in the survey) as of 2020 (wave 15):

library(survey)

hrs_design <- 
    svydesign(
        id = ~ raehsamp ,
        strata = ~ raestrat ,
        weights = ~ r3wtresp , 
        nest = TRUE ,
        data = subset( hrs_df , r3wtresp > 0 & inw15 == 1 )
    )

Variable Recoding

Add new columns to the data set:

hrs_design <- 
    update( 
        hrs_design , 

        one = 1 ,
        
        working_in_1996 = r3work ,

        working_in_2020 = r15work ,

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

Analysis Examples with the survey library  

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

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

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

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

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

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

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

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

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

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

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

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

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

svyby( 
    ~ h15ahous , 
    ~ marital_stat_1996 , 
    hrs_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE , na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ h4ahous , 
    denominator = ~ h15ahous , 
    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( ~ h15ahous , 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( ~ h15ahous , hrs_design , na.rm = TRUE )

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

grouped_result <-
    svyby( 
        ~ h15ahous , 
        ~ 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( ~ h15ahous , hrs_design , na.rm = TRUE )

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

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

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

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( h15ahous ~ working_in_2020 , hrs_design )

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

svychisq( 
    ~ working_in_2020 + marital_stat_2020 , 
    hrs_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        h15ahous ~ working_in_2020 + marital_stat_2020 , 
        hrs_design 
    )

summary( glm_result )

Replication Example

This example matches statistics and confidence intervals to four digits from the Gateway to Global Aging’s An Introduction to HRS, RAND HRS Longitudinal File, and Harmonized HRS:

  1. Navigate to Contributed Projects at https://hrsdata.isr.umich.edu/data-products/contributed-projects.

  2. Choose Gateway Harmonized HRS Latest release: Aug 2023 Version D

  3. Download the STATA dataset H_HRS_d_stata.zip dated 09/12/2023

harmonized_hrs_fn <- file.path( path.expand( "~" ) , "H_HRS_d.dta" )

harmonized_hrs_tbl <- read_dta( harmonized_hrs_fn )

harmonized_hrs_df <- data.frame( harmonized_hrs_tbl )

names( harmonized_hrs_df ) <- tolower( names( harmonized_hrs_df ) )

Merge on cluster and strata variables from the RAND HRS Longitudinal file:

harmonized_hrs_rand_df <-
    merge(
        harmonized_hrs_df ,
        hrs_df[ c( 'hhid' , 'pn' , 'raestrat' , 'raehsamp' ) ] ,
        by = c( 'hhid' , 'pn' )
    )

stopifnot( nrow( harmonized_hrs_rand_df ) == nrow( hrs_df ) )

Limit the survey design to respondents answering at least two of the five different life satisfaction questions in the 2014 (wave 12) psychosocial leave-behind survey:

h12sc_df <- subset( harmonized_hrs_rand_df , r12scwtresp > 0 & inw12sc == 1 )

r12sc_design <-
    svydesign(
        ~ raehsamp ,
        strata = ~ raestrat ,
        data = h12sc_df ,
        weights = ~ r12scwtresp ,
        nest = TRUE
    )

Reproduce the coefficient, standard error, and confidence intervals presented at 53:20 of the tutorial:

result <- svymean( ~ r12lsatsc , r12sc_design , na.rm = TRUE )

stopifnot( round( coef( result ) , 4 ) == 4.9822 )
stopifnot( round( SE( result ) , 4 ) == 0.0226 )
stopifnot( round( confint( result , df = degf( r12sc_design ) ) , 4 ) == c( 4.9369 , 5.0276 ) )

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

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