-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-2.glm_pediatric_harmo(cov).R
396 lines (322 loc) · 16 KB
/
5-2.glm_pediatric_harmo(cov).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
##################################################################
## ENIGMA-OCD pediatric GLM ##
##################################################################
##########################################################################
########## 1. load data ##########
##########################################################################
###### load prediction probability data
setwd("~/Library/Mobile Documents/com~apple~CloudDocs/connectome21/2021-1/project/ENIGMA-OCD/final/4.h2oDAI/2.pediatric/2.NeuroCombat/After piras updated_Pediatric_21.12.22/Pedi_dx_harmo.cov")
train_pred <- read.csv("h2oai_experiment_ped_Dx_harmo.cov_21.12.22_train_predictions.csv")
test_pred <- read.csv("h2oai_experiment_ped_Dx_harmo.cov_21.12.22_test_predictions.csv")
##########################################################################
########## 2. data preprocessing ##########
##########################################################################
##### preprocessing for the analysis - factorization
train_varName_fact <- c(which(names(train_pred) == 'Dx'), which(names(train_pred) == 'Site'), which(names(train_pred) == 'Sex'),
which(names(train_pred) == 'Med'), which(names(train_pred) == 'Agr_Check'), which(names(train_pred) == 'Clean'),
which(names(train_pred) == 'Sex_Rel'),which(names(train_pred) == 'Hoard'), which(names(train_pred) == 'Ord'),
which(names(train_pred) == 'Anx'), which(names(train_pred) == 'CurAnx'), which(names(train_pred) == 'Dep'),
which(names(train_pred) == 'CurDep'), which(names(train_pred) == 'site_int'))
train_pred[,train_varName_fact] = lapply(train_pred[,train_varName_fact], factor)
test_varName_fact <- c(which(names(test_pred) == 'Dx'), which(names(test_pred) == 'Site'), which(names(test_pred) == 'Sex'),
which(names(test_pred) == 'Med'), which(names(test_pred) == 'Agr_Check'), which(names(test_pred) == 'Clean'),
which(names(test_pred) == 'Sex_Rel'), which(names(test_pred) == 'Hoard'), which(names(test_pred) == 'Ord'),
which(names(test_pred) == 'Anx'), which(names(test_pred) == 'CurAnx'), which(names(test_pred) == 'Dep'),
which(names(test_pred) == 'CurDep'), which(names(test_pred) == 'site_int'))
test_pred[,test_varName_fact] = lapply(test_pred[,test_varName_fact], factor)
##### missing values / re-coding
library(dplyr)
# 999
train_pred[train_pred == 999] <- NA
test_pred[test_pred == 999] <- NA
##### filtering by Dx group
train_pred_HC <- train_pred %>% filter(Dx == 0)
train_pred_OCD <- train_pred %>% filter(Dx == 1)
test_pred_HC <- test_pred %>% filter(Dx == 0)
test_pred_OCD <- test_pred %>% filter(Dx == 1)
##### missing value processing based on ENIGMA-OCD clinical criteria
# [HC] train_pred_HC, test_pred_HC
# MED=0 / AO - / Dur - / Sev - / Agr_Check - / Clean - / Sex_Rel - / Hoard - / Ord - / Anx 0 / CurAnx 0 / Dep 0 / CurDep 0
# -
# [OCD] train_pred_OCD, test_pred_OCD
train_pred_OCD$Med[train_pred_OCD$Med == 0] <- NA
train_pred_OCD$Anx[train_pred_OCD$Anx == 0] <- NA
train_pred_OCD$Dep[train_pred_OCD$Dep == 0] <- NA
train_pred_OCD$CurAnx[train_pred_OCD$CurAnx == 0] <- NA
train_pred_OCD$CurDep[train_pred_OCD$CurDep == 0] <- NA
train_pred_OCD$Agr_Check[train_pred_OCD$Agr_Check == 999] <- NA
train_pred_OCD$Clean[train_pred_OCD$Clean == 999] <- NA
train_pred_OCD$Ord[train_pred_OCD$Ord == 999] <- NA
train_pred_OCD$Sex_Rel[train_pred_OCD$Sex_Rel == 999] <- NA
train_pred_OCD$Hoard[train_pred_OCD$Hoard == 999] <- NA
test_pred_OCD$Med[test_pred_OCD$Med == 0] <- NA
test_pred_OCD$Anx[test_pred_OCD$Anx == 0] <- NA
test_pred_OCD$Dep[test_pred_OCD$Dep == 0] <- NA
test_pred_OCD$CurAnx[test_pred_OCD$CurAnx == 0] <- NA
test_pred_OCD$CurDep[test_pred_OCD$CurDep == 0] <- NA
test_pred_OCD$Agr_Check[test_pred_OCD$Agr_Check == 999] <- NA
test_pred_OCD$Clean[test_pred_OCD$Clean == 999] <- NA
test_pred_OCD$Ord[test_pred_OCD$Ord == 999] <- NA
test_pred_OCD$Sex_Rel[test_pred_OCD$Sex_Rel == 999] <- NA
test_pred_OCD$Hoard[test_pred_OCD$Hoard == 999] <- NA
train_pred_OCD <- droplevels(train_pred_OCD)
test_pred_OCD <- droplevels(test_pred_OCD)
###### normality test before the analysis
# normality test (O -> t-test ok)
# in train set
shapiro.test(train_pred$Dx.1) # W = 0.98034, p-value = 0.001423 (*high p-value means normality OK)
qqnorm(train_pred$Dx.1)
qqline(train_pred$Dx.1)
shapiro.test(train_pred_HC$Dx.1) # W = 0.97195, p-value = 0.01767
qqnorm(train_pred_HC$Dx.1)
qqline(train_pred_HC$Dx.1)
shapiro.test(train_pred_OCD$Dx.1) # W = 0.97888, p-value = 0.02885
qqnorm(train_pred_OCD$Dx.1)
qqline(train_pred_OCD$Dx.1)
# in test set
shapiro.test(test_pred$Dx.1) # W = 0.96363, p-value = 0.05626
qqnorm(test_pred$Dx.1)
qqline(train_pred$Dx.1)
shapiro.test(test_pred_HC$Dx.1) # W = 0.975, p-value = 0.7006
qqnorm(test_pred_HC$Dx.1)
qqline(test_pred_HC$Dx.1)
shapiro.test(test_pred_OCD$Dx.1) # W = 0.9431, p-value = 0.06957
qqnorm(test_pred_OCD$Dx.1)
qqline(test_pred_OCD$Dx.1)
##########################################################################
########## 3. sample characteristics ##########
##########################################################################
##### description analysis of "Dx.1" (prediction probability) by "dx (diagnosis)" group
train_pred %>%
group_by(Dx) %>%
summarise(mean_value = mean(Dx.1, na.rm = T), sd_value = sd(Dx.1, na.rm = T))
test_pred %>%
group_by(Dx) %>%
summarise(mean_value = mean(Dx.1, na.rm = T), sd_value = sd(Dx.1, na.rm = T))
##########################################################################
########## 4. association between clinical variables and Preds ##########
##########################################################################
# Dx.1 ~ Med + Sex + Age + Site + AverageFA + AverageMD + AverageRD + AverageAD
### demo (age + sex + ...)
agesex_1 <- glm(Dx.1 ~ Age + Sex + Dx + Site, data = train_pred)
summary(agesex_1)
aov <- aov(agesex_1)
summary(aov)
### clinical association
## set (1) predictors & (2) covariates
#################### train set ####################
predictors_i_1 <- colnames(train_pred_OCD)[8:20] # (1) predictors # ****
#[predictors_i_1] Med, AO, Dur, Sev, Agr_Check, Clean, Sex_Rel, Hoard, Ord, Anx, CurAnx, Dep, CurDep
covars = c("Sex", "Age", "Site", "AverageFA", "AverageMD", "AverageRD", "AverageAD") # (2) covariates
## make formula
forms = paste('Dx.1 ~', predictors_i_1) # ****
for(i in 1:length(covars)){
forms = paste(forms, '+', covars[i])
}
## empty vector for coeff, p-value
temp.names <- vector()
name_lm <- vector()
temp.names <- vector()
name_aov <- vector()
#glm
temp.coeff <- vector()
temp.p <- vector()
#aov
temp.aov <- vector()
temp.DF <- vector()
temp.MeanSq <- vector()
temp.F <- vector()
temp.Pr <- vector()
## loop for glm w/ multiple variables
for(i in 1:length(forms)){
form = as.formula(forms[i])
# glm
temp.lm <- lm(formula = form, data = train_pred_OCD) # **** train_pred_OCD ***
name_lm[i] <- predictors_i_1[i]
temp.coeff[i] <- round(temp.lm[[1]][2], 3)
temp.p[i] <- round(coef(summary(temp.lm))[2,4], 3)
fdr <- round(p.adjust(temp.p, method = 'fdr'), 3)
glm_coeff_train <- data.frame(y = name_lm, beta = temp.coeff, p = temp.p, fdr)
# aov
temp.aov <- anova(temp.lm) #ancova
name_aov[i] <- predictors_i_1[i]
#temp.DF[i] <- round(temp.aov[1,1],5)
#temp.MeanSq[i] <- round(temp.aov[[3]][1],5)
temp.F[i] <- round(temp.aov[1,4],3)
temp.Pr[i] <- round(temp.aov[1,5],3)
fdr_aov <- round(p.adjust(temp.Pr, method = 'fdr'), 3)
aov_coeff_train <- data.frame(y = name_aov, f.value = temp.F, Pr = temp.Pr, fdr_aov)
}
View(glm_coeff_train)
View(aov_coeff_train)
#################### test set ####################
predictors_i_2 <- colnames(test_pred_OCD)[c(8:19)] # (1) predictors # ****
#[predictors_i_2] Med, AO, Dur, Sev, Agr_Check, Clean, Sex_Rel, Hoard, Ord, Anx, CurAnx, Dep
#CurDep level - only 1
covars = c("Sex", "Age", "Site", "AverageFA", "AverageMD", "AverageRD", "AverageAD") # (2) covariates
print(covars)
## make formula
forms = paste('Dx.1 ~', predictors_i_2) # ****
for(i in 1:length(covars)){
forms = paste(forms, '+', covars[i])
}
## empty vector for coeff, p-value
temp.names <- vector()
name_lm <- vector()
temp.names <- vector()
name_aov <- vector()
#glm
temp.coeff <- vector()
temp.p <- vector()
#aov
temp.aov <- vector()
temp.DF <- vector()
temp.MeanSq <- vector()
temp.F <- vector()
temp.Pr <- vector()
## loop for glm w/ multiple variables
for(i in 1:length(forms)){
form = as.formula(forms[i])
# glm
temp.lm <- lm(formula = form, data = test_pred_OCD) # **** train_pred_OCD ***
name_lm[i] <- predictors_i_2[i]
temp.coeff[i] <- round(temp.lm[[1]][2], 3)
temp.p[i] <- round(coef(summary(temp.lm))[2,4], 3)
fdr <- p.adjust(temp.p, method = 'fdr')
glm_coeff_test <- data.frame(y = name_lm, beta = temp.coeff, p = temp.p, fdr)
# aov
temp.aov <- anova(temp.lm) #ancova
name_aov[i] <- predictors_i_1[i]
#temp.DF[i] <- round(temp.aov[1,1],5)
#temp.MeanSq[i] <- round(temp.aov[[3]][1],5)
temp.F[i] <- round(temp.aov[1,4],3)
temp.Pr[i] <- round(temp.aov[1,5],3)
fdr_aov <- round(p.adjust(temp.Pr, method = 'fdr'), 3)
aov_coeff_test <- data.frame(y = name_aov, f.value = temp.F, Pr = temp.Pr, fdr_aov)
}
View(glm_coeff_test)
View(aov_coeff_test)
##########################################################################
########## 5. averageFA/MD/AD/RD across site (site variability) ##########
##########################################################################
#################### train set ####################
siteFA <- glm(AverageFA ~ Site + Sex + Age, data = train_pred_OCD) #train_pred_HC / train_pred_OCD
siteMD <- glm(AverageMD ~ Site + Sex + Age, data = train_pred_OCD) #train_pred_HC / train_pred_OCD
siteAD <- glm(AverageAD ~ Site + Sex + Age, data = train_pred_OCD) #train_pred_HC / train_pred_OCD
siteRD <- glm(AverageRD ~ Site + Sex + Age, data = train_pred_OCD) #train_pred_HC / train_pred_OCD
aov_siteFA <- aov(siteFA)
aov_siteMD <- aov(siteMD)
aov_siteAD <- aov(siteAD)
aov_siteRD <- aov(siteRD)
summary(aov_siteFA)
summary(aov_siteMD)
summary(aov_siteAD)
summary(aov_siteRD)
#################### test set ####################
siteFA <- glm(AverageFA ~ Site + Sex + Age, data = test_pred_OCD) #test_pred_HC / test_pred_OCD
siteMD <- glm(AverageMD ~ Site + Sex + Age, data = test_pred_OCD) #test_pred_HC / test_pred_OCD
siteAD <- glm(AverageAD ~ Site + Sex + Age, data = test_pred_OCD) #test_pred_HC / test_pred_OCD
siteRD <- glm(AverageRD ~ Site + Sex + Age, data = test_pred_OCD) #test_pred_HC / test_pred_OCD
aov_siteFA <- aov(siteFA)
aov_siteMD <- aov(siteMD)
aov_siteAD <- aov(siteAD)
aov_siteRD <- aov(siteRD)
summary(aov_siteFA)
summary(aov_siteMD)
summary(aov_siteAD)
summary(aov_siteRD)
##########################################################################
########## 6-1. [train] individual performance across site (site variability) ##########
##########################################################################
#################### mutate threshold ####################
library(dplyr)
train_pred <- train_pred %>%
mutate(threshold = ifelse(Site == 'Bangalore', 0.4313105,
ifelse(Site == 'Barcelona', 0.559111,
ifelse(Site == 'British Columbia', 0.3878554,
ifelse(Site == 'Calgary', 0.5781595,
ifelse(Site == 'Chiba', 0.4980515,
ifelse(Site == 'Oxford', 0.5619532,
ifelse(Site == 'Yale', 0.4215366,
ifelse(Site == 'Zurich', 0.5133553, NA)))))))))
#################### check data ####################
str(train_pred$threshold)
hist(train_pred$threshold)
sum(is.na(train_pred$threshold)) # 0
train_pred %>%
xtabs(~ Site, addNA = TRUE, data = .)
#################### create confusion matrix variable ####################
train_pred <- train_pred %>%
mutate(Dx_predicted = ifelse(Dx.1 >= threshold, 1, 0))
train_pred <- train_pred %>%
mutate(ConfusionMatrix = ifelse(Dx_predicted == 0 & Dx == 0, 'TN',
ifelse(Dx_predicted ==1 & Dx == 1, 'TP',
ifelse(Dx_predicted == 0 & Dx == 1, 'FN',
ifelse(Dx_predicted == 1 & Dx == 0, 'FP', NA)))))
#################### check TRUE / FALSE ####################
# True prediction [proba_val] - TN, TP = 1, FN, FP = 0 / False_prediction_total - (reverse)
train_pred <- train_pred %>%
mutate(True_prediction_total = ifelse(ConfusionMatrix == 'TN' | ConfusionMatrix == 'TP', 1, 0),
False_prediction_total = ifelse(ConfusionMatrix == 'FN' | ConfusionMatrix == 'FP', 1, 0))
train_pred %>%
xtabs(~ ConfusionMatrix + True_prediction_total, data = ., addNA = T)
train_pred %>%
xtabs(~ ConfusionMatrix + False_prediction_total, data = ., addNA = T)
#################### OCD
# True prediction_OCD [proba_val_ocd] - TP = 1, FN = 0 / False_prediction_OCD - FN = 1, TP = 0
train_pred_OCD_x <- train_pred %>% filter(Dx == 1)
table(train_pred_OCD_x$ConfusionMatrix) # there's only FN or TP
train_pred_OCD_x <- train_pred_OCD_x %>%
mutate(True_prediction_OCD = ifelse(ConfusionMatrix == 'TP', 1, 0))
train_pred_OCD_x<- train_pred_OCD_x %>%
mutate(False_prediction_OCD = ifelse(ConfusionMatrix == 'FN', 1, 0))
# check
table(train_pred_OCD_x$True_prediction_OCD)
table(train_pred_OCD_x$False_prediction_OCD)
addmargins(xtabs(formula = ~ Site + True_prediction_OCD, data = train_pred_OCD_x)) #
#################### HC
# True prediction_HC [proba_val_hc] - TN = 1, FP =0 / False_prediction_HC - FP = 1, TN = 0
train_pred_HC_x <- train_pred %>% filter(Dx == 0)
table(train_pred_HC_x$ConfusionMatrix) # there's only FP or TN
train_pred_HC_x <- train_pred_HC_x %>%
mutate(True_prediction_HC = ifelse(ConfusionMatrix == 'TN', 1, 0))
train_pred_HC_x <- train_pred_HC_x %>%
mutate(False_prediction_HC = ifelse(ConfusionMatrix == 'FP', 1, 0))
# check
table(train_pred_HC_x$True_prediction_HC)
table(train_pred_HC_x$False_prediction_HC)
addmargins(xtabs(formula = ~ Site + True_prediction_HC, data = train_pred_HC_x)) #
######################
# That is, even when adjusting for covariates (age, sex, site, and mean DTI metrics (FA, MD, AD, RD) , the site variability in classification performance persisted in OCD patients (????2 = 57.194, p< .001) and HCs (????2 = 50.303, p< .001).
# 1. In OCD
# covariate model
# remove Average MD, RD due to vif issue
OCD_model.cov_demo2.brain2.clinical3 <- train_pred_OCD_x %>%
mutate(Med_12 = as.factor(ifelse(Med == 2, 2, ifelse(Med == 1, 1, NA)))) %>%
glm(True_prediction_OCD ~ Age + Sex + AverageFA + AverageAD + Sev + Dur+ Med_12,
family = binomial(link="logit"), data = .)
summary(OCD_model.cov_demo2.brain2.clinical3)
# covariate + site model
OCD_model.cov_demo2.brain2.clinical3_site <- train_pred_OCD_x %>%
mutate(Med_12 = as.factor(ifelse(Med == 2, 2, ifelse(Med == 1, 1, NA)))) %>%
glm(True_prediction_OCD ~ Age + Sex + AverageFA + AverageAD + Sev + Dur+ Med_12 + Site,
family = binomial(link="logit"), data = .)
summary(OCD_model.cov_demo2.brain2.clinical3_site)
# check vif
car::vif(OCD_model.cov_demo2.brain2.clinical3)
car::vif(OCD_model.cov_demo2.brain2.clinical3_site)
# site variability test
anova(OCD_model.cov_demo2.brain2.clinical3, OCD_model.cov_demo2.brain2.clinical3_site, test = 'Chisq') # chisq test : because of logistic regression
# 2. In HC
# covariate model
HC_model.cov_demo2.brain2 <- train_pred_HC_x %>%
glm(True_prediction_HC ~ Age + Sex + AverageFA + AverageAD,
family = binomial(link = 'logit'),data = .)
summary(HC_model.cov_demo2.brain2)
# covariate + site model
HC_model.cov_demo2.brain2_site <- train_pred_HC_x %>%
glm(True_prediction_HC ~ Age + Sex + AverageFA + AverageAD + Site,
family = binomial(link = 'logit'),data = .)
summary(HC_model.cov_demo2.brain2_site)
# site variability test
anova(HC_model.cov_demo2.brain2, HC_model.cov_demo2.brain2_site, test = 'Chisq') # chisq test : because of logistic regression