-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNHANES_PRA on DM.R
336 lines (285 loc) · 12.3 KB
/
NHANES_PRA on DM.R
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
# BSTT 413 FINAL PROJECT #
###########################
# Download NHANES Dataset #
###########################
library(tidyverse) # data management (dplyr) and plots (ggplot2)
library(foreign) # reading xport (generated by SAS) files
require(dplyr)
# 1.1 Diagnosed diabetes
diabfile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/DIQ_J.XPT",
destfile = diabfile,
mode = "wb") # use 'wb' to read as binary file
diabdf = read.xport(diabfile)
# using foreign package (file must be binary format) to read the export into R dataframe
# 1.2 Undiagnosed diabetes (A1c blood sugar lab data)
a1cfile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/GHB_J.XPT",
destfile = a1cfile,
mode = "wb")
a1cdf = read.xport(a1cfile)
# 2. Demographics - demo and weighting variables
demofile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/DEMO_J.XPT",
destfile = demofile,
mode = "wb")
demodf = read.xport(demofile)
# 3. Health Insurance
insurfile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/HIQ_J.XPT",
destfile = insurfile,
mode = "wb")
insurdf = read.xport(insurfile)
# 4. Physical Activity (Adults Section)
pafile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/PAQ_J.XPT",
destfile = pafile,
mode = "wb")
padf = read.xport(pafile)
# 5. Diet
dietfile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/DBQ_J.XPT",
destfile = dietfile,
mode = "wb")
dietdf = read.xport(dietfile)
# 6. BMI
bmifile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/BMX_J.XPT",
destfile = bmifile,
mode = "wb")
bmidf = read.xport(bmifile)
# 7. Blood Pressure
bpfile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/BPX_J.XPT",
destfile = bpfile,
mode = "wb")
bpdf = read.xport(bpfile)
# 8. Mental health
mhfile = tempfile()
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/DPQ_J.XPT",
destfile = mhfile,
mode = 'wb')
mhdf = read.xport(mhfile)
##############################
# Merging and Transformation #
##############################
nhanesdat = full_join(diabdf, a1cdf, by="SEQN") %>%
select(SEQN,DIQ010,LBXGH) %>%
filter(DIQ010 != 9) %>%
mutate(diabetes = case_when(
DIQ010 %in% c(1, 3) ~ "Yes",
DIQ010 =2 & LBXGH >=6.5 ~ "Yes",
DIQ010 =2 & LBXGH <6.5 ~ "No",
DIQ010 =2 & is.na(LBXGH) ~ "No")) %>%
inner_join(demodf,by="SEQN") %>%
left_join(insurdf, by="SEQN") %>%
left_join(padf, by="SEQN") %>%
left_join(dietdf, by="SEQN") %>%
left_join(bmidf, by="SEQN") %>%
left_join(bpdf, by="SEQN") %>%
left_join(mhdf, by="SEQN") %>%
select(SEQN,
RIDAGEYR, # age
RIAGENDR, # gender
RIDRETH3, # Race
INDHHIN2, # household income
HIQ011, # Health Insurance
PAQ650, # Physical Activity (Vigorous recreational activities)
PAQ665, # Physical Activity (Moderate recreational activities)
DBQ700, # How healthy the diet is
BMXBMI, # BMI
BPXDI3, # Diastolic: Blood pres (3rd rdg) mm Hg (range 0-134)
BPXSY3, # Systolic: Blood pres (3rd rdg) mm Hg (72-238)
starts_with("DPQ"), # MH screener questions
diabetes, # Diabates (Diagnosed and undiagnosed)
SDMVPSU, # Masked variance pseudo-PSU, weighting variable
SDMVSTRA, # Masked variance pseudo-stratum
WTINT2YR, # Full sample 2 year interview weight
WTMEC2YR) %>% # Full sample 2 year MEC exam weight
# use "na_if" to convert an annoying value to NA
mutate_at(c("HIQ011", "DBQ700", "PAQ650", "PAQ665"),
na_if, 7) %>% # use "na_if" to convert an annoying value to NA
mutate_at(c("HIQ011", "DBQ700", "PAQ665", "PAQ665"),
na_if, 9) %>%
mutate_at(vars(starts_with("DPQ")), # identify the variables starting with "DPQ"
na_if, 7) %>%
mutate_at(vars(starts_with("DPQ")),
na_if, 9) %>%
mutate_at("INDHHIN2",na_if, 77) %>%
mutate_at("INDHHIN2",na_if, 99) %>%
# Gender/Insurance/Diabates: Change to factor
mutate(RIAGENDR = factor(RIAGENDR, labels = c('Male', 'Female'))) %>%
mutate(HIQ011 = factor(HIQ011,labels = c('Insured', 'Uninsured'))) %>%
mutate(HIQ011 = relevel(HIQ011,ref='Uninsured')) %>%
mutate(diabetes=factor(diabetes)) %>%
# Race:
mutate(RaceEth = case_when(
RIDRETH3 %in% c(1:2) ~ "Hispanic Origin",
RIDRETH3 %in% c(3) ~ "NH White",
RIDRETH3 %in% c(4) ~ "NH Black",
RIDRETH3 %in% c(6:7) ~ "NH Asian/other")) %>%
mutate(RaceEth=factor(RaceEth)) %>%
mutate(RaceEth=relevel(RaceEth,ref="NH White")) %>%
# Household income :
mutate(income = case_when(
INDHHIN2 %in% c(1:4, 13) ~ "0-20K",
INDHHIN2 %in% c(5:8, 12) ~ "20-55K",
INDHHIN2 %in% c(9:10, 14,15) ~ "55-100K+")) %>%
# Diet: How healthy
mutate(diet = case_when(
DBQ700 %in% c(1:2) ~ "Very good/Excellent",
DBQ700 %in% c(3) ~ "Good",
DBQ700 %in% c(4:5) ~ "Poor/Fair")) %>%
# Physical Activity: Moderate or vigorous
mutate(pa=ifelse(PAQ650 ==1 | PAQ665==1 ,"Yes (active)","No (inactive)")) %>%
# BMI (kg/m**2): cut into four levels, make factor and add labels
mutate(bmi_cat = cut(BMXBMI,
c(0,18.5,25,30,Inf),right=F,
labels = c("underweight","normal","overweight","Obese"))) %>%
mutate(bmi_cat=factor(bmi_cat)) %>%
mutate(bmi_cat=relevel(bmi_cat,ref="normal")) %>%
# Depression scale total -> binary variable
mutate(deptot = rowSums(select(., starts_with("DPQ")), na.rm = T)) %>%
# create new variable "deptot", and remove "NA" to simplify the question
mutate(depressed = factor(deptot > 9, labels = c("Min/mild", "Mod/Severe"))) %>%
# HBP: DBP>=80 or SBP>=130 mmHg
mutate(hbp=ifelse(BPXDI3 >=80 | BPXSY3 >=130 ,"Yes (hypertension)","No (normal)")) %>%
# Age: select adults, cut into tertiles, make factor and add labels
filter(RIDAGEYR>=18) %>%
mutate(age_cat = cut(RIDAGEYR,
breaks = quantile(RIDAGEYR, probs = c(0, 1/3, 2/3, 1)),
include.lowest = T,
labels = c("18-39", "40-61", "62+"))) %>% #includes 80+
# filter based on inclusion criteria (The primary exposure)
filter(!is.na(pa))
# Save to local drive
save(nhanesdat, file="nhanesdat.RData")
################################################
# Analysis of NHANES 2017-2018 data on Diabates#
################################################
# Use load() to retrieve saved data
load("nhanesdat.RData")
library(tidyr)
library(gridExtra)
require(dplyr)
require(ggplot2)
##### Univariate analysis #####
library(arsenal)
Table_Univariate <-tableby(~ diabetes + pa + diet +
BMXBMI + BPXDI3 + BPXSY3 + deptot +
RIDAGEYR + RaceEth + RIAGENDR + income + HIQ011, data = nhanesdat)
summary(Table_Univariate)
##### Bivariate Analysis, use CreateTableOne() #####
library(tableone)
Table_Bivariate <-CreateTableOne(vars=c("pa","diet","bmi_cat","hbp","depressed",
"age_cat","RaceEth","RIAGENDR","income","HIQ011"),
factorVars =c("pa","diet","bmi_cat","hbp","depressed",
"age_cat","RaceEth","RIAGENDR","income","HIQ011"),
strata ="diabetes",
data=nhanesdat)
summary(Table_Bivariate)
# Plots - may or may not be included in report
nhanesdat %>%
filter(!is.na(age_cat)) %>%
group_by(age_cat, diabetes) %>% # order matters
summarise(n = n()) %>%
mutate(P = n/sum(n),
Percent = P * 100) %>%
ggplot(., aes(y=Percent, x = age_cat, fill=diabetes)) + geom_col(width=0.7) +
scale_fill_brewer(palette ='Blues')+
theme_bw()+theme(text = element_text(size = 15))+
ggtitle("Prevalence of Diabetes among Age Groups")
nhanesdat %>%
filter(!is.na(bmi_cat)) %>%
group_by(bmi_cat, diabetes) %>% # order matters
summarise(n = n()) %>%
mutate(P = n/sum(n),
Percent = P * 100) %>%
ggplot(., aes(y=Percent, x = bmi_cat, fill=diabetes)) + geom_col(width=0.7) +
scale_fill_brewer(palette ='Blues')+
theme_bw()+theme(text = element_text(size = 15))+
ggtitle("Prevalence of Diabetes by Weight (BMI)")
nhanesdat %>%
filter(!is.na(RaceEth)) %>%
filter(!is.na(pa)) %>%
group_by(RaceEth,pa,diabetes) %>% # order matters
summarise(n = n()) %>%
mutate(P = n/sum(n),
Percent = P * 100) %>%
ggplot(., aes(y=Percent, x = RaceEth,fill=diabetes)) + geom_col(width=0.7) +
scale_fill_brewer(palette ='Blues')+
geom_line(aes(y=Percent,x=RaceEth,group=diabetes),color="orangered3",linetype="dotdash")+
theme_bw()+theme(text = element_text(size = 15))+
facet_wrap(~pa)+
ggtitle("Prevalence of Diabetes Among Race/Ethnicity Groups by Physical Activity")
##### Multivariate Analysis #####
# MODEL 1: Unweighted basic model
g.base <- glm(diabetes ~ pa + diet + bmi_cat + hbp + depressed + age_cat + RaceEth + RIAGENDR + income + HIQ011+
pa:RaceEth,
data = nhanesdat, family = binomial())
summary(g.base)
# Variable selection, start with complete set (2858 observations used)
completeFun <- function(data, desiredCols) {
completeVec <- complete.cases(data[, desiredCols])
return(data[completeVec, ])
}
nhanes.cplt<-completeFun(nhanesdat,c("diabetes","pa","diet","bmi_cat","hbp","depressed",
"age_cat","RaceEth","RIAGENDR","income","HIQ011"))
g1 <- glm(diabetes ~ pa + diet + bmi_cat + hbp + depressed + age_cat + RaceEth + RIAGENDR +income + HIQ011 +
pa:RaceEth,
data = nhanes.cplt, family = binomial())
g2 <-step(g1,direction = "both")
summary(g1)
summary(g2)
# MODEL 2 : Unweighted final model
g.final <-glm(diabetes ~ pa + bmi_cat + depressed + age_cat + RaceEth + RIAGENDR +pa:RaceEth,
data = nhanesdat, family = binomial())
summary(g.final)
# MODEL 3: Weighted final model
library(survey)
library(srvyr)
library(sjPlot)
require(sjPlot)
nhanessvy <- nhanesdat %>%
as_survey_design(ids = SDMVPSU, strata = SDMVSTRA, nest = T, weights = WTMEC2YR)
g.svyfull <- svyglm(diabetes ~ pa + bmi_cat + depressed + age_cat + RaceEth + RIAGENDR +pa:RaceEth,
design=nhanessvy, family = quasibinomial(link = "logit"))
g.svyredu <- svyglm(diabetes ~ pa + bmi_cat + depressed + age_cat + RaceEth + RIAGENDR,
design=nhanessvy, family = quasibinomial(link = "logit"))
summary(g.svyfull)
summary(g.svyredu)
anova(g.svyfull,g.svyredu)
# Compare three models, and the prevalence of diabetes between unweighted and weighted data
library(epiR)
tab_model(g.base,g.final,g.svyfull,g.svyredu,CSS = css_theme("cells"))
plogis(-3.98495) # prob. of having diabetes for the reference group (base model)
plogis(-3.88495) # prob. of having diabetes for the reference group (reduced model - unweighted)
plogis(-3.8009) # prob. of having diabetes for the reference group (reduced moderl -weighted)
plogis(-3.8936) # prob. of having diabetes for the reference group (final model - weighted without interaction)
nhanesdat %>%
group_by(diabetes) %>%
summarise(n = n()) %>%
mutate(P=n/sum(n),
Proportion = P*100)
nhanessvy %>%
group_by(diabetes) %>%
summarise(Proportion = survey_mean(),
total = survey_total())
diabetes <-c("Yes","Yes","No","No")
sample <-c("Weighted","Unweighted","Weighted","Unweighted")
Proportion <-c(15.4,20.6,84.6,79.4)
diab.prev<-data.frame(diabates=diabetes,Sample=sample,Proportion=Proportion)
ggplot(diab.prev, aes(y=Proportion, x = sample,fill=diabetes)) +
geom_col(width=0.6) +
geom_text(aes(label = paste0(round(Proportion,1),"%")),
position = position_stack(vjust = 0.5),size=6,color="orangered3")+
scale_fill_brewer(palette ='Blues')+
theme_bw()+theme(text = element_text(size = 20))+
ggtitle("Prevalence of Diabetes NHANES 2017-2018 (Unweighted vs. Weighted)")
p1<-plot_model(g.final, title = "ORs for Model 2",
sortOdds = F, showModelSummary = T)
p2<-plot_model(g.svyfull, title = "ORs for Model 3",
sortOdds = F, showModelSummary = T)
p3<-plot_model(g.svyredu, title = "ORs for Model 4 ",
sortOdds = F, showModelSummary = T)
grid.arrange(p1, p2,p3,ncol=3)