-
Notifications
You must be signed in to change notification settings - Fork 4
/
f - run_demog_regression.R
553 lines (489 loc) · 21.5 KB
/
f - run_demog_regression.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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
######################################### Adjusted Linear Regression ########################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Purpose: This function runs the adjusted linear regressions for the demographics and immune measures
#
# Inputs: nhanes_subset - dataframe containing complete demographic and immune data for each
# participant
# conversion - dataframe of chemical names, codenames, and families
# demog_dataset - dataframe that includes the MEC survey weights
#
# Outputs: model_stats_demog - dataframe of linear regression outputs
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
run_demog_regression <- function(nhanes_subset,
conversion,
demog_dataset)
{
library(tidyverse)
library(survey)
library(broom)
library(gt)
library(flextable)
#TEMPORARY
# nhanes_subset <- nhanes_subset_dataset
# conversion <- use_these_chems
# demog_dataset <- demographics_clean
#############################################################################################################
###################################### Select Variables, Merge Datasets #####################################
#############################################################################################################
# Select the variables to use from nhanes subset and demographics dataset
nhanes_vars <- nhanes_subset %>%
dplyr::select(SEQN,
"LBDLYMNO", #lymphocytes
"LBDNENO", #neutrophils
"LBDMONO", #monocytes
"LBDBANO", #basophils
"LBDEONO", #eosinophils
"LBXWBCSI", #WBC count
"LBXRBCSI", #RBC count
"LBXMCVSI", #MCV
RIAGENDR,
RIDRETH1,
RIDAGEYR,
INDFMPIR,
BMXWAIST,
SMOKING,
SDDSRVYR,
SDMVPSU,
SDMVSTRA)
demog_vars <- demog_dataset %>%
dplyr::select(SEQN,
WTMEC4YR,
WTMEC2YR)
# Merge the two subsets of variables and keep only participants previously selected
vars_weights <- left_join(nhanes_vars, demog_vars, by = "SEQN")
#############################################################################################################
############################################### Clean Weights ###############################################
#############################################################################################################
# Make a subset of participants who were in cycles 1 and 2
cycles1_2 <- c(1:2)
vars_12 <- vars_weights %>%
filter(SDDSRVYR %in% cycles1_2)
# Make WTMEC2YR NA because CDC says to use the MEC4 weights for the first two cycles
vars_12$WTMEC2YR <- NA
# Check that all participants have weights
summary(vars_12$WTMEC4YR)
sum(is.na(vars_12$WTMEC4YR))
sum(!is.na(vars_12$WTMEC4YR))
# Make a subset of participants after cycle 2
cycle_other <- c(3:10)
vars_other <- vars_weights %>%
filter(SDDSRVYR %in% cycle_other)
# Make WTMEC4YR NA because CDC says to use the MEC2 weights for cycles after 2
vars_other$WTMEC4YR <- NA
# Check that all participants have weights
summary(vars_other$WTMEC2YR)
sum(is.na(vars_other$WTMEC2YR))
sum(!is.na(vars_other$WTMEC2YR)) #37173 + 8355 = 45528
# Everyone has a weight and neither set of weights has extra rows
# Combine the two subsets back together
vars_weights_merge <- bind_rows(vars_12, vars_other)
dim(vars_weights_merge)
rm(vars_weights)
# Multiply MEC4 weights by 2/10 and MEC2 by 1/10
# Merge the weights columns
# Make factor variables
vars_weights_clean <- vars_weights_merge %>%
mutate(mec4_clean = WTMEC4YR * (2/10),
mec2_clean = WTMEC2YR * (1/10)) %>%
mutate(weights_adjusted = coalesce(mec4_clean, mec2_clean)) %>%
dplyr::select(-WTMEC4YR,
-WTMEC2YR,
-mec4_clean,
-mec2_clean,
-SMOKING) %>%
mutate(RIAGENDR = relevel(factor(RIAGENDR),
ref = 1),
RIDRETH1 = relevel(factor(RIDRETH1),
ref = 3),
SDDSRVYR = factor(SDDSRVYR))
sum(is.na(vars_weights_clean$weights_adjusted))
sum(vars_weights_clean$weights_adjusted)
#188 million
#############################################################################################################
######################################### Create Individual Datasets ########################################
#############################################################################################################
#Chirag suggests to remove the "lonely" PSUs - strata with only one PSU
#this is because "a single-PSU stratum makes no contribution to the variance"
# - https://r-survey.r-forge.r-project.org/survey/html/surveyoptions.html
options(survey.lonely.psu = "remove")
# Make datasets for each immune measure
# Lymphocytes dataset (don't really need to pivot longer but it will make the code the same later)
LR_data_ly <- vars_weights_clean %>%
dplyr::select(-"LBDNENO", #neutrophils
-"LBDMONO", #monocytes
-"LBDBANO", #basophils
-"LBDEONO", #eosinophils
-"LBXWBCSI", #WBC count
-"LBXRBCSI", #RBC count
-"LBXMCVSI" ) %>% #MCV
pivot_longer(cols = "LBDLYMNO", #lymphocytes
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# Neutrophils dataset
LR_data_ne <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDMONO",
-"LBDBANO",
-"LBDEONO",
-"LBXWBCSI",
-"LBXRBCSI",
-"LBXMCVSI") %>%
pivot_longer(cols = "LBDNENO",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# Monocytes dataset
LR_data_mo <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDNENO",
-"LBDBANO",
-"LBDEONO",
-"LBXWBCSI",
-"LBXRBCSI",
-"LBXMCVSI") %>%
pivot_longer(cols = "LBDMONO",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# Basophils dataset
LR_data_ba <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDNENO",
-"LBDMONO",
-"LBDEONO",
-"LBXWBCSI",
-"LBXRBCSI",
-"LBXMCVSI") %>%
pivot_longer(cols = "LBDBANO",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# Eosinophils dataset
LR_data_eo <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDNENO",
-"LBDMONO",
-"LBDBANO",
-"LBXWBCSI",
-"LBXRBCSI",
-"LBXMCVSI") %>%
pivot_longer(cols = "LBDEONO",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# WBC dataset
LR_data_wbc <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDNENO",
-"LBDMONO",
-"LBDBANO",
-"LBDEONO",
-"LBXRBCSI",
-"LBXMCVSI") %>%
pivot_longer(cols = "LBXWBCSI",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# RBC dataset
LR_data_rbc <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDNENO",
-"LBDMONO",
-"LBDBANO",
-"LBDEONO",
-"LBXWBCSI",
-"LBXMCVSI") %>%
pivot_longer(cols = "LBXRBCSI",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
# MCV dataset
LR_data_mcv <- vars_weights_clean %>%
dplyr::select(-"LBDLYMNO",
-"LBDNENO",
-"LBDMONO",
-"LBDBANO",
-"LBDEONO",
-"LBXWBCSI",
-"LBXRBCSI") %>%
pivot_longer(cols = "LBXMCVSI",
names_to = "celltype_codename",
values_to = "cell_measurement") %>%
dplyr::select(-SEQN) %>%
na.omit(.)
#############################################################################################################
############################################# Make Survey Objects ###########################################
#############################################################################################################
# Pull everything together to get the survey adjustment
svy_nhanes_ly <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_ly
, nest = TRUE)
svy_nhanes_ne <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_ne
, nest = TRUE)
svy_nhanes_mo <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_mo
, nest = TRUE)
svy_nhanes_ba <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_ba
, nest = TRUE)
svy_nhanes_eo <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_eo
, nest = TRUE)
svy_nhanes_wb <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_wbc
, nest = TRUE)
svy_nhanes_rb <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_rbc
, nest = TRUE)
svy_nhanes_mc <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data_mcv
, nest = TRUE)
#############################################################################################################
############################################# Linear Regressions ############################################
#############################################################################################################
# Run regressions
# Add confidence intervals + FDRs
# Keep only age term
# Add a row label for immune measure
z_score <- 1.96
df_regression_ly <- LR_data_ly %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_ly,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Lymphocytes")
df_regression_ne <- LR_data_ne %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_ne,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Neutrophils")
df_regression_mo <- LR_data_mo %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_mo,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Monocytes")
df_regression_ba <- LR_data_ba %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_ba,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Basophils")
df_regression_eo <- LR_data_eo %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_eo,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Eosinophils")
df_regression_wb <- LR_data_wbc %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_wb,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "White Blood Cells")
df_regression_rb <- LR_data_rbc %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_rb,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Red Blood Cells")
df_regression_mc <- LR_data_mcv %>%
do(svyglm(cell_measurement ~
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
design = svy_nhanes_mc,
data = .) %>%
tidy(.)) %>%
mutate(lower.CI = estimate - (z_score*std.error),
upper.CI = estimate + (z_score*std.error)) %>%
# mutate(FDR = p.adjust(p.value, method = "fdr")) %>%
filter(term == "RIDAGEYR") %>%
mutate(immune_measure = "Mean Corpuscular Volume")
#############################################################################################################
###################################### ADD CONFIDENCE INTERVALS AND FDR #####################################
#############################################################################################################
# Put the results together
model_stats_demog <- bind_rows(list(df_regression_ly,
df_regression_ne,
df_regression_mo,
df_regression_ba,
df_regression_eo,
df_regression_wb,
df_regression_rb,
df_regression_mc))
# Multiple comparisons of age to different immune measures so adding the FDR
model_stats_demog$FDR <- p.adjust(model_stats_demog$p.value, method='BY')
# Interpret the results for a 10-year increase in age
chem_pct <- c("Lymphocytes",
"Neutrophils",
"Monocytes",
"Basophils",
"Eosinophils")
# print("test")
model_interpret <- model_stats_demog %>%
mutate(interpret = case_when(immune_measure %in% chem_pct ~ estimate*1000*10,
immune_measure == "White Blood Cells" ~ estimate * 1000*10,
immune_measure == "Red Blood Cells" ~ estimate * 1000000*10,
immune_measure == "Mean Corpuscular Volume" ~ estimate*10)) %>%
mutate(lower_ci = case_when(immune_measure %in% chem_pct ~ lower.CI*1000*10,
immune_measure == "White Blood Cells" ~ lower.CI * 1000*10,
immune_measure == "Red Blood Cells" ~ lower.CI * 1000000*10,
immune_measure == "Mean Corpuscular Volume" ~ lower.CI*10)) %>%
mutate(upper_ci = case_when(immune_measure %in% chem_pct ~ upper.CI*1000*10,
immune_measure == "White Blood Cells" ~ upper.CI * 1000*10,
immune_measure == "Red Blood Cells" ~ upper.CI * 1000000*10,
immune_measure == "Mean Corpuscular Volume" ~ upper.CI*10)) %>%
mutate(units = case_when(immune_measure %in% chem_pct ~ "cells per uL",
immune_measure == "White Blood Cells" ~ "cells per uL",
immune_measure == "Red Blood Cells" ~ "cells per uL",
immune_measure == "Mean Corpuscular Volume" ~ "fL"))
# print("test 2")
#############################################################################################################
############################################# Make The Table Nice ###########################################
#############################################################################################################
# Clean up results table
model_clean <- model_interpret %>%
dplyr::select(-statistic,
-p.value,
-lower.CI,
-upper.CI,
-std.error,
-estimate) %>%
relocate(immune_measure, .before = "term") %>%
relocate(interpret, .after = "term") %>%
relocate(lower_ci, .after = "interpret") %>%
relocate(upper_ci, .after = "lower_ci") #%>%
# mutate(lower_ci = sprintf("%0.2f", lower_ci),
# upper_ci = sprintf("%0.2f", upper_ci)) %>%
# mutate(ci = paste0(lower_ci, " - ", upper_ci)) %>%
# relocate(ci, .after = "interpret") %>%
# dplyr::select(-lower_ci,
# -upper_ci)
# Set wd
setwd(paste0(current_directory, "/Regression Results"))
write.csv(model_clean, "age regressions.csv")
# Make a gt table for saving as html
# model_clean %>%
# dplyr::select(-term) %>%
# gt() %>%
# cols_label(interpret = "Beta Interpretation",
# immune_measure = "Immune Measure",
# ci = "95% Confidence Interval",
# units = "Units") %>%
# fmt_number(columns = c("interpret"),
# decimals = 2) %>%
# fmt_scientific(columns = "FDR",
# decimals = 1) %>%
# cols_align(align = "center",
# columns = c("interpret",
# "ci",
# "FDR")) %>%
# # cols_align(align = "right",
# # columns = "") %>%
# tab_options(column_labels.font.weight = "bold") %>%
# gtsave("interpreted_age_regressions_new.html")
View(model_clean)
# Reset the working directory
setwd(current_directory)
}