-
Notifications
You must be signed in to change notification settings - Fork 3
/
f - correlation_plot_unweight_weight.R
279 lines (238 loc) · 12.4 KB
/
f - correlation_plot_unweight_weight.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
correlation_plot_unweight_weight <- function(model_stats_wt_adj,
model_stats_wt_unadj)
{
#PURPOSE: scaled cotinine adj vs scaled non-cotinine adj, both weighted
library(tidyverse)
library(ggrepel)
# library(rstatix)
library(broom)
#TEMPORARY
# model_stats_wt_adj <- model_stats_smk_scaled
# model_stats_wt_unadj <- model_stats_scale_no_smk
###########################################################################################################
############################################## CLEAN DATASETS #############################################
###########################################################################################################
# pull the estimates from each dataset
#weighted and cotinine
wt_est <- model_stats_wt_adj %>%
filter(term == "chem_log_measurement") %>%
mutate(immune_measure = case_when(immune_measure == "Lymphocyte (1000 cells/uL)" ~ "Lymphocytes (1000 cells/uL)",
immune_measure == "Monocyte (1000 cells/uL)" ~ "Monocytes (1000 cells/uL)",
TRUE ~ immune_measure)) %>%
dplyr::select(chemical_name,
chem_family,
immune_measure,
celltype_codename,
estimate,
FDR) %>%
rename(estimate_wt = estimate,
FDR_wt = FDR)
#weighted and no cotinine
wt_unadj_est <- model_stats_wt_unadj %>%
filter(term == "chem_log_measurement") %>%
mutate(immune_measure = case_when(immune_measure == "Lymphocyte (1000 cells/uL)" ~ "Lymphocytes (1000 cells/uL)",
immune_measure == "Monocyte (1000 cells/uL)" ~ "Monocytes (1000 cells/uL)",
TRUE ~ immune_measure)) %>%
dplyr::select(chemical_name,
chem_family,
immune_measure,
celltype_codename,
estimate,
FDR) %>%
rename(estimate_wt_nosmk = estimate,
FDR_wt = FDR)
# View(wt_est)
# View(wt_unadj_est)
#merge by these columns
merge_by <- c("chemical_name",
"chem_family",
"immune_measure",
"celltype_codename")
###########################################################################################################
############################################## MERGE DATASETS #############################################
###########################################################################################################
#merge datasets
wt_unadj_adj <- full_join(wt_est, wt_unadj_est, by = merge_by)
# View(wt_unadj_adj)
#############################################################################################################
################################# DEFINE COLORS FOR FOREST PLOT - CHEM CLASSES ##############################
#############################################################################################################
# Define a vector of chemical family names in a particular order
chem_family_levels <- c("Acrylamide"
# , "Melamine"
, "Brominated Flame Retardants (BFR)"
, "Phosphate Flame Retardants (PFR)"
, "Polychlorinated Biphenyls (PCB)"
, "Dioxins"
, "Furans"
, "Metals"
, "Phthalates & Plasticizers"
, "Personal Care & Consumer Product Compounds"
, "Pesticides"
, "Aromatic Amines"
# , "Phytoestrogens"
, "Polyaromatic Hydrocarbons (PAH)"
, "Volatile Organic Compounds (VOC)"
, "Smoking Related Compounds"
, "Per- and Polyfluoroalkyl Substances (PFAS)"
, "Aldehydes"
# , "Dietary Components"
, "Other")
# Define a string vector of color hexcodes for the chemical family in corresponding order
chem_family_colors <- c("#8B0000" # Acrylamide
# , "#9b870c" # Melamine
, "#EE0000" # BFRs
, "#FF6B00" # PFRs
, "#FF69B4" # PCBs
, "#FFA500" # Dioxins
, "#EEEE00" # Furans
, "#228B22" # Metals
, "#A4D3EE" # Phthalates & Plasticizers
, "#A2CD5A" # Personal Care
, "#1E90FF" # Pesticides
, "#be67c9" # Aromatic Amines
# , "#7D26CD" # Phytoestrogens
, "#cf9b76" # PAHs
, "#828282" # VOCs
, "#8B4513" # Smoking
, "#FFB6C1" # PFCs
, "#0E1171" # Aldehydes
, "#BABABA" ) # Other
# Define a string vector of shape codes for the chemical family in corresponding order
chem_family_shapes <- c(16 # Acrylamide
# , "#9b870c" # Melamine
, 16 # BFRs
, 16 # PFRs
, 16 # PCBs
, 16 # Dioxins
, 16 # Furans
, 18 # Metals
, 16 # Phthalates & Plasticizers
, 16 # Personal Care
, 16 # Pesticides
, 17 # Aromatic Amines
# , 16 # Phytoestrogens
, 16 # PAHs
, 15 # VOCs
, 16 # Smoking
, 16 # PFCs
, 16 # Aldehydes
, 25 ) # Other
############ Second Plot ############
# Ensure that the levels of the chemical family are in a defined order to ensure proper color scheme
wt_unadj_adj$chem_family <- factor(wt_unadj_adj$chem_family
, levels = chem_family_levels)
#this drops the units from the chemical names
wt_unadj_adj$chemical_name <- gsub("\\s\\(([^()]+)\\)$"
, ""
, wt_unadj_adj$chemical_name)
#shorten this terrible name
wt_unadj_adj$chemical_name <- gsub("N-Acetyl-S-(2-hydroxy-3-methyl-3-butenyl)-L-cysteine + N-Acetyl-S-(2-hydroxy-2-methyl-3-butenyl)-L-cysteine",
"N-Acetyl-S-(2-hydroxy-2/3-methyl-3-butenyl)-L-cysteine",
wt_unadj_adj$chemical_name,
fixed = TRUE)
#set up the order of the facets
wt_unadj_adj$immune_measure <- factor(wt_unadj_adj$immune_measure,
levels = c("Lymphocytes (1000 cells/uL)",
"Neutrophils (1000 cells/uL)",
"Monocytes (1000 cells/uL)",
"Basophils (1000 cells/uL)",
"Eosinophils (1000 cells/uL)",
"WBC (1000 cells/uL)",
"RBC (million cells/uL)",
"Mean Corpuscular Volume (fL)"))
###########################################################################################################
########################################### MAKE 2ND SCATTERPLOT ##########################################
###########################################################################################################
wt_unadj_adj_plot <-
ggplot(data = wt_unadj_adj,
aes(x = estimate_wt_nosmk,
y = estimate_wt,
color = chem_family,
shape = chem_family,
label = chemical_name)) +
geom_point(aes(col = chem_family),
size = 3) +
geom_abline(intercept = 0,
slope = 1)+
ggrepel::geom_text_repel(aes(label = chemical_name,
size = 3.5),
box.padding = unit(0.5, "lines"),
point.padding = unit(0.2, "lines"),
max.overlaps = getOption("ggrepel.max.overlaps", default = 35),
show.legend = FALSE)+
theme_bw() +
#axes
xlab('Weighted, Not Cotinine Adjusted Beta Coefficients')+
ylab('Weighted, Cotinine Adjusted Beta Coefficients')+
theme(axis.title = element_text(size = 15))+
theme(axis.text = element_text(size = 12))+
#legend
scale_color_manual(name = "Chemical Family",
values = chem_family_colors)+
scale_shape_manual(name = "Chemical Family",
values = chem_family_shapes)+
guides(color = guide_legend(nrow = 3))+
theme(legend.position = "top",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))+
guides(colour = guide_legend(override.aes = list(size = 3)))+
#facet
facet_wrap(vars(immune_measure),
ncol = 2,
nrow = 4,
scales = "free")+
theme(strip.text = element_text(size = 15,
face = "bold"))
setwd(paste0(current_directory, "/Correlation Plots - Demog, Cells, Chemicals"))
# Save the plot as a pdf for viewing at a high resolution
print("weighted cotinine vs no cotinine estimates.pdf")
ggsave(filename = "new weighted cotinine vs no cotinine estimates_new.pdf"
, plot = wt_unadj_adj_plot
, width = 14
, height = 9)
# Save the plot as a png for presentation
print("weighted cotinine vs no cotinine estimates.png")
ggsave(filename = "new weighted cotinine vs no cotinine estimates_new.png"
, plot = wt_unadj_adj_plot
, units = "in"
, width = 14
, height = 9
, dpi = 900)
setwd(current_directory)
###########################################################################################################
############################################### CORRELATIONS ##############################################
###########################################################################################################
#calculate correlations between weighted cotinine and weighted no cotinine for each set of immune measures
cor_chems <- wt_unadj_adj %>%
group_by(immune_measure) %>%
summarise(correlations = cor(estimate_wt_nosmk, estimate_wt)) %>%
ungroup()
print("correlations between weighted cotinine and no cotinine")
print(cor_chems)
print(mean(cor_chems$correlations))
# CODE FROM: https://cran.r-project.org/web/packages/broom/vignettes/broom_and_dplyr.html
#get correlations of old and new models to compare the estimates by immune measure
data_test <- wt_unadj_adj %>%
dplyr::select(-chem_family,
-celltype_codename,
-FDR_wt.x,
-FDR_wt.y)
#nest the data into lists
nested <- group_by(data_test, immune_measure) %>% nest()
# nested %>%
# mutate(test = map(data, ~ cor.test(.x$estimate_wt, .x$estimate_wt_nosmk)))
cor_df <- nested %>%
mutate(test = map(data, ~ cor.test(.x$estimate_wt,
.x$estimate_wt_nosmk,
method = "pearson")), # S3 list-col
tidied = map(test, tidy)) %>%
unnest(tidied) %>%
as.data.frame()
print("correlations and p-values")
cor_df %>%
dplyr::select(immune_measure,
estimate,
p.value) %>%
print()
}