-
Notifications
You must be signed in to change notification settings - Fork 449
/
hrs.Rmd
358 lines (259 loc) · 10.8 KB
/
hrs.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Health and Retirement Study (HRS) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <img src='https://img.shields.io/badge/Tested%20Locally-Windows%20Laptop-brightgreen' alt='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?"
* Many tables from different timepoints, most with one row per sampled respondent and linkable.
* A complex sample survey designed to generalize to Americans aged 50+ at each interview point.
* Released biennially since 1992.
* Administered by the [University of Michigan's Institute for Social Research](http://isr.umich.edu/) with data management by the [RAND Corporation](http://www.rand.org/) and cross-national harmonization by the [University of Southern California](https://g2aging.org/). Funded by the [National Institute on Aging](https://www.nia.nih.gov/) and the [Social Security Administration](https://www.ssa.gov/).
---
Please skim before you begin:
1. [Getting Started with the Health and Retirement Study](https://hrsonline.isr.umich.edu/sitedocs/dmgt/IntroUserGuide.pdf)
2. [RAND HRS Longitudinal File 2020 (V1) Documentation](https://www.rand.org/content/dam/rand/www/external/labor/aging/dataprod/randhrs1992_2020v1.pdf)
3. A haiku regarding this microdata:
```{r}
# 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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
# hrs_fn <- file.path( path.expand( "~" ) , "HRS" , "this_file.rds" )
# saveRDS( hrs_df , file = hrs_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# 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):
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ h4ahous ,
denominator = ~ h15ahous ,
hrs_design ,
na.rm = TRUE
)
```
### Subsetting {-}
Restrict the survey design to :
```{r eval = FALSE , results = "hide" }
sub_hrs_design <- subset( hrs_design , working_in_1996 == 1 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
degf( hrs_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ h15ahous , hrs_design , na.rm = TRUE )
```
Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# 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:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ working_in_2020 , hrs_design ,
method = "likelihood" , na.rm = TRUE )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( h15ahous ~ working_in_2020 , hrs_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ working_in_2020 + marital_stat_2020 ,
hrs_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
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](https://youtu.be/D409oUPXWUU?t=3201):
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
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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](https://github.com/tidyverse/dplyr/)-like syntax. [srvyr](https://github.com/gergness/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](https://cran.r-project.org/web/packages/srvyr/vignettes/srvyr-vs-survey.html) details the available features. As a starting point for HRS users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
hrs_srvyr_design <- as_survey( hrs_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
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 ) )
```