This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from ajdamico/asdfree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-addhealth.Rmd
307 lines (229 loc) · 9.64 KB
/
08-addhealth.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
# National Longitudinal Study of Adolescent to Adult Health (ADDHEALTH) {-}
[![Build Status](https://travis-ci.org/asdfree/addhealth.svg?branch=master)](https://travis-ci.org/asdfree/addhealth) [![Build status](https://ci.appveyor.com/api/projects/status/github/asdfree/addhealth?svg=TRUE)](https://ci.appveyor.com/project/ajdamico/addhealth)
The National Longitudinal Study of Adolescent to Adult Health follows a cohort of teenagers from the 1990s into adulthood.
* Many tables, most with one row per sampled youth respondent.
* A complex sample survey designed to generalize to adolescents in grades 7-12 in the United States during the 1994-95 school year.
* Released at irregular intervals, with 1994-1995, 1996, 2001-2002, and 2008-2009 available and 2016-2018 forthcoming.
* Administered by the [Carolina Population Center](http://www.cpc.unc.edu/) and funded by [a consortium](http://www.cpc.unc.edu/projects/addhealth/about/funders).
## Simplified Download and Importation {-}
The R `lodown` package easily downloads and imports all available ADDHEALTH microdata by simply specifying `"addhealth"` 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.
```{r eval = FALSE }
library(lodown)
lodown( "addhealth" , output_dir = file.path( path.expand( "~" ) , "ADDHEALTH" ) ,
your_email = "[email protected]" ,
your_password = "password" )
```
`lodown` also provides a catalog of available microdata extracts with the `get_catalog()` function. After requesting the ADDHEALTH catalog, you could pass a subsetted catalog through the `lodown()` function in order to download and import specific extracts (rather than all available extracts).
```{r eval = FALSE , results = "hide" }
library(lodown)
# examine all available ADDHEALTH microdata files
addhealth_cat <-
get_catalog( "addhealth" ,
output_dir = file.path( path.expand( "~" ) , "ADDHEALTH" ) ,
your_email = "[email protected]" ,
your_password = "password" )
# wave i only
addhealth_cat <- subset( addhealth_cat , wave == "wave i" )
# download the microdata to your local computer
addhealth_cat <- lodown( "addhealth" , addhealth_cat ,
your_email = "[email protected]" ,
your_password = "password" )
```
## Analysis Examples with the `survey` library \ {-}
Construct a complex sample survey design:
```{r eval = FALSE }
```
```{r eval = FALSE }
options( survey.lonely.psu = "adjust" )
library(survey)
addhealth_df <-
readRDS(
file.path( path.expand( "~" ) , "ADDHEALTH" ,
"wave i consolidated.rds" )
)
addhealth_design <-
svydesign(
id = ~cluster2 ,
data = addhealth_df ,
weights = ~ gswgt1 ,
nest = TRUE
)
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE }
addhealth_design <-
update(
addhealth_design ,
one = 1 ,
male = as.numeric( as.numeric( bio_sex ) == 1 ) ,
hours_of_computer_games = ifelse( h1da10 > 99 , NA , h1da10 ) ,
hours_of_television = ifelse( h1da8 > 99 , NA , h1da8 )
)
```
### Unweighted Counts {-}
Count the unweighted number of records in the survey sample, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( weights( addhealth_design , "sampling" ) != 0 )
svyby( ~ one , ~ h1gh25 , addhealth_design , unwtd.count )
```
### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , addhealth_design )
svyby( ~ one , ~ h1gh25 , addhealth_design , svytotal )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ hours_of_computer_games , addhealth_design , na.rm = TRUE )
svyby( ~ hours_of_computer_games , ~ h1gh25 , addhealth_design , svymean , na.rm = TRUE )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ h1gh24 , addhealth_design , na.rm = TRUE )
svyby( ~ h1gh24 , ~ h1gh25 , addhealth_design , svymean , na.rm = TRUE )
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ hours_of_computer_games , addhealth_design , na.rm = TRUE )
svyby( ~ hours_of_computer_games , ~ h1gh25 , addhealth_design , svytotal , na.rm = TRUE )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ h1gh24 , addhealth_design , na.rm = TRUE )
svyby( ~ h1gh24 , ~ h1gh25 , addhealth_design , svytotal , na.rm = TRUE )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ hours_of_computer_games , addhealth_design , 0.5 , na.rm = TRUE )
svyby(
~ hours_of_computer_games ,
~ h1gh25 ,
addhealth_design ,
svyquantile ,
0.5 ,
ci = TRUE ,
keep.var = TRUE ,
na.rm = TRUE
)
```
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ hours_of_computer_games ,
denominator = ~ hours_of_television ,
addhealth_design ,
na.rm = TRUE
)
```
### Subsetting {-}
Restrict the survey design to self-reported fair or poor health:
```{r eval = FALSE , results = "hide" }
sub_addhealth_design <- subset( addhealth_design , as.numeric( h1gh1 ) %in% c( 4 , 5 ) )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
svymean( ~ hours_of_computer_games , sub_addhealth_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( ~ hours_of_computer_games , addhealth_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ hours_of_computer_games ,
~ h1gh25 ,
addhealth_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( addhealth_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ hours_of_computer_games , addhealth_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( ~ hours_of_computer_games , addhealth_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ hours_of_computer_games , addhealth_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( ~ male , addhealth_design ,
method = "likelihood" )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( hours_of_computer_games ~ male , addhealth_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ male + h1gh24 ,
addhealth_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
svyglm(
hours_of_computer_games ~ male + h1gh24 ,
addhealth_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](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 ADDHEALTH users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
addhealth_srvyr_design <- as_survey( addhealth_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
addhealth_srvyr_design %>%
summarize( mean = survey_mean( hours_of_computer_games , na.rm = TRUE ) )
addhealth_srvyr_design %>%
group_by( h1gh25 ) %>%
summarize( mean = survey_mean( hours_of_computer_games , na.rm = TRUE ) )
```
---
## Replication Example {-}
A member of the AddHealth staff at UNC ran the mean hours of television using the wave one public use file with stata to confirm that the calculations presented on this page follow the correct methodology.
svyset cluster2 [pweight=gswgt1]
svy: mean w1hrtv
Survey: Mean estimation
Number of strata = 1 Number of obs = 6477
Number of PSUs = 132 Population size = 22159516
Design df = 131
--------------------------------------------------------------
| Linearized
| Mean Std. Err. [95% Conf. Interval]
-------------+------------------------------------------------
w1hrtv | 15.64193 .3902066 14.87 16.41385
--------------------------------------------------------------
```{r eval = FALSE , results = "hide" }
result <-
svymean(
~ hours_of_television ,
addhealth_design ,
na.rm = TRUE
)
stopifnot( round( coef( result ) , 3 ) == 15.642 )
stopifnot( round( SE( result ) , 4 ) == 0.3902 )
```