-
Notifications
You must be signed in to change notification settings - Fork 3
/
f - table_1_weighted.R
362 lines (309 loc) · 14.6 KB
/
f - table_1_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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#################################### 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_1_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,
RIDRETH1,
RIDAGEYR,
RIAGENDR,
INDFMPIR,
BMXWAIST,
SDDSRVYR,
SMOKING,
URXUCR,
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)) #8355
# 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 %>%
mutate(RIDRETH1 = factor(RIDRETH1,
levels = c(1, 2, 3, 4, 5),
labels=c("Mexican American",
"Other Hispanic",
"Non-Hispanic White",
"Non-Hispanic Black",
"Other Race")),
RIAGENDR = factor(RIAGENDR,
levels = c(1, 2),
labels = c("Male", "Female")),
SDDSRVYR = factor(SDDSRVYR)) %>%
dplyr::select(RIAGENDR,
RIDRETH1,
RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING,
SDMVPSU,
SDMVSTRA,
weights_adjusted)
# Set the labels for all the variables
LR_data$RIDAGEYR <- set_label(LR_data$RIDAGEYR, "Age (years)")
LR_data$RIAGENDR <- set_label(LR_data$RIAGENDR, "Sex")
LR_data$RIDRETH1 <- set_label(LR_data$RIDRETH1, "Race/Ethnicity")
LR_data$INDFMPIR <- set_label(LR_data$INDFMPIR, "Family PIR")
LR_data$BMXWAIST <- set_label(LR_data$BMXWAIST, "Waist Circumference (cm)")
LR_data$URXUCR <- set_label(LR_data$URXUCR, "Urinary Creatinine")
LR_data$SMOKING <- set_label(LR_data$SMOKING, "Cotinine (ng/mL)")
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 #########################################
#############################################################################################################
# COUNT (%)
unwt_count <- LR_data %>%
tbl_summary(include = c(RIAGENDR,
RIDRETH1),
statistic = all_categorical() ~ "{n} ({p}%)",
digits = list(all_categorical() ~ c(1, 1)),
missing = "no",
sort = all_categorical() ~ "frequency"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Count (%)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
# MEAN (SD)
unwt_mean <- LR_data %>%
tbl_summary(include = c(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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 ##########################################
#############################################################################################################
# COUNT (%)
wt_count <- NHANES.svy %>%
tbl_svysummary(include = c(RIAGENDR,
RIDRETH1),
statistic = all_categorical() ~ "{n} ({p}%)",
digits = list(all_categorical() ~ c(1, 1)),
missing = "no",
sort = all_categorical() ~ "frequency"
) %>%
modify_header(label ~ "**Variable**") %>%
modify_header(stat_0 ~ "**Count (%)**") %>%
bold_labels() %>%
modify_footnote(update = everything() ~ NA)
# MEAN (SD)
wt_mean <- NHANES.svy %>%
tbl_svysummary(include = c(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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(RIDAGEYR,
INDFMPIR,
BMXWAIST,
URXUCR,
SMOKING),
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_count, unwt_mean, unwt_median, unwt_range)) %>%
modify_spanning_header(update = everything() ~ NA)
# Merge weighted tables
wt_stats <- tbl_merge(tbls = list(wt_count, 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_1_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_1_unweighted-weighted_new.docx")
# Save Weighted and Unweighted tables separately
unwt_stats %>%
as_flex_table() %>%
save_as_docx(path = "table_1_unweighted_new.docx")
wt_stats %>%
as_flex_table() %>%
save_as_docx(path = "table_1_weighted_new.docx")
#############################################################################################################
setwd(current_directory)
}