-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTADPOLE_Train.R
421 lines (335 loc) · 16 KB
/
TADPOLE_Train.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#AdjustedFrame The datatoPredict
#Predictors the predictors
#The months for training
#numberOfRandomSamples the number of samples from the adjustedFrame
#MLMethod the Machine Learning method
#asFactor is the class should be treated as a factor
#... parameters to be passed to the ML method
#It will return models that predict if a subject will convert to MCI to AD
#it will return models that predict the time to conversion
TrainTadpoleClassModels <- function(AdjustedFrame,predictors,months=NULL,numberOfRandomSamples=5,delta=FALSE,MLMethod=BSWiMS.model,asFactor=FALSE,...)
{
AdjustedFrame$RID <- as.character(AdjustedFrame$RID)
library("FRESA.CAD")
if (is.null(months))
{
months <- as.numeric(names(table(AdjustedFrame$M)))
print(months)
}
cpredictors <- predictors
AdjustedFrame <- AdjustedFrame[order(AdjustedFrame$Years_bl),]
AdjustedFrame <- AdjustedFrame[order(as.numeric(AdjustedFrame$RID)),]
pdis <- AdjustedFrame$RID
lastTimepointSet <- AdjustedFrame[c(pdis[1:(length(pdis)-1)] != pdis[-1],TRUE),]
rownames(lastTimepointSet) <- lastTimepointSet$RID
BaseTimepointSet <- AdjustedFrame[c(TRUE,pdis[-1] != pdis[1:(length(pdis)-1)]),]
rownames(BaseTimepointSet) <- BaseTimepointSet$RID
deltaFeaturepredictors <- predictors[regexpr('_bl', predictors) < 0][-(c(1:2))]
TimePointsSubset <- list();
Orderbytimepoint <- NULL
m <- 0
i <- 1;
for (m in months)
{
TimePointsSubset[[i]] <- subset(AdjustedFrame,M == m)
rownames(TimePointsSubset[[i]]) <- TimePointsSubset[[i]]$RID
TimePointsSubset[[i]]$Year_bl_LastVisit <- lastTimepointSet[TimePointsSubset[[i]]$RID,"Years_bl"]
TimePointsSubset[[i]]$Last_DX <- lastTimepointSet[TimePointsSubset[[i]]$RID,"DX"]
TimePointsSubset[[i]]$TimeToLastVisit <- TimePointsSubset[[i]]$Year_bl_LastVisit - TimePointsSubset[[i]]$Years_bl
if (delta)
{
deltaObservations <- TimePointsSubset[[i]][,deltaFeaturepredictors] - BaseTimepointSet[rownames(TimePointsSubset[[i]]),deltaFeaturepredictors]
colnames(deltaObservations) <- paste("Delta",colnames(deltaObservations),sep="_")
TimePointsSubset[[i]] <- cbind(TimePointsSubset[[i]],deltaObservations)
}
TimePointsSubset[[i]] <- TimePointsSubset[[i]][complete.cases(TimePointsSubset[[i]][,predictors]),]
Orderbytimepoint <- rbind(Orderbytimepoint,TimePointsSubset[[i]])
i <- i + 1
}
AdjustedFrame <- Orderbytimepoint
AdjustedFrame <- AdjustedFrame[order(AdjustedFrame$Years_bl),]
AdjustedFrame <- AdjustedFrame[order(as.numeric(AdjustedFrame$RID)),]
Orderbytimepoint <- NULL
if (delta)
{
predictors <- c(predictors,colnames(deltaObservations))
}
## Get All the MCI subjects that progressed to AD
print(table(AdjustedFrame$DX))
MCISubset <- subset(AdjustedFrame,DX == "NL to MCI" | DX == "MCI" | DX == "Dementia to MCI")
MCIIDS <- unique(MCISubset$RID)
print(length(MCIIDS))
subsetMCIADConversion <- subset(AdjustedFrame,DX == "MCI to Dementia" | DX == "Dementia")
print(nrow(subsetMCIADConversion))
MCIConverters <- subsetMCIADConversion$RID %in% MCIIDS
subsetMCIADConversion <- subsetMCIADConversion[MCIConverters,]
print(nrow(subsetMCIADConversion))
pdis <- subsetMCIADConversion$RID
subsetMCIADConversion <- subsetMCIADConversion[c(TRUE,pdis[-1] != pdis[1:(length(pdis)-1)]),]
print(nrow(subsetMCIADConversion))
rownames(subsetMCIADConversion) <- subsetMCIADConversion$RID
### MCI Subset by time points
MCItoADorderbytimepoint <- NULL
for (m in months)
{
TimePointsMCISubset <- subset(MCISubset,M == m)
rownames(TimePointsMCISubset) <- TimePointsMCISubset$RID
TimePointsMCISubset$TimeToEvent <- subsetMCIADConversion[TimePointsMCISubset$RID,"Years_bl"] - TimePointsMCISubset$Years_bl
MCItoADorderbytimepoint <- rbind(MCItoADorderbytimepoint,TimePointsMCISubset)
}
controlMCIToADset <- MCItoADorderbytimepoint[is.na(MCItoADorderbytimepoint$TimeToEvent),]
controlMCIToADset <- subset(controlMCIToADset,TimeToLastVisit > 3)
hist(controlMCIToADset$TimeToLastVisit)
controlMCIToADset$TimeToEvent <- controlMCIToADset$TimeToLastVisit
caseMCIToADset <- MCItoADorderbytimepoint[!is.na(MCItoADorderbytimepoint$TimeToEvent),]
caseMCIToADset <- subset(caseMCIToADset,TimeToEvent > 0 & TimeToEvent < 5.0 )
hist(caseMCIToADset$TimeToEvent)
## MCI Modeling Set
controlMCIToADset$class <- 0
caseMCIToADset$class <- 1
MCI_to_AD_set <- rbind(controlMCIToADset,caseMCIToADset)
MCI_to_AD_set$TimeToLastVisit <- NULL
MCI_to_AD_TrainSet <- MCI_to_AD_set[MCI_to_AD_set$D1==1,]
print(table(MCI_to_AD_TrainSet$class))
## Modeling MCI conversion
print(table(MCI_to_AD_TrainSet$VISCODE))
MCI_to_ADSets <- list();
MCI_TO_AD_Model <- list();
MCI_TO_AD_TimeModel <- list();
n=1
pMCItoADEvent <- 0;
for (n in 1:numberOfRandomSamples)
{
randomnumber <- sample(1:nrow(MCI_to_AD_TrainSet),nrow(MCI_to_AD_TrainSet))
MCI_to_AD_RandomSet <- MCI_to_AD_TrainSet[randomnumber,]
MCI_to_AD_RandomSet <- MCI_to_AD_RandomSet[order(as.numeric(MCI_to_AD_RandomSet$RID)),]
RID <- MCI_to_AD_RandomSet$RID
set1 <- MCI_to_AD_RandomSet[c(RID[1:length(RID)-1] != RID[-1],TRUE),]
rownames(set1) <- set1$RID
set1 <- set1[complete.cases(set1),]
print(nrow(set1))
print(table(set1$class))
pMCItoADEvent <- pMCItoADEvent + sum(set1$class)/nrow(set1)
MCI_to_ADSets[[n]] <- set1[,c("class",predictors)]
if (asFactor)
{
MCI_to_ADSets[[n]]$class <- as.factor(MCI_to_ADSets[[n]]$class)
}
MCI_TO_AD_Model[[n]] <- MLMethod(class ~ .,MCI_to_ADSets[[n]],...)
sm <- summary(MCI_TO_AD_Model[[n]])
print(sm$tAUC)
MCI_TO_AD_Model[[n]]$BSWiMS.model$bootCV$data <- NULL
MCI_TO_AD_Model[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
MCI_TO_AD_Model[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
set1 <- subset(set1,class==1)
print(nrow(set1))
MCI_to_ADSets[[n]] <- set1[,c("TimeToEvent",predictors)]
MCI_to_ADSets[[n]]$TimeToEvent <- set1$TimeToEvent
MCI_TO_AD_TimeModel[[n]] <- MLMethod(TimeToEvent ~ .,MCI_to_ADSets[[n]],...)
MCI_TO_AD_TimeModel[[n]]$univariate <- NULL
MCI_TO_AD_TimeModel[[n]]$BSWiMS.model$bootCV$data <- NULL
MCI_TO_AD_TimeModel[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
MCI_TO_AD_TimeModel[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
}
pMCItoADEvent <- pMCItoADEvent/numberOfRandomSamples
## Get All the MCI subjects that progressed to NC
subsetMCINCConversion <- subset(AdjustedFrame,DX == "MCI to NL" | DX == "NL")
print(nrow(subsetMCIADConversion))
MCIConverters <- subsetMCINCConversion$RID %in% MCIIDS
subsetMCINCConversion <- subsetMCINCConversion[MCIConverters,]
print(nrow(subsetMCINCConversion))
pdis <- subsetMCINCConversion$RID
subsetMCINCConversion <- subsetMCINCConversion[c(TRUE,pdis[-1] != pdis[1:(length(pdis)-1)]),]
print(nrow(subsetMCINCConversion))
rownames(subsetMCINCConversion) <- subsetMCINCConversion$RID
### MCI Subset by time points
MCIToNCorderbytimepoint <- NULL
for (m in months)
{
TimePointsMCISubset <- subset(MCISubset,M == m)
rownames(TimePointsMCISubset) <- TimePointsMCISubset$RID
TimePointsMCISubset$TimeToEvent <- subsetMCINCConversion[TimePointsMCISubset$RID,"Years_bl"] - TimePointsMCISubset$Years_bl
MCIToNCorderbytimepoint <- rbind(MCIToNCorderbytimepoint,TimePointsMCISubset)
}
controlMCIToNCset <- MCIToNCorderbytimepoint[is.na(MCIToNCorderbytimepoint$TimeToEvent),]
controlMCIToNCset <- subset(controlMCIToNCset,TimeToLastVisit > 3)
hist(controlMCIToNCset$TimeToLastVisit)
controlMCIToNCset$TimeToEvent <- controlMCIToNCset$TimeToLastVisit
caseMCIToNCset <- MCIToNCorderbytimepoint[!is.na(MCIToNCorderbytimepoint$TimeToEvent),]
caseMCIToNCset <- subset(caseMCIToNCset,TimeToEvent > 0 & TimeToEvent < 5.0 )
hist(caseMCIToNCset$TimeToEvent)
## MCI Modeling Set
controlMCIToNCset$class <- 0
caseMCIToNCset$class <- 1
MCI_to_NC_set <- rbind(controlMCIToNCset,caseMCIToNCset)
MCI_to_NC_set$TimeToLastVisit <- NULL
MCI_to_NC_TrainSet <- MCI_to_NC_set[MCI_to_NC_set$D1==1,]
print(table(MCI_to_NC_TrainSet$class))
## Modeling MCI conversion
print(table(MCI_to_NC_TrainSet$VISCODE))
MCI_to_NCSets <- list();
MCI_TO_NC_Model <- list();
MCI_TO_NC_TimeModel <- list();
n=1
pMCItoNCEvent <- 0;
for (n in 1:numberOfRandomSamples)
{
randomnumber <- sample(1:nrow(MCI_to_NC_TrainSet),nrow(MCI_to_NC_TrainSet))
MCI_to_NC_RandomSet <- MCI_to_NC_TrainSet[randomnumber,]
MCI_to_NC_RandomSet <- MCI_to_NC_RandomSet[order(as.numeric(MCI_to_NC_RandomSet$RID)),]
RID <- MCI_to_NC_RandomSet$RID
set1 <- MCI_to_NC_RandomSet[c(RID[1:length(RID)-1] != RID[-1],TRUE),]
rownames(set1) <- set1$RID
set1 <- set1[complete.cases(set1),]
print(nrow(set1))
print(table(set1$class))
pMCItoNCEvent <- pMCItoNCEvent + sum(set1$class)/nrow(set1)
MCI_to_NCSets[[n]] <- set1[,c("class",predictors)]
if (asFactor)
{
MCI_to_NCSets[[n]]$class <- as.factor(MCI_to_NCSets[[n]]$class)
}
MCI_TO_NC_Model[[n]] <- MLMethod(class ~ .,MCI_to_NCSets[[n]],...)
MCI_TO_NC_Model[[n]]$BSWiMS.model$bootCV$data <- NULL
MCI_TO_NC_Model[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
MCI_TO_NC_Model[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
sm <- summary(MCI_TO_NC_Model[[n]])
print(sm$tAUC)
set1 <- subset(set1,class==1)
print(nrow(set1))
MCI_to_NCSets[[n]] <- set1[,c("TimeToEvent",predictors)]
MCI_to_NCSets[[n]]$TimeToEvent <- set1$TimeToEvent
MCI_TO_NC_TimeModel[[n]] <- MLMethod(TimeToEvent ~ .,MCI_to_NCSets[[n]],...)
MCI_TO_NC_TimeModel[[n]]$BSWiMS.model$bootCV$data <- NULL
MCI_TO_NC_TimeModel[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
MCI_TO_NC_TimeModel[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
}
pMCItoNCEvent <- pMCItoNCEvent/numberOfRandomSamples
## Get All the NC subjects that progressed
print(table(AdjustedFrame$DX))
NCSubset <- subset(AdjustedFrame,DX == "NL" | DX == "MCI to NL")
NCIDS <- unique(NCSubset$RID)
print(length(NCIDS))
subsetNCConvConversion <- subset(AdjustedFrame,DX == "NL to Dementia" | DX == "NL to MCI" | DX == "MCI")
print(nrow(subsetNCConvConversion))
MCIConverters <- subsetNCConvConversion$RID %in% NCIDS
subsetNCConvConversion <- subsetNCConvConversion[MCIConverters,]
print(nrow(subsetNCConvConversion))
pdis <- subsetNCConvConversion$RID
subsetNCConvConversion <- subsetNCConvConversion[c(TRUE,pdis[-1] != pdis[1:(length(pdis)-1)]),]
print(nrow(subsetNCConvConversion))
rownames(subsetNCConvConversion) <- subsetNCConvConversion$RID
### NC Subset by time points
NCConvorderbytimepoint <- NULL
for (m in months)
{
TimePointsNCSubset <- subset(NCSubset,M == m)
rownames(TimePointsNCSubset) <- TimePointsNCSubset$RID
TimePointsNCSubset$TimeToEvent <- subsetNCConvConversion[TimePointsNCSubset$RID,"Years_bl"] - TimePointsNCSubset$Years_bl
NCConvorderbytimepoint <- rbind(NCConvorderbytimepoint,TimePointsNCSubset)
}
controlNCConvset <- NCConvorderbytimepoint[is.na(NCConvorderbytimepoint$TimeToEvent),]
controlNCConvset <- subset(controlNCConvset, TimeToLastVisit > 3)
hist(controlNCConvset$TimeToLastVisit)
controlNCConvset$TimeToEvent <- controlNCConvset$TimeToLastVisit
caseNCConvset <- NCConvorderbytimepoint[!is.na(NCConvorderbytimepoint$TimeToEvent),]
caseNCConvset <- subset(caseNCConvset,TimeToEvent > 0 & TimeToEvent < 5.0)
hist(caseNCConvset$TimeToEvent)
## Modeling Nomal congitive Set
controlNCConvset$class <- 0
caseNCConvset$class <- 1
NCConv_set <- rbind(controlNCConvset,caseNCConvset)
NCConv_TrainSet <- NCConv_set[NCConv_set$D1==1,]
table(NCConv_TrainSet$class)
table(NCConv_TrainSet$VISCODE)
NCConvSets <- list();
NCConv_Model <- list();
NL_TO_OTHER_TimeModel <- list();
n=1
pNCtoMCIEvent <- 0;
for (n in 1:numberOfRandomSamples)
{
randomnumber <- sample(1:nrow(NCConv_TrainSet),nrow(NCConv_TrainSet))
NCConv_RandomSet <- NCConv_TrainSet[randomnumber,]
NCConv_RandomSet <- NCConv_RandomSet[order(as.numeric(NCConv_RandomSet$RID)),]
RID <- NCConv_RandomSet$RID
set1 <- NCConv_RandomSet[c(RID[1:length(RID)-1] != RID[-1],TRUE),]
rownames(set1) <- set1$RID
set1 <- set1[complete.cases(set1),]
print(nrow(set1))
print(table(set1$class))
pNCtoMCIEvent <- pNCtoMCIEvent + sum(set1$class)/nrow(set1)
NCConvSets[[n]] <- set1[,c("class",predictors)]
if (asFactor)
{
NCConvSets[[n]]$class <- as.factor(NCConvSets[[n]]$class)
}
NCConv_Model[[n]] <- MLMethod(class ~ .,NCConvSets[[n]],...)
NCConv_Model[[n]]$BSWiMS.model$bootCV$data <- NULL
NCConv_Model[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
NCConv_Model[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
sm <- summary(NCConv_Model[[n]])
print(sm$tAUC)
set1 <- subset(set1,class==1)
print(nrow(set1))
NCConvSets[[n]] <- set1[,c("TimeToEvent",predictors)]
NCConvSets[[n]]$TimeToEvent <- set1$TimeToEvent
NL_TO_OTHER_TimeModel[[n]] <- MLMethod(TimeToEvent ~ .,NCConvSets[[n]],...)
NL_TO_OTHER_TimeModel[[n]]$BSWiMS.model$bootCV$data <- NULL
NL_TO_OTHER_TimeModel[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
NL_TO_OTHER_TimeModel[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
}
pNCtoMCIEvent <- pNCtoMCIEvent/numberOfRandomSamples
## Cross Sectional Modeling
### Baseline Modeling Set
class <-
2*(AdjustedFrame$DX == "Dementia" | AdjustedFrame$DX == "MCI to Dementia" | AdjustedFrame$DX == "NL to Dementia") +
1*(AdjustedFrame$DX == "Dementia to MCI" | AdjustedFrame$DX == "MCI" | AdjustedFrame$DX == "NL to MCI")
AdjustedFrame$class <- class
AllADNISets <- list();
AllADNI_Model <- list();
n=1
for (n in 1:numberOfRandomSamples)
{
randomnumber <- sample(1:nrow(AdjustedFrame),nrow(AdjustedFrame))
AllADNI_RandomSet <- AdjustedFrame[randomnumber,]
AllADNI_RandomSet <- AllADNI_RandomSet[order(as.numeric(AllADNI_RandomSet$RID)),]
RID <- AllADNI_RandomSet$RID
set1 <- AllADNI_RandomSet[c(RID[1:length(RID)-1] != RID[-1],TRUE),]
rownames(set1) <- set1$RID
AllADNISets[[n]] <- set1[,c("class",cpredictors)]
AllADNISets[[n]] <- AllADNISets[[n]][complete.cases(AllADNISets[[n]]),]
print(nrow(AllADNISets[[n]]))
print(table(set1$DX_bl,set1$class))
if (asFactor)
{
AllADNISets[[n]]$class <- as.factor(AllADNISets[[n]]$class)
}
AllADNI_Model[[n]] <- MLMethod(class ~ .,AllADNISets[[n]],...)
AllADNI_Model[[n]]$BSWiMS.model$bootCV$data <- NULL
AllADNI_Model[[n]]$BSWiMS.model$bootCV$testOutcome <- NULL
AllADNI_Model[[n]]$BSWiMS.model$bootCV$testPrediction <- NULL
AllADNI_Model[[n]]$oridinalModels$data <- NULL
AllADNI_Model[[n]]$oridinalModels$theClassBaggs[[1]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$theClassBaggs[[2]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$theClassBaggs[[3]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$theBaggedModels[[1]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$theBaggedModels[[2]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$redBaggedModels[[1]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$redBaggedModels[[2]]$bagged.model$model <- NULL
AllADNI_Model[[n]]$oridinalModels$polr <- NULL
}
predicitionModels <- list(CrossModels = AllADNI_Model,
MCIToADModels=MCI_TO_AD_Model,
MCIToADTimeModel = MCI_TO_AD_TimeModel,
MCIToNCModels=MCI_TO_NC_Model,
MCIToNCTimeModel = MCI_TO_NC_TimeModel,
NCToMCIModel=NCConv_Model,
NCToMCITimeModel=NL_TO_OTHER_TimeModel,
predictors = cpredictors,
pMCItoADEvent = pMCItoADEvent,
pNCtoMCIEvent = pNCtoMCIEvent,
pMCItoNCEvent = pMCItoNCEvent
)
return (predicitionModels)
}