-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRadiomicsExercises.R
More file actions
348 lines (265 loc) · 11.9 KB
/
RadiomicsExercises.R
File metadata and controls
348 lines (265 loc) · 11.9 KB
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
###############################################################################
###############################################################################
##
## Radiomics prediction Exercises
## On the basis of FMradio 1.1
## Course: Statistics for Omics: Radiomics
## Place: Amsterdam, 28/06/2019
##
## Code: Carel F.W. Peeters
## Department of Epidemiology & Biostatistics
## VU University medical center Amsterdam
## Amsterdam, the Netherlands
## cf.peeters@vumc.nl
## Date: 22/06/2019, Amsterdam, VUmc
##
###############################################################################
###############################################################################
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Preliminaries**
#' **------------------------------------------------------------------------**
## Set working directory
setwd("C:/Users/cf.peeters/Desktop/Teaching/Courses_Advanced/2019_Statistics_for_Omics/2. Radiomics/02. Exercises")
## Needed library
library(FMradio)
## Other requirements
require("Biobase")
require("rags2ridges")
require("DandEFA")
require("randomForestSRC")
require("pec")
require("survival")
## Convenience functions
source("Convenience.R")
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 1: Get acquainted with the data**
#' **------------------------------------------------------------------------**
## Data packaged as expressionset
## Will invoke basic functions for looking at data
## Load data, get to know objects
load("AnonymousRadio.Rdata")
## Look at radiomic measurements
exprs(AnonymousRadio)[10:30,1:5]
## Look at Clinical (sample) information
head(pData(AnonymousRadio))
###############################################################################
###############################################################################
##
## Section 1: Factor Analytic Projection
##
###############################################################################
###############################################################################
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 2: Assess redundancy in the correlation matrix**
#' **------------------------------------------------------------------------**
## Scale data and get raw correlation matrix
DATAscaled <- scale(t(exprs(AnonymousRadio)))
R <- cor(DATAscaled)
## Redundancy visualization
radioHeat(R, diag = FALSE,
threshold = TRUE,
threshvalue = .95,
labelsize = .01)
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 3: Redundancy-filter the correlation matrix**
#' **------------------------------------------------------------------------**
## Redundancy filtering
## And subsetting data
## 124 features remain
RFs <- RF(R, t = .95)
DATAscaledS <- subSet(DATAscaled, RFs)
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 4: Find an optimal regularized correlation matrix**
#' **------------------------------------------------------------------------**
## Optimal penalty
set.seed(303)
OPT <- regcor(DATAscaledS, fold = 5)
## Look at optimal penalty-value
## Obtain regularized correlation matrix
## Conditioning can again be assessed with, e.g., CNplot from rag2ridges
OPT$optPen
Re = OPT$optCor
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 5: Perform factor analysis on the regularized correlation matrix**
#' **------------------------------------------------------------------------**
## Assess dimensionality factor solution
## 13 considered upper bound
## Variance explained would suggest 8 factors
dimGB(Re)
dimVAR(Re, 15, graph = TRUE)
## Assessing solutions around 8
## 9th factor seems weak
## Will keep solution at 8
## ML factor analysis with Varimax rotation
fito <- mlFA(R = Re, m = 8)
print(fito$Loadings, digits = 2, cutoff = .3, sort = TRUE)
## Visualizing solution using Dandelion plot
dandpal <- rev(rainbow(100, start = 0.4, end = 0.6))
dandelion(fito$Loadings, bound = .3, mcex = c(1,1), palet = dandpal)
## Export to pdf for inspection
pdf(file = "Dandelion.pdf", width = 11, height = 11)
dandelion(fito$Loadings, bound = .3, mcex = c(1,1), palet = dandpal)
dev.off()
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 6: Obtain factor scores**
#' **------------------------------------------------------------------------**
## Factor scores
Lambda <- fito$Loadings
Psi <- fito$Uniqueness
Scores <- facScore(DATAscaledS, Lambda, Psi)
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 7: Assess factor scores**
#' **------------------------------------------------------------------------**
## Determinacy factor scores
## Highly determinate
DF <- facSMC(Re, Lambda); DF
###############################################################################
###############################################################################
##
## Section 2: Prediction
##
###############################################################################
###############################################################################
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 8: Concatenate original and projected data**
#' **------------------------------------------------------------------------**
## Combine original (scaled) data with projected meta-features
DAT <- cbind(DATAscaled, Scores)
## Include the survival information
Status <- as.numeric(AnonymousRadio$Death_yesno) - 1
time <- AnonymousRadio$Death_followuptime_months
DAT <- cbind(time, Status, DAT)
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 9: Set up model comparisons**
#' **------------------------------------------------------------------------**
## Formulating the model formula's
FitRSF <- as.formula(paste("Surv(time, Status)~",
paste(colnames(DAT)[3:434], collapse="+")))
FitMetaCox <- as.formula(paste("Surv(time, Status) ~",
paste(colnames(DAT[,c(435:442)]), collapse="+")))
models <- list("MetaCox" = coxph(FitMetaCox, data = DAT, x = TRUE, y = TRUE),
"RforestCox" = rfsrc(FitRSF, data = DAT))
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 10: Compare models w.r.t. prediction error**
#' **------------------------------------------------------------------------**
## Assessing prediction error
## Median follow-up time = 25.7
## (Averaged) repeated 5-fold cross-validation
set.seed(446464)
PredError <- pec(object = models,
formula = Surv(time, Status) ~ 1,
data = DAT,
exact = TRUE,
maxtime = median(time),
cens.model = "marginal",
splitMethod = "cv5",
B = 50,
verbose = TRUE)
## Summary results:
## Apparent and cross-validated prediction error and R2
crps(PredError)
Or2(crps(PredError))
#'#############################################################################
#'#############################################################################
#' **------------------------------------------------------------------------**
#' **Exercise 11: Visualize the results**
#' **------------------------------------------------------------------------**
## Visualize apparent prediction error
plot(PredError, what = "AppErr",
xlab = "Time (months)",
ylab = "Apparent prediction error",
legend.cex = .9,
legend.lty = 1,
legend.lwd = 2,
legend.legend = c("Reference model",
"FMradio",
"Random survival forest"),
add.refline = TRUE,
lwd = 1.5,
legend.y.intersp = 1.7)
## Visualize cross-validated prediction error
plot(PredError, what = "crossvalErr",
xlab = "Time (months)",
ylab = "Averaged cross-validated prediction error",
legend.cex = .9,
legend.lty = 1,
legend.lwd = 2,
legend.legend = c("Reference model",
"FMradio",
"Random survival forest"),
add.refline = TRUE,
lwd = 1.5,
legend.y.intersp = 1.7)
## Visualize apparent residual explained variation
R2Table <- R2(PredError, times = seq(0,median(time),.01), reference = 1)
plotR2Table(R2Table, "AE", Xlab = "Time (months)")
## Visualize cross-validated residual explained variation
plotR2Table(R2Table, "CV", Xlab = "Time (months)")
## Exporting
pdf("Internal.pdf", width = 15, height = 15)
par(mfrow=c(2,2))
## Visualize apparent prediction error
plot(PredError, what = "AppErr",
xlab = "Time (months)",
ylab = "Apparent prediction error",
legend.cex = .9,
legend.lty = 1,
legend.lwd = 2,
legend.legend = c("Reference model",
"FMradio",
"Random survival forest"),
add.refline = TRUE,
lwd = 1.5,
legend.y.intersp = 1.7)
## Visualize cross-validated prediction error
plot(PredError, what = "crossvalErr",
xlab = "Time (months)",
ylab = "Averaged cross-validated prediction error",
legend.cex = .9,
legend.lty = 1,
legend.lwd = 2,
legend.legend = c("Reference model",
"FMradio",
"Random survival forest"),
add.refline = TRUE,
lwd = 1.5,
legend.y.intersp = 1.7)
## Visualize apparent residual explained variation
R2Table <- R2(PredError, times = seq(0,median(time),.01), reference = 1)
plotR2Table(R2Table, "AE", Xlab = "Time (months)")
## Visualize cross-validated residual explained variation
plotR2Table(R2Table, "CV", Xlab = "Time (months)")
dev.off()
###############################################################################
###############################################################################
##
## Section 3: Hidden Gems
##
###############################################################################
###############################################################################
## Really?
FMradio:::.Airwolf()
## You betcha!
FMradio:::.Airwolf2()