-
Notifications
You must be signed in to change notification settings - Fork 3
/
f - table_2_weighted.R
347 lines (296 loc) · 14.3 KB
/
f - table_2_weighted.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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#################################### CALCULATE WEIGHTED SUMMARY STATISTICS ##################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Purpose: This function calculates summary statistics for participants and survey weights them
#
# Inputs: nhanes_subset - dataframe of complete demographics, cells, and chemicals
#
# Outputs: weighted table 1
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
table_2_weighted <- function(nhanes_subset,
demog_dataset)
{
library(tidyverse)
library(survey)
library(sjlabelled)
library(gtsummary)
library(gt)
library(flextable)
#Temporary
# nhanes_subset <- nhanes_subset_dataset
# 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
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)
# Multiply MEC4 weights by 2/10 and MEC2 by 1/10
# Merge the weights columns
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)
sum(is.na(vars_weights_clean$weights_adjusted))
sum(vars_weights_clean$weights_adjusted)
#188 million
#############################################################################################################
######################################## Create Survey Design Object ########################################
#############################################################################################################
#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")
# Clean up the dataset, set the variable order
LR_data <- vars_weights_clean %>%
dplyr::select("LBDLYMNO", #lymphocytes
"LBDNENO", #neutrophils
"LBDMONO", #monocytes
"LBDBANO", #basophils
"LBDEONO", #eosinophils
"LBXWBCSI", #WBC count
"LBXRBCSI", #RBC count
"LBXMCVSI", #MCV
SDMVPSU,
SDMVSTRA,
weights_adjusted)
# Set the labels for all the variables
LR_data$LBDLYMNO <- set_label(LR_data$LBDLYMNO, "Lymphocytes (1000 cells/uL)")
LR_data$LBDNENO <- set_label(LR_data$LBDNENO, "Neutrophils (1000 cells/uL)")
LR_data$LBDMONO <- set_label(LR_data$LBDMONO, "Monocytes (1000 cells/uL)")
LR_data$LBDBANO <- set_label(LR_data$LBDBANO, "Basophils (1000 cells/uL)")
LR_data$LBDEONO <- set_label(LR_data$LBDEONO, "Eosinophils (1000 cells/uL)")
LR_data$LBXWBCSI <- set_label(LR_data$LBXWBCSI, "White Blood Cells (1000 cells/uL)")
LR_data$LBXRBCSI <- set_label(LR_data$LBXRBCSI, "Red Blood Cells (million cells/uL)")
LR_data$LBXMCVSI <- set_label(LR_data$LBXMCVSI, "Mean Corpuscular Volume (fL)")
str(LR_data)
# Pull everything together to get the survey adjustment
NHANES.svy <- svydesign(strata = ~SDMVSTRA
, id = ~SDMVPSU
, weights = ~weights_adjusted
, data = LR_data
, nest = TRUE)
#############################################################################################################
########################################## Create Unweighted Tables #########################################
#############################################################################################################
# MEAN (SD)
unwt_mean <- LR_data %>%
tbl_summary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{mean} ({sd})"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Mean (sd)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
# MEDIAN (IQR)
unwt_median <- LR_data %>%
tbl_summary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{median} ({IQR})"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Median (IQR)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
# RANGE
unwt_range <- LR_data %>%
tbl_summary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{min} - {max}"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Range**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
#############################################################################################################
########################################### Create Weighted Tables ##########################################
#############################################################################################################
# MEAN (SD)
wt_mean <- NHANES.svy %>%
tbl_svysummary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{mean} ({sd})"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Mean (sd)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
# MEDIAN (IQR)
wt_median <- NHANES.svy %>%
tbl_svysummary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{median} ({p75} - {p25})"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Median (IQR)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
#Calculate IQR in the most convoluted way possible, but only because I got to the end and realized I can't convert a tibble back into a gtsummary object
#and I'm not remaking code to calculate the survey adjusted IQR
wt_iqr_calculations <- NHANES.svy %>%
tbl_svysummary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{median} ({p75} - {p25})"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Median (IQR)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA) %>%
as_tibble() %>%
mutate(iqr_full = str_extract(string = .$`**Median (IQR)**`,
pattern = "(?<=\\().*(?=\\))"),
p_75 = as.numeric(gsub(" .*$", "", iqr_full)),
p_25 = as.numeric(gsub(".*- ", "", iqr_full)),
var_median = as.numeric(gsub(" .*$", "", `**Median (IQR)**`))) %>%
mutate(calc_iqr = (p_75 - p_25)) %>%
pull(calc_iqr) %>%
as.numeric()
print("paste these weighted IQR numbers into the table manually")
print(wt_iqr_calculations)
# RANGE
wt_range <- NHANES.svy %>%
tbl_svysummary(include = c(LBDLYMNO,
LBDNENO,
LBDMONO,
LBDBANO,
LBDEONO,
LBXWBCSI,
LBXRBCSI,
LBXMCVSI),
statistic = list(all_continuous() ~ "{min} - {max}"),
digits = list(all_continuous() ~ c(1, 1)),
missing = "no"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Range**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
#############################################################################################################
################################################ Merge Tables ###############################################
#############################################################################################################
#set directory
setwd(paste0(current_directory, "/Tables - Table 1"))
# Merge unweighted tables
unwt_stats <- tbl_merge(tbls = list(unwt_mean, unwt_median, unwt_range)) %>%
modify_spanning_header(update = everything() ~ NA)
# Merge weighted tables
wt_stats <- tbl_merge(tbls = list(wt_mean, wt_median, wt_range)) %>%
modify_spanning_header(update = everything() ~ NA)
# Merge unweighted and weighted - save as html
# tbl_merge(tbls = list(unwt_stats, wt_stats),
# tab_spanner = c("**Unweighted**", "**Weighted**")) %>%
# as_gt() %>%
# gtsave(filename = "table_2_unweighted-weighted_new.html")
# Import table into Word and make any formatting edits (Insert, Text:Object:Text from file)
# Merge unweighted and weighted - save as word doc
# tbl_merge(tbls = list(unwt_stats, wt_stats),
# tab_spanner = c("**Unweighted**", "**Weighted**")) %>%
# as_flex_table() %>%
# save_as_docx(path = "table_2_unweighted-weighted_new.docx")
# Save Weighted and Unweighted tables separately
unwt_stats %>%
as_flex_table() %>%
save_as_docx(path = "table_2_unweighted_new.docx")
wt_stats %>%
as_flex_table() %>%
save_as_docx(path = "table_2_weighted_new.docx")
#############################################################################################################
setwd(current_directory)
}