-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmccv_power.qmd
465 lines (405 loc) · 14.5 KB
/
mccv_power.qmd
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
---
title: "Estimating Power"
format:
html:
code-fold: true
code-summary: 'Show The Code'
---
One of the advantages of using MCCV is it's data-driven approach. Given data, you can answer questions such as:
1. How predictive is Y from X?
2. Is a certain cut of the data driving the prediction?
3. Is there another variable obscuring or influencing the contribution of X in predicting Y?
In this article I want to illustrate another type of question MCCV can address:
- How does the prediction of Y by X compare to the prediction by X to a random Y? In other words, what is the power of my data X to predict Y?
I will discuss at the end how the term 'power' is used here in comparison to the statistical and more common form of the term. First, I will show two examples showing the power of X to predict Y.
This first example shows low power
```{python}
#| warning: false
import numpy as np
N=100
X1 = np.random.normal(loc=0,scale=1,size=N)
X2 = np.random.normal(loc=0.5,scale=1,size=N)
X = np.concatenate([X1,X2])
Y = np.concatenate([np.repeat(0,N),np.repeat(1,N)])
import pandas as pd
df = pd.DataFrame(data={'Y' : Y,'X' : X})
df.index.name = 'pt'
```
```{r}
#| warning: false
library(tidyverse)
df <- reticulate::py$df
df[['Y']] <- factor(df$Y,levels=c(0,1))
df %>%
ggplot(aes(Y,X)) +
geom_boxplot(outlier.size = NA,alpha=0,linewidth=2) +
geom_point(position = position_jitter(width = .2),pch=21,fill='gray',size=3) +
labs(x = "Response",y="Predictor",title = "Predictor values for Class One are 50% larger on average") +
theme_bw(base_size = 16)
```
```{python}
import mccv
mccv_obj = mccv.mccv(num_bootstraps=200,n_jobs=4)
mccv_obj.set_X(df[['X']])
mccv_obj.set_Y(df[['Y']])
mccv_obj.run_mccv()
mccv_obj.mccv_data['Feature Importance'].insert(
len(mccv_obj.mccv_data['Feature Importance'].columns),
'type',
'real'
)
mccv_obj.mccv_data['Model Learning'].insert(
len(mccv_obj.mccv_data['Model Learning'].columns),
'type',
'real'
)
mccv_obj.mccv_data['Patient Predictions'].insert(
len(mccv_obj.mccv_data['Patient Predictions'].columns),
'type',
'real'
)
mccv_obj.run_permuted_mccv()
mccv_obj.mccv_permuted_data['Feature Importance'].insert(
len(mccv_obj.mccv_permuted_data['Feature Importance'].columns),
'type',
'permuted'
)
mccv_obj.mccv_permuted_data['Model Learning'].insert(
len(mccv_obj.mccv_permuted_data['Model Learning'].columns),
'type',
'permuted'
)
mccv_obj.mccv_permuted_data['Patient Predictions'].insert(
len(mccv_obj.mccv_permuted_data['Patient Predictions'].columns),
'type',
'permuted'
)
fimp_df = \
pd.concat([
(mccv_obj.mccv_data['Feature Importance'].
query('feature=="X"').
reset_index(drop=True)),
(mccv_obj.mccv_permuted_data['Feature Importance'].
query('feature=="X"').
reset_index(drop=True))
])
ppred_df = \
pd.concat([
(mccv_obj.mccv_data['Patient Predictions'].
drop('y_pred',axis=1).
reset_index()),
(mccv_obj.mccv_permuted_data['Patient Predictions'].
reset_index().
drop('y_pred',axis=1))
])
pred_df = \
pd.concat([
(mccv_obj.mccv_data['Model Learning'].
reset_index()),
(mccv_obj.mccv_permuted_data['Model Learning'].
reset_index())
])
```
```{r}
#| warning: false
pks <-
ks.test(
reticulate::py$pred_df %>%
filter(type=='real') %>%
pull(validation_roc_auc),
reticulate::py$pred_df %>%
filter(type=='permuted') %>%
pull(validation_roc_auc),
alternative = 'greater'
)[['p.value']]
pwilcox <-
wilcox.test(
reticulate::py$pred_df %>%
filter(type=='real') %>%
pull(validation_roc_auc),
reticulate::py$pred_df %>%
filter(type=='permuted') %>%
pull(validation_roc_auc)
)[['p.value']]
reticulate::py$pred_df %>%
ggplot(aes(validation_roc_auc,fill=type)) +
geom_histogram(aes(y=after_stat(count)),alpha=.5,binwidth = .05) +
scale_fill_brewer(palette = 'Set1',direction = -1,
guide=guide_legend(title=NULL)) +
theme_bw() +
scale_x_continuous(
breaks=seq(0,1,0.05),
) +
labs(x='Validation AUROC from Model Learning',
y='Number of Validation AUROC values',
caption=paste0('Wilcox p-value = ',scales::scientific(pwilcox,3),'\n',
'Kolmogorov-Smirnov p-value = ',scales::scientific(pks,3),'\n',
'Real validation AUROC values are greater than Permuted validation AUROC values'))
```
```{r}
#| warning: false
pks <-
ks.test(
reticulate::py$fimp_df %>%
filter(type=='real') %>%
pull(importance),
reticulate::py$fimp_df %>%
filter(type=='permuted') %>%
pull(importance)
)[['p.value']]
pwilcox <-
wilcox.test(
reticulate::py$fimp_df %>%
filter(type=='real') %>%
pull(importance),
reticulate::py$fimp_df %>%
filter(type=='permuted') %>%
pull(importance)
)[['p.value']]
reticulate::py$fimp_df %>%
ggplot(aes(importance,fill=type)) +
geom_histogram(aes(y=after_stat(count)),alpha=.5,binwidth = .2) +
scale_fill_brewer(palette = 'Set1',direction = -1,
guide=guide_legend(title=NULL)) +
theme_bw() +
labs(x='Importance of X in Predicting Y',
y='Number of X importance values',
caption=paste0('Wilcox p-value = ',scales::scientific(pwilcox,3),'\n',
'Kolmogorov-Smirnov p-value = ',scales::scientific(pks,3),'\n',
'Real X importance values are greater than Permuted X importance values'))
```
```{r}
#| warning: false
pks <-
ks.test(
reticulate::py$ppred_df %>%
filter(type=='real' & y_true==1) %>%
pull(y_proba),
reticulate::py$ppred_df %>%
filter(type=='permuted' & y_true==1) %>%
pull(y_proba)
)[['p.value']]
pwilcox <-
wilcox.test(
reticulate::py$ppred_df %>%
filter(type=='real' & y_true==1) %>%
pull(y_proba),
reticulate::py$ppred_df %>%
filter(type=='permuted' & y_true==1) %>%
pull(y_proba)
)[['p.value']]
reticulate::py$ppred_df %>%
arrange(pt,bootstrap) %>%
ggplot(aes(y_proba,fill=factor(y_true),group=y_true)) +
geom_histogram(aes(y=after_stat(count)),alpha=.5,binwidth = .01) +
scale_fill_brewer(NULL,
palette = 'Set1',
direction = -1,
labels=c("Class 0","Class 1"),
guide=guide_legend(title=NULL)) +
facet_wrap(~type,ncol=1,scales='free') +
theme_bw() +
labs(x='Prediction Probability',
y='Number of Prediction Probabilities',
caption = paste0('Wilcox p-value = ',scales::scientific(pwilcox,3),'\n',
'Kolmogorov-Smirnov p-value = ',scales::scientific(pks,3),'\n',
'Real Class 1 probabilities are greater than Permuted Class 1 probabilities'))
```
This next example shows high power
```{python}
#| warning: false
import numpy as np
N=100
X1 = np.random.normal(loc=0,scale=1,size=N)
X2 = np.random.normal(loc=2,scale=1,size=N)
X = np.concatenate([X1,X2])
Y = np.concatenate([np.repeat(0,N),np.repeat(1,N)])
import pandas as pd
df = pd.DataFrame(data={'Y' : Y,'X' : X})
df.index.name = 'pt'
```
```{r}
#| warning: false
library(tidyverse)
df <- reticulate::py$df
df[['Y']] <- factor(df$Y,levels=c(0,1))
df %>%
ggplot(aes(Y,X)) +
geom_boxplot(outlier.size = NA,alpha=0,linewidth=2) +
geom_point(position = position_jitter(width = .2),pch=21,fill='gray',size=3) +
labs(x = "Response",y="Predictor",title = "Predictor values for Class One are 200% larger on average") +
theme_bw(base_size = 16)
```
```{python}
import mccv
mccv_obj = mccv.mccv(num_bootstraps=200,n_jobs=4)
mccv_obj.set_X(df[['X']])
mccv_obj.set_Y(df[['Y']])
mccv_obj.run_mccv()
mccv_obj.mccv_data['Feature Importance'].insert(
len(mccv_obj.mccv_data['Feature Importance'].columns),
'type',
'real'
)
mccv_obj.mccv_data['Model Learning'].insert(
len(mccv_obj.mccv_data['Model Learning'].columns),
'type',
'real'
)
mccv_obj.mccv_data['Patient Predictions'].insert(
len(mccv_obj.mccv_data['Patient Predictions'].columns),
'type',
'real'
)
mccv_obj.run_permuted_mccv()
mccv_obj.mccv_permuted_data['Feature Importance'].insert(
len(mccv_obj.mccv_permuted_data['Feature Importance'].columns),
'type',
'permuted'
)
mccv_obj.mccv_permuted_data['Model Learning'].insert(
len(mccv_obj.mccv_permuted_data['Model Learning'].columns),
'type',
'permuted'
)
mccv_obj.mccv_permuted_data['Patient Predictions'].insert(
len(mccv_obj.mccv_permuted_data['Patient Predictions'].columns),
'type',
'permuted'
)
fimp_df = \
pd.concat([
(mccv_obj.mccv_data['Feature Importance'].
query('feature=="X"').
reset_index(drop=True)),
(mccv_obj.mccv_permuted_data['Feature Importance'].
query('feature=="X"').
reset_index(drop=True))
])
ppred_df = \
pd.concat([
(mccv_obj.mccv_data['Patient Predictions'].
drop('y_pred',axis=1).
reset_index()),
(mccv_obj.mccv_permuted_data['Patient Predictions'].
reset_index().
drop('y_pred',axis=1))
])
pred_df = \
pd.concat([
(mccv_obj.mccv_data['Model Learning'].
reset_index()),
(mccv_obj.mccv_permuted_data['Model Learning'].
reset_index())
])
```
```{r}
#| warning: false
pks <-
ks.test(
reticulate::py$pred_df %>%
filter(type=='real') %>%
pull(validation_roc_auc),
reticulate::py$pred_df %>%
filter(type=='permuted') %>%
pull(validation_roc_auc),
alternative = 'greater'
)[['p.value']]
pwilcox <-
wilcox.test(
reticulate::py$pred_df %>%
filter(type=='real') %>%
pull(validation_roc_auc),
reticulate::py$pred_df %>%
filter(type=='permuted') %>%
pull(validation_roc_auc)
)[['p.value']]
reticulate::py$pred_df %>%
ggplot(aes(validation_roc_auc,fill=type)) +
geom_histogram(aes(y=after_stat(count)),alpha=.5,binwidth = .05) +
scale_fill_brewer(palette = 'Set1',direction = -1,
guide=guide_legend(title=NULL)) +
theme_bw() +
scale_x_continuous(
breaks=seq(0,1,0.05),
) +
labs(x='Validation AUROC from Model Learning',
y='Number of Validation AUROC values',
caption=paste0('Wilcox p-value = ',scales::scientific(pwilcox,3),'\n',
'Kolmogorov-Smirnov p-value = ',scales::scientific(pks,3),'\n',
'Real validation AUROC values are greater than Permuted validation AUROC values'))
```
```{r}
#| warning: false
pks <-
ks.test(
reticulate::py$fimp_df %>%
filter(type=='real') %>%
pull(importance),
reticulate::py$fimp_df %>%
filter(type=='permuted') %>%
pull(importance)
)[['p.value']]
pwilcox <-
wilcox.test(
reticulate::py$fimp_df %>%
filter(type=='real') %>%
pull(importance),
reticulate::py$fimp_df %>%
filter(type=='permuted') %>%
pull(importance)
)[['p.value']]
reticulate::py$fimp_df %>%
ggplot(aes(importance,fill=type)) +
geom_histogram(aes(y=after_stat(count)),alpha=.5,binwidth = .2) +
scale_fill_brewer(palette = 'Set1',direction = -1,
guide=guide_legend(title=NULL)) +
theme_bw() +
labs(x='Importance of X in Predicting Y',
y='Number of X importance values',
caption=paste0('Wilcox p-value = ',scales::scientific(pwilcox,3),'\n',
'Kolmogorov-Smirnov p-value = ',scales::scientific(pks,3),'\n',
'Real X importance values are greater than Permuted X importance values'))
```
```{r}
#| warning: false
pks <-
ks.test(
reticulate::py$ppred_df %>%
filter(type=='real' & y_true==1) %>%
pull(y_proba),
reticulate::py$ppred_df %>%
filter(type=='permuted' & y_true==1) %>%
pull(y_proba)
)[['p.value']]
pwilcox <-
wilcox.test(
reticulate::py$ppred_df %>%
filter(type=='real' & y_true==1) %>%
pull(y_proba),
reticulate::py$ppred_df %>%
filter(type=='permuted' & y_true==1) %>%
pull(y_proba)
)[['p.value']]
reticulate::py$ppred_df %>%
arrange(pt,bootstrap) %>%
ggplot(aes(y_proba,fill=factor(y_true),group=y_true)) +
geom_histogram(aes(y=after_stat(count)),alpha=.5,binwidth = .01) +
scale_fill_brewer(NULL,
palette = 'Set1',
direction = -1,
labels=c("Class 0","Class 1"),
guide=guide_legend(title=NULL)) +
facet_wrap(~type,ncol=1,scales='free') +
theme_bw() +
labs(x='Prediction Probability',
y='Number of Prediction Probabilities',
caption = paste0('Wilcox p-value = ',scales::scientific(pwilcox,3),'\n',
'Kolmogorov-Smirnov p-value = ',scales::scientific(pks,3),'\n',
'Real Class 1 probabilities are greater than Permuted Class 1 probabilities'))
```
These two examples show different metrics for defining a 'powerful' prediction. Here, a powerful prediction can only be defined by the combination of different components to describe how and to what degree a predictor X can predict a response Y. These components can come from the model learning process as well as the applying the model on new data i.e. a validation set. Defining a powerful prediction is difficult using only one quantitative metric, like the statistical tests shown in the plot captions. But a few metrics noted here (and probably others I've mistakingly overlooked) can accurately define a powerful prediction:
- The average AUROC for the validation set should be more than 50%. To be more strict, the 95% confidence interval should be greater than 50% AUROC.
- The importance value (beta coefficient for logistic regression) of X for predicting Y should be above the null association i.e. 0 and should _barely_ overlap the importance values for a shuffled response. Statistical tests will probably produce a false positive by showing a small p-value, like here, so they shouldn't be *the* metric in defining a powerful prediction.
- There seems to be at least two takeaways from the distributions of the prediction probabilities. First, the distribution of the real predicted probabilities for class 1 need to be greater than that for class 0. Also the distribution of real, class 1 predicted probabilities need to be greater than the distribution of permuted, class 1 predicted probabilities.
I used a combination of the metrics referenced above in the papers published using MCCV. This article's toy examples illustrate why these metrics are sensible for defining the power of a prediction given the data.