-
Notifications
You must be signed in to change notification settings - Fork 4
/
f - run_if_else_glm_wt_nosmk.R
297 lines (244 loc) · 12.3 KB
/
f - run_if_else_glm_wt_nosmk.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
# Sensitivity analysis of linear regression: survey weighted, but unadjusted for smoking
run_if_else_glm_wt_nosmk <- function(chemical_immune_chunk,
weights_dataset,
df_chem_master,
nhanes_subset)
{
library(tidyverse)
library(broom)
library(survey)
#############################################################################################################
################################################ Get Basic Info #############################################
#############################################################################################################
chem_codename <- chemical_immune_chunk$chemical_codename %>%
unique(.)
print(chem_codename)
#update the name to include WT to match the weights codenames for the chemicals
if(chem_codename == "LBX138LA")
{
chem_weight_codename <- "WT_LBX138158LA"
} else if(chem_codename == "LBX196LA") {
chem_weight_codename <- "WT_LBX196203LA"
} else {
chem_weight_codename <- paste0("WT_", chem_codename)
}
immune_codename <- chemical_immune_chunk$celltype_codename %>%
unique(.)
print(paste(immune_codename, chem_codename))
# print(chem_codename)
#Calculate the number of cycles per chemical
# sort(unique(chemical_immune_chunk$SDDSRVYR))
cycle_length <- length(unique(chemical_immune_chunk$SDDSRVYR))
print(paste("Number of cycles:", cycle_length))
print(unique(chemical_immune_chunk$SDDSRVYR))
#remove NAs from entire dataset
chemical_immune_chunk <- chemical_immune_chunk
print(dim(chemical_immune_chunk))
# grabs the unique data for the cycle year (SDDSRVYR) for non-NA chemical concentrations
cycle_unique <- unique(chemical_immune_chunk$SDDSRVYR)
# print(str_detect(chem_codename, "^LB"))
#mute the summarize grouping message to reduce clutter in the console output
options(dplyr.summarise.inform = FALSE)
#############################################################################################################
#################################### Adjust the Weights Based on Cycle Info #################################
#############################################################################################################
#select SEQN and chemical/immune combo
subset_weights_dataset <- weights_dataset %>%
filter(SDDSRVYR != -1 ) %>%
dplyr::select(SEQN,
all_of(chem_weight_codename))
#merge in the weights dataset into the chemical_immune_chunk
chunk_and_weight <- left_join(chemical_immune_chunk, subset_weights_dataset, by = "SEQN") %>%
rename_at(vars(starts_with("WT_")), ~ "unadj_weight") %>%
drop_na(unadj_weight) %>%
filter(!unadj_weight == 0)
# View(chunk_and_weight)
#select SEQN and the survey variables - they're already in chunk_and_weight
# survey_df <- nhanes_subset %>%
# dplyr::select(SEQN,
# SDMVPSU,
# SDMVSTRA)
# View(survey_df)
#merge in the survey variables
# chunk_weight_survey <- left_join(chunk_and_weight, survey_df, by = "SEQN")
chunk_weight_survey <- chunk_and_weight
# View(chunk_weight_survey)
# #check if any chemicals were measured in only cycle 1 and not 2
test <- long_nhanes_subset_dataset %>%
group_by(chemical_codename) %>%
summarise(cycle_count = unique(SDDSRVYR)) %>%
ungroup()
test_tab <- as.data.frame.matrix(table(test$chemical_codename, test$cycle_count))
colnames(test_tab) <- paste0("cycle", 1:10)
df_problematic_pesticides <- find_problematic_pesticides(df_chem_master)
identifiers <- test_tab %>%
mutate(category_id = case_when(cycle1 == 1 & cycle2 == 0 ~ "1 or 2",
cycle2 == 1 & cycle1 == 0 ~ "1 or 2",
cycle1 == 1 & cycle2 == 1 ~ "1 and 2"
)) %>%
mutate(category_id = ifelse(is.na(category_id), "other", category_id))
# View(identifiers)
# #so there are some chemicals that are only measured in cycle 1 and not 2 and also 2 and not 1
index_problematic_pesticides <- which(rownames(identifiers) %in% df_problematic_pesticides$chemical_codename_use)
# print(rownames(identifiers)[index_problematic_pesticides])
identifiers[index_problematic_pesticides,"category_id"] <- "1 or 2"
# View(identifiers)
#merge in the category_ids into chunk_weight_survey for categorization
#make the rownames of identifiers into a column called chemical_codenames
identifiers_chem <- rownames_to_column(identifiers, var = "chemical_codename")
# View(identifiers_chem)
#only keep the identifiers and chemical_codename
id_chem <- identifiers_chem %>%
dplyr::select(chemical_codename,
category_id)
chunk_weight_id <- left_join(chunk_weight_survey, id_chem, by = "chemical_codename")
# View(chunk_weight_id)
# Corrects the weights according to rules for combining data across multiple NHANES cycles
chunk_and_ad_weight <- chunk_weight_id %>%
mutate(adjusted_weights = case_when(SDDSRVYR %in% c(1,2) & category_id == "1 and 2" ~ ((2/cycle_length)*(unadj_weight))
, SDDSRVYR %in% c(1,2) & category_id == "1 or 2" ~ ((1/cycle_length)*(unadj_weight))
, SDDSRVYR %in% c(3:10) ~ (1/cycle_length)*(unadj_weight)
))
# print(str(chunk_and_ad_weight$RIDRETH1))
# View(chunk_and_ad_weight)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ACCOUNT FOR NHANES SAMPLING DESIGN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#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")
# Define a data frame containing the outcome variable and necessary covariates
LR_data <- data.frame(SEQN = factor(chunk_and_ad_weight$SEQN)
, SDDSRVYR = factor(chunk_and_ad_weight$SDDSRVYR)
, RIDAGEYR = I(chunk_and_ad_weight$RIDAGEYR)
, RIAGENDR = relevel(factor(chunk_and_ad_weight$RIAGENDR), ref = "1")
, RIDRETH1 = relevel(factor(chunk_and_ad_weight$RIDRETH1), ref = "3")
, INDFMPIR = I(chunk_and_ad_weight$INDFMPIR)
, BMXWAIST = I(chunk_and_ad_weight$BMXWAIST)
, URXUCR = I(chunk_and_ad_weight$URXUCR)
, SMOKING = chunk_and_ad_weight$SMOKING
, cell_measurement = I(chunk_and_ad_weight$cell_measurement)
, chem_log_measurement = I(chunk_and_ad_weight$chem_log_measurement)
, SDMVPSU = chunk_and_ad_weight$SDMVPSU #stage 1 sampling unit in NHANES
, SDMVSTRA = chunk_and_ad_weight$SDMVSTRA #stratum to which the PSU belongs
, adjusted_weights = chunk_and_ad_weight$adjusted_weights) %>%
drop_na(SMOKING)
print("Dimension of LR_data")
print(dim(LR_data))
# print(LR_data$RIDRETH1 %>% levels(.))
#pull everything together to get the survey adjustment
NHANES.svy <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~adjusted_weights
, data = LR_data
, nest = TRUE)
# print(NHANES.svy)
# print("nhanes.svy design variable created")
#############################################################################################################
########################################### Sort And Run Regressions ########################################
#############################################################################################################
#survey cycle >1 and blood
if (cycle_length > 1 & str_detect(chem_codename, "^LB"))
{
# print(as.numeric(LR_data$SDDSRVYR))
min_cycle <- min(as.numeric(LR_data$SDDSRVYR), na.rm = TRUE)
# print(min_cycle)
print(1)
# print(paste0("reference cycle: ", min_cycle))
LR_data$SDDSRVYR <- factor(LR_data$SDDSRVYR)
# print(levels(LR_data$SDDSRVYR))
model <- svyglm(cell_measurement ~
chem_log_measurement+
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
SDDSRVYR,
na.action = na.omit,
design = NHANES.svy,
data = LR_data)
# print(tidy(model))
print("chemical in more than one survey cycle")
#
# #survey cycle ==1 and blood
} else
{
if (cycle_length == 1 & str_detect(chem_codename, "^LB"))
{
print(2)
print(paste0("survey cycle: ", unique(LR_data$SDDSRVYR)))
model <- svyglm(cell_measurement ~
chem_log_measurement+
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST,
na.action = na.omit,
design = NHANES.svy,
data = LR_data)
# print(tidy(model))
print("chemical only in one survey cycle")
#
# #survey cycle >1 and urinary
} else {
if (cycle_length > 1 & str_detect(chem_codename, "^LB", negate = TRUE)) {
print(3)
min_cycle <- min(as.numeric(LR_data$SDDSRVYR), na.rm = TRUE)
# print(min_cycle)
# print(paste0("reference cycle: ", min_cycle))
LR_data$SDDSRVYR <- factor(LR_data$SDDSRVYR)
# print(levels(LR_data$SDDSRVYR))
model <- svyglm(cell_measurement ~
chem_log_measurement +
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
URXUCR+
SDDSRVYR,
na.action = na.omit,
design = NHANES.svy,
data = LR_data)
# print(tidy(model))
print("chemical in more than one survey cycle")
#
#
} else { #survey cycle ==1 and urinary
print(4)
# print(paste0("survey cycle: ", unique(LR_data$SDDSRVYR)))
model <- svyglm(cell_measurement ~
chem_log_measurement+
RIDRETH1+
RIDAGEYR+
RIAGENDR+
INDFMPIR+
BMXWAIST+
URXUCR,
na.action = na.omit,
design = NHANES.svy,
data = LR_data)
# print(tidy(model))
print("chemical only in one survey cycle")
}
}
}
#############################################################################################################
#################################### Calculate sample size of each model ####################################
#############################################################################################################
nobs <- glance(model) %>%
pull(nobs) %>%
unlist(.)
# print(nobs)
#############################################################################################################
######################################### Turn Regressions Into Table #######################################
#############################################################################################################
#compile regression models using broom
df_tidy <- tidy(model)
df_tidy <- as.data.frame(df_tidy) %>%
mutate(nobs = nobs)
return(df_tidy)
}