-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_prep.R
More file actions
252 lines (211 loc) · 5.46 KB
/
Copy pathdata_prep.R
File metadata and controls
252 lines (211 loc) · 5.46 KB
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
library(tidyverse)
library(haven)
# install.packages("pak")
# pak::pak("kyleGrealis/nhanesdata")
library(nhanesdata)
library(janitor)
# 1. Data----
## Statins----
### Module: Prescription medications: RXQ_RX, RXDD_DRUG
rxq_rx <-
read_nhanes("rxq_rx") %>%
clean_names() %>%
filter(year == 2015) %>%
mutate(
statins_use = as.integer(str_detect(rxddrug, regex('atorvastatin|simvastatin|fluvastatin|lovastatin|
pitavastatin|pravastatin|rosuvastatin', ignore_case = T))),
#rxddays_statin = ifelse(statins_use == 1, rxddays, NA_real_)
) %>%
select(year, seqn, statins_use, rxddays) %>%
group_by(year, seqn) %>%
summarise(
statin = as.integer(any(statins_use == 1, na.rm = TRUE)),
statin_days = ifelse(
any(statins_use == 1, na.rm = TRUE),
max(rxddays[statins_use == 1], na.rm = TRUE),
NA_real_
),
.groups = "drop"
)
table(rxq_rx$statin)
### Module: Blood Pressure & Cholesterol Questionnaire BPQ
#### use to filter previous dx of hypercholesterolemia
bpq<- read_nhanes("bpq") %>% # blood pressure and cholesterol
filter(year == 2015) %>%
select(year,seqn,bpq080) %>%
filter(bpq080 == "Yes")
#### final exposure data --> all data have to fit this----
exposure_df<-
bpq %>%
left_join(rxq_rx)
## CVD and mortality----
mcq <- read_nhanes("mcq")
# "mcq160e", # heart attack
# "mcq160f", # stroke
# "mcq160d", # angina
# "mcq160c", # coronary heart disease
# "mcq160b" # congestive heart failure
cvd <- mcq %>%
filter(year == 2015) %>%
select(seqn, year, mcq160e, mcq180e) %>%
mutate(
heart_attack = ifelse(mcq160e == "Yes", 1, 0),
age_first_ha = mcq180e
) %>%
select(year, seqn, heart_attack, age_first_ha)
## Final data exposure + otucome----
final_df<-
exposure_df %>%
left_join(cvd)
## Confounders
### Age, sex, race, Income, edu level
demo_conf <-
read_nhanes("demo") %>%
select(year,seqn,ridageyr,riagendr,ridreth3,dmdeduc2,indfmpir) %>%
filter(year==2015) %>%
mutate(
dmdeduc2 = case_when(
dmdeduc2 == "Less than 9th grade"~ "less_than_9th",
dmdeduc2 == "9-11th grade (Includes 12th grade with no diploma)"~ "9th_to_11th",
dmdeduc2 == "High school graduate/GED or equivalent"~ "high_school",
dmdeduc2 == "Some college or AA degree"~ "some_college",
dmdeduc2 == "College graduate or above"~ "college_or_above",
dmdeduc2 %in% c("Don't know", "Refused")~ NA_character_,
TRUE ~ NA_character_
)
) %>%
rename(
age = ridageyr,
sex = riagendr,
race = ridreth3,
educ = dmdeduc2,
income = indfmpir
)
### diet
diet_conf<-
read_nhanes("dbq") %>%
filter(year == 2015) %>%
select(year,seqn,dbq700) %>%
rename(
diet = dbq700
)
### High blood pressure
bloodpress_conf <-
read_nhanes("bpx") %>%
filter(year==2015) %>%
rename(
systolic1 = bpxsy1,
diastolic1 = bpxdi1,
systolic2 = bpxsy2,
diastolic2 = bpxdi2
) %>%
mutate(
sys_mean = (systolic1+systolic2)/2,
diat_mean =(diastolic1+diastolic2)/2 ,
hypertension = ifelse(sys_mean >= 140 | diat_mean >= 90,1,0)
) %>%
select(year,seqn,hypertension)
### Diabetes
diabetes_conf<-
read_nhanes("ghb") %>%
filter(year == 2015) %>%
mutate(
diabetes_hbg = ifelse(lbxgh >=6.5,1,0)
) %>%
select(
year,seqn,diabetes_hbg
)
### Cholesterol
chol_conf<-
read_nhanes("tchol") %>%
filter(year == 2015) %>%
rename(
total_chol = lbxtc
) %>%
select(
year,seqn,total_chol
)
### IMC
imc_conf<-
read_nhanes("bmx") %>%
select(
year,seqn,bmxbmi,bmxht,bmxwt
) %>%
filter(
year == 2015
)
### Smoking status
smoking_comf <-
read_nhanes("smq") %>% # smq020: >100 cig/lifetime, smq040: current
filter(
year == 2015
) %>%
mutate(
smoking_stat = case_when(
smq020 == "No" ~ 0, # Never
smq020=="Yes" & smq040=="Not at all" ~ 1, # Former
smq020=="Yes" & smq040 %in% c("Every day","Some days") ~ 2, # Current
T ~ NA
)
) %>%
select(year,seqn,smoking_stat)
### Physical act
phys_conf<-
read_nhanes("paq") %>%
filter(year ==2015) %>%
mutate(
physical_act = ifelse(paq650 == "No" & paq665 == "No",0,1)
) %>%
select(year,seqn,physical_act)
### Health insurance
insurance_conf<-
read_nhanes("hiq") %>%
filter(
year == 2015
) %>%
rename(
health_insurace = hiq011
) %>%
select(
year,seqn,health_insurace
)
## Proximity health care
proximity_conf <-
read_nhanes("huq") %>%
filter(year == 2015) %>%
mutate(
huq030 = ifelse(huq030 == "There is more than one place" | huq030 == "Yes","Yes","No")
) %>%
select(
year,seqn,huq030
)
# Final data ----
df_work<-
final_df %>%
left_join(demo_conf) %>%
left_join(bloodpress_conf) %>%
left_join(diabetes_conf) %>%
left_join(chol_conf) %>%
left_join(imc_conf) %>%
left_join(smoking_comf) %>%
left_join(phys_conf) %>%
left_join(insurance_conf) %>%
left_join(proximity_conf) %>%
left_join(diet_conf) %>%
mutate(
statin_start = age-(statin_days/365),
heart_attack_incident = case_when(
statin == 1 ~ as.integer(heart_attack == 1 & age_first_ha > statin_start),
statin == 0 ~ heart_attack,
TRUE ~ NA_integer_
)
) %>%
filter(
!health_insurace %in% c("Don't know","Refused"),
diet != "Don't know"
) %>%
mutate(
statin = as.factor(statin),
educ = as.numeric(factor(educ))
)
write.csv(df_work,"./data/df_work.csv",row.names = F)