-
Notifications
You must be signed in to change notification settings - Fork 449
/
nhanes.Rmd
526 lines (400 loc) · 15.6 KB
/
nhanes.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# National Health and Nutrition Examination Survey (NHANES) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <a href="https://github.com/asdfree/nhanes/actions"><img src="https://github.com/asdfree/nhanes/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a>
Doctors and dentists accompany survey interviewers in a [mobile medical center](https://blogs.cdc.gov/nchs/2013/01/30/217/) that travels the country. While survey researchers read the questionnaires, medical professionals administer laboratory tests and conduct a full medical examination. The blood work and in-person check-up allow epidemiologists to answer questions like, "how many people have diabetes but don't know they have diabetes?"
* Many tables containing information from the various examinations, generally one row per respondent.
* A complex sample survey designed to generalize to the civilian non-institutionalized U.S. population.
* Released biennially since 1999-2000.
* Administered by the [Centers for Disease Control and Prevention](http://www.cdc.gov/).
---
Please skim before you begin:
1. [About the National Health and Nutrition Examination Survey](https://www.cdc.gov/nchs/nhanes/about_nhanes.htm)
2. [NHANES Tutorials](https://wwwn.cdc.gov/nchs/nhanes/tutorials/default.aspx)
3. A haiku regarding this microdata:
```{r}
# doctor, dentist, labs
# mobile examination
#vanlife interviews
```
---
## Download, Import, Preparation {-}
Download and import the demographics (demo) and total cholesterol laboratory (tchol) data:
```{r eval = FALSE , results = "hide" }
library(haven)
nhanes_2015_2016_demo_url <- "https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT"
nhanes_2017_2018_demo_url <- "https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/DEMO_J.XPT"
nhanes_2015_2016_tchol_url <- "https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/TCHOL_I.XPT"
nhanes_2017_2018_tchol_url <- "https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/TCHOL_J.XPT"
nhanes_2015_2016_demo_tbl <- read_xpt( nhanes_2015_2016_demo_url )
nhanes_2017_2018_demo_tbl <- read_xpt( nhanes_2017_2018_demo_url )
nhanes_2015_2016_tchol_tbl <- read_xpt( nhanes_2015_2016_tchol_url )
nhanes_2017_2018_tchol_tbl <- read_xpt( nhanes_2017_2018_tchol_url )
nhanes_2015_2016_demo_df <- data.frame( nhanes_2015_2016_demo_tbl )
nhanes_2017_2018_demo_df <- data.frame( nhanes_2017_2018_demo_tbl )
nhanes_2015_2016_tchol_df <- data.frame( nhanes_2015_2016_tchol_tbl )
nhanes_2017_2018_tchol_df <- data.frame( nhanes_2017_2018_tchol_tbl )
```
Specify which variables to keep from both the demo and tchol data files, then stack the four years:
```{r eval = FALSE , results = "hide" }
demo_vars <-
c(
# unique person identifier (merge variable)
"SEQN" ,
# the two-year interviewed + MEC examined weight
"WTMEC2YR" ,
# note that this is a special weight for only
# individuals who took the mobile examination center (MEC) exam
# there is one other weight available - WTINT2YR -
# that should be used when MEC variables are not part of the analysis
# interviewed only or interviewed + MEC
"RIDSTATR" ,
# primary sampling unit varaible, used in complex design
"SDMVPSU" ,
# strata variable, used in complex design
"SDMVSTRA" ,
# race / ethnicity
"RIDRETH3" ,
# age
"RIDAGEYR" ,
# gender
"RIAGENDR" ,
# pregnant at interview
"RIDEXPRG"
)
nhanes_2015_2018_demo_df <-
rbind(
nhanes_2015_2016_demo_df[ , demo_vars ] ,
nhanes_2017_2018_demo_df[ , demo_vars ]
)
tchol_vars <-
c(
# unique person identifier (merge variable)
"SEQN" ,
# laboratory total cholesterol variable
# https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/TCHOL_J.htm
"LBXTC"
)
nhanes_2015_2018_tchol_df <-
rbind(
nhanes_2015_2016_tchol_df[ , tchol_vars ] ,
nhanes_2017_2018_tchol_df[ , tchol_vars ]
)
```
Merge the two pooled datasets, limit the data.frame to mobile examination component respondents:
```{r eval = FALSE , results = "hide" }
nhanes_full_df <-
merge(
nhanes_2015_2018_demo_df ,
nhanes_2015_2018_tchol_df ,
all = TRUE
)
names( nhanes_full_df ) <- tolower( names( nhanes_full_df ) )
nhanes_df <- subset( nhanes_full_df , ridstatr %in% 2 )
```
Scale the mobile examination component two-year weight to generalize to the pooled, four year period:
```{r eval = FALSE , results = "hide" }
nhanes_df[ , 'wtmec4yr' ] <- nhanes_df[ , 'wtmec2yr' ] / 2
```
### Save Locally \ {-}
Save the object at any point:
```{r eval = FALSE , results = "hide" }
# nhanes_fn <- file.path( path.expand( "~" ) , "NHANES" , "this_file.rds" )
# saveRDS( nhanes_df , file = nhanes_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# nhanes_df <- readRDS( nhanes_fn )
```
### Survey Design Definition {-}
Construct a complex sample survey design:
```{r eval = FALSE , results = "hide" }
library(survey)
nhanes_design <-
svydesign(
id = ~ sdmvpsu ,
strata = ~ sdmvstra ,
nest = TRUE ,
weights = ~ wtmec4yr ,
data = nhanes_df
)
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
nhanes_design <-
update(
nhanes_design ,
one = 1 ,
# define high total cholesterol as 1 if mg/dL is at or above 240 and zero otherwise.
hi_tchol = ifelse( lbxtc >= 240 , 1 , 0 ) ,
gender = factor( riagendr , levels = 1:2 , labels = c( 'male' , 'female' ) ) ,
age_categories =
factor(
1 + findInterval( ridageyr , c( 20 , 40 , 60 ) ) ,
levels = 1:4 ,
labels = c( "0-19" , "20-39" , "40-59" , "60+" )
) ,
# recode the ridreth3 variable as:
# mexican american and other hispanic -> 4
# non-hispanic white -> 1
# non-hispanic black -> 2
# non-hispanic asian -> 3
# other race including multi-racial -> 5
race_ethnicity =
factor(
c( 4 , 4 , 1 , 2 , NA , 3 , 5 )[ ridreth3 ] ,
levels = 1:5 ,
labels = c( 'nh white' , 'nh black' , 'nh asian' , 'hispanic' , 'other' )
) ,
pregnant_at_interview =
ifelse( ridexprg %in% 1:2 , as.numeric( ridexprg == 1 ) , NA )
)
```
---
## 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( 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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
svymean( ~ riagendr , nhanes_design )
svyby( ~ riagendr , ~ race_ethnicity , nhanes_design , svymean )
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
svytotal( ~ riagendr , nhanes_design )
svyby( ~ riagendr , ~ race_ethnicity , nhanes_design , svytotal )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ lbxtc , nhanes_design , 0.5 , na.rm = TRUE )
svyby(
~ lbxtc ,
~ race_ethnicity ,
nhanes_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
```
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ lbxtc ,
denominator = ~ ridageyr ,
nhanes_design ,
na.rm = TRUE
)
```
### Subsetting {-}
Restrict the survey design to respondents aged 60 or older:
```{r eval = FALSE , results = "hide" }
sub_nhanes_design <- subset( nhanes_design , age_categories == "60+" )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
degf( nhanes_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ lbxtc , nhanes_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( ~ 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:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ pregnant_at_interview , nhanes_design ,
method = "likelihood" , na.rm = TRUE )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( lbxtc ~ pregnant_at_interview , nhanes_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ pregnant_at_interview + riagendr ,
nhanes_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
svyglm(
lbxtc ~ pregnant_at_interview + riagendr ,
nhanes_design
)
summary( glm_result )
```
---
## Direct Method of Age-Adjustment Replication Example {-}
This example matches the total cholesterol statistics and standard errors in [Table 1](https://www.cdc.gov/nchs/data/databriefs/db363-tables-508.pdf#page=1) from [Data Brief 363](https://www.cdc.gov/nchs/products/databriefs/db363.htm):
Match the crude estimates in the footnote and also in the unadjusted age categories:
```{r eval = FALSE , results = "hide" }
crude_overall <-
svymean( ~ hi_tchol , subset( nhanes_design , ridageyr >= 20 ) , na.rm = TRUE )
stopifnot( round( coef( crude_overall ) , 3 ) == 0.115 )
crude_by_gender <-
svyby(
~ hi_tchol ,
~ gender ,
subset( nhanes_design , ridageyr >= 20 ) ,
svymean ,
na.rm = TRUE
)
stopifnot( round( coef( crude_by_gender )[ 1 ] , 3 ) == 0.103 )
stopifnot( round( coef( crude_by_gender )[ 2 ] , 3 ) == 0.126 )
crude_by_age <-
svyby(
~ hi_tchol ,
~ age_categories ,
subset( nhanes_design , ridageyr >= 20 ) ,
svymean ,
na.rm = TRUE
)
stopifnot( round( coef( crude_by_age )[ 1 ] , 3 ) == 0.075 )
stopifnot( round( coef( crude_by_age )[ 2 ] , 3 ) == 0.157 )
stopifnot( round( coef( crude_by_age )[ 3 ] , 3 ) == 0.114 )
stopifnot( round( SE( crude_by_age )[ 1 ] , 3 ) == 0.005 )
stopifnot( round( SE( crude_by_age )[ 2 ] , 3 ) == 0.011 )
stopifnot( round( SE( crude_by_age )[ 3 ] , 3 ) == 0.008 )
```
Sum up [2000 Census totals](https://seer.cancer.gov/seerstat/tutorials/aarates/step3.html) based on the age groupings specified in footnote:
```{r eval = FALSE , results = "hide" }
pop_by_age <-
data.frame(
age_categories = c( "0-19" , "20-39" , "40-59" , "60+" ) ,
Freq = c( 78782657 , 77670618 , 72816615 , 45363752 )
)
```
Create a design with the nationwide population stratified to the above census counts:
```{r eval = FALSE , results = "hide" }
nhanes_age_adjusted <-
postStratify(
subset( nhanes_design , !is.na( hi_tchol ) ) ,
~ age_categories ,
pop_by_age
)
```
Match the overall adjusted estimates:
```{r eval = FALSE , results = "hide" }
results_overall <-
svymean( ~ hi_tchol , subset( nhanes_age_adjusted , ridageyr >= 20 ) , na.rm = TRUE )
stopifnot( round( coef( results_overall ) , 3 ) == 0.114 )
stopifnot( round( SE( results_overall ) , 3 ) == 0.006 )
```
Create a design stratified to census counts broken out by gender, then match those estimates:
```{r eval = FALSE , results = "hide" }
nhanes_by_gender <-
svystandardize(
nhanes_design ,
by = ~ age_categories , # stratification variable
over = ~ gender , # break out variable
population = pop_by_age , # data.frame containing census populations
excluding.missing = ~ hi_tchol # analysis variable of interest
)
results_by_gender <-
svyby(
~ hi_tchol ,
~ gender ,
subset( nhanes_by_gender , ridageyr >= 20 ) ,
svymean ,
na.rm=TRUE
)
stopifnot( round( coef( results_by_gender )[ 1 ] , 3 ) == 0.105 )
stopifnot( round( coef( results_by_gender )[ 2 ] , 3 ) == 0.121 )
stopifnot( round( SE( results_by_gender )[ 1 ] , 3 ) == 0.007 )
stopifnot( round( SE( results_by_gender )[ 2 ] , 3 ) == 0.008 )
```
Create a design stratified to census counts broken out by race/ethnicity, then match those estimates:
```{r eval = FALSE , results = "hide" }
nhanes_by_race <-
svystandardize(
nhanes_design ,
by = ~ age_categories , # stratification variable
over = ~ race_ethnicity , # break out variable
population = pop_by_age , # data.frame containing census populations
excluding.missing = ~ hi_tchol # analysis variable of interest
)
results_by_race_ethnicity <-
svyby(
~ hi_tchol ,
~ race_ethnicity ,
design = subset( nhanes_by_race , ridageyr >= 20 ) ,
svymean ,
na.rm=TRUE
)
stopifnot( round( coef( results_by_race_ethnicity )[ 1 ] , 3 ) == 0.117 )
stopifnot( round( coef( results_by_race_ethnicity )[ 2 ] , 3 ) == 0.100 )
stopifnot( round( coef( results_by_race_ethnicity )[ 3 ] , 3 ) == 0.116 )
stopifnot( round( coef( results_by_race_ethnicity )[ 4 ] , 3 ) == 0.109 )
stopifnot( round( SE( results_by_race_ethnicity )[ 1 ] , 3 ) == 0.007 )
stopifnot( round( SE( results_by_race_ethnicity )[ 2 ] , 3 ) == 0.009 )
stopifnot( round( SE( results_by_race_ethnicity )[ 3 ] , 3 ) == 0.011 )
stopifnot( round( SE( results_by_race_ethnicity )[ 4 ] , 3 ) == 0.009 )
```
---
## 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 NHANES users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
nhanes_srvyr_design <- as_survey( nhanes_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
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 ) )
```