forked from easonfg/cali_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcali_tutorial.R
358 lines (282 loc) · 11.1 KB
/
cali_tutorial.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
rm(list=ls())
library(pROC)
library(ResourceSelection)
# library(randomForest)
library(e1071)
library(generalhoslem)
library(ggplot2)
library(reshape2)
library(plyr)
library(Hmisc)
library(rms)
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
source('./svm_platt_recal.R')
source('./svm_iso_recal.R')
source('./lr_platt_recal.R')
source('./lr_iso_recal.R')
source('./hosmer_lemeshow.R')
source('./Spiegelhalter_z.R')
source('./reliability_diagram.R')
source('./mce_ece.R')
source('./cox.R')
source('./ici.R')
set.seed(612)
#####################
#### normalize to 0 and 1
vert_norm <- function(dataframe){
dataframe = as.data.frame(dataframe)
df_min = apply(dataframe, 2, min)
df_max = apply(dataframe, 2, max)
#matrix minus min of each col
temp_df = t(t(dataframe)-apply(dataframe,2,min))
#divided by max - min of each row
return_df = t(t(temp_df) / (df_max - df_min))
return(as.data.frame(return_df))
}
######
pipe_run = function(ninstances){
####### create 23 coefficients #######
iter <- 20
out <- matrix(NA, nrow=iter, ncol=ninstances)
counter = 1
for (i in seq(from=3, to=60, by=3)){
res = rbinom(ninstances, 1, i/100)
out[counter,] = c(res)
counter = counter + 1
}
out = rbind(out, rnorm(ninstances, 0.5, 0.1))
out = rbind(out, rnorm(ninstances, 0.5, 0.5))
out = rbind(out, rnorm(ninstances, 0.5, 1))
B = rep(c(1.0, -0.5), times = c(1,1), length.out = 20)
B = c(B, 1.0, -1.0, 1.0)
logit_y = t(out)%*%B - 4.60
prob = exp(logit_y)/(1+exp(logit_y))
####### create 23 coefficients #######
##### use uniform distributions to create labels ####
uni_dist = runif(ninstances, min = 0, max = 1)
labels = as.integer(uni_dist < prob)
dev_prob = rep(prob)
dev_prob[dev_prob<0.027] = dev_prob[dev_prob<0.027]
dev_prob[dev_prob>0.127] = dev_prob[dev_prob>0.127]
dev_labels = as.integer(uni_dist < dev_prob)
##### use uniform distributions to create labels ####
# combine coefficients and labels
perfect = as.data.frame(t(out))
perfect = cbind(perfect, y=dev_labels)
perfect = vert_norm(perfect)
#### set training to be 50% of total number of samples and validation and test set to be 25%
### create train, validate, and test datasets
smp_size <- floor(0.50 * nrow(perfect))
val_smp_size = floor(0.25*nrow(perfect))
train_ind <- sample(seq_len(nrow(perfect)), size = smp_size)
train <- perfect[train_ind, ]
val_test <- perfect[-train_ind, ]
validate_ind <- sample(seq_len(nrow(val_test)), size = val_smp_size)
validate <- val_test[validate_ind, ]
test <- val_test[-validate_ind, ]
#######
###
##### logistic regression ####
### train and predict ###
#train with 'train' dataset and and predict 'test' dataset with logistic regressions
log_mod <- glm(y~., family=binomial(link='logit'),data=train)
log_pred = as.data.frame(predict(log_mod, test[,-length(test)], type='response'))
### get auc ###
pr <- ROCR::prediction(log_pred, test$y)
auc_log <- ROCR::performance(pr, measure = "auc")
auc_log <- [email protected][[1]]
### recalibration ###
# lr recalibration with platt
lr_platt_recal = lr_platt_recal_func(log_mod, validate, log_pred, test)
## lr recalibration with isotonic regression
lr_iso_recal = lr_iso_recal_func(log_mod, validate, log_pred, test)
## recalibration ###
###### svm #######
### train and predict ###
svmfit = svm(factor(y) ~ ., data = train, kernel = "linear", cost = 10, scale = FALSE, probability=TRUE)
ygrid = predict(svmfit, test[,-length(test)], probability=TRUE)
ygrid_norm = as.data.frame(attr(ygrid, 'probabilities')[,2])
### get auc ###
pr <- ROCR::prediction(ygrid_norm, test$y)
auc_svmnorm <- ROCR::performance(pr, measure = "auc")
auc_svmnorm <- [email protected][[1]]
## svm recalibartion with platt scaling
svm_platt_recal = svm_platt_recal_func(svmfit, validate, ygrid_norm, test)
## svm recalibartion with isotonic regression
svm_iso_recal = svm_iso_recal_func(svmfit, validate, ygrid_norm, test)
###### svm #######
##average absolute difference ##
print('log org avg absolute diff')
print(mean(abs(test$y-log_pred[,1])))
print('svm org avg aboslute diff')
print(mean(abs(test$y-ygrid_norm[,1])))
print('log platt avg absolute diff')
print(mean(abs(test$y-lr_platt_recal)))
print('svm platt avg absolute diff')
print(mean(abs(test$y-svm_platt_recal)))
print('log iso avg absolute diff')
print(mean(abs(test$y-lr_iso_recal)))
print('svm iso avg absolute diff')
print(mean(abs(test$y-svm_iso_recal)))
##average absolute difference##
##calibration-in-the-large##
print('log org cal in large')
print(mean(test$y)/mean(log_pred[,1]))
print('svm org cal in large')
print(mean(test$y)/mean(ygrid_norm[,1]))
print('log platt cal in large')
print(mean(test$y)/mean(lr_platt_recal))
print('svm platt cal in large')
print(mean(test$y)/mean(svm_platt_recal))
print('log iso cal in large')
print(mean(test$y)/mean(lr_iso_recal))
print('svm iso cal in large')
print(mean(test$y)/mean(svm_iso_recal))
##calibration-in-the-large##
### measuring calibration ###
### hosmer lemeshow test ###
print('hosmer lemeshow test: lr org C')
hosmer_lemeshow(test$y, log_pred[,1], 10, 'C')
print('hosmer lemeshow test: svm org C')
hosmer_lemeshow(test$y, ygrid_norm[,1], 10, 'C')
print('hosmer lemeshow test: lr platt C')
hosmer_lemeshow(test$y, lr_platt_recal, 10, 'C')
print('hosmer lemeshow test: lr iso C')
hosmer_lemeshow(test$y, lr_iso_recal, 10, 'C')
print('hosmer lemeshow test: svm platt C')
hosmer_lemeshow(test$y, svm_platt_recal, 10, 'C')
print('hosmer lemeshow test: svm iso C')
hosmer_lemeshow(test$y, svm_iso_recal, 10, 'C')
print('hosmer lemeshow test: lr org H')
hosmer_lemeshow(test$y, log_pred[,1], 10, 'H')
print('hosmer lemeshow test: svm org H')
hosmer_lemeshow(test$y, ygrid_norm[,1], 10, 'H')
print('hosmer lemeshow test: lr platt H')
hosmer_lemeshow(test$y, lr_platt_recal, 10, 'H')
print('hosmer lemeshow test: lr iso H')
hosmer_lemeshow(test$y, lr_iso_recal, 10, 'H')
print('hosmer lemeshow test: svm platt H')
hosmer_lemeshow(test$y, svm_platt_recal, 10, 'H')
print('hosmer lemeshow test: svm iso H')
hosmer_lemeshow(test$y, svm_iso_recal, 10, 'H')
### hosmer lemeshow test ###
### Brier score ###
cat('Brier score: svm org', val.prob(ygrid_norm[,1],test$y)['Brier'], '\n')
cat('Brier score: lr org', val.prob(log_pred[,1],test$y)['Brier'], '\n')
cat('Brier score: svm platt', val.prob(svm_platt_recal,test$y)['Brier'], '\n')
cat('Brier score: lr platt', val.prob(lr_platt_recal,test$y)['Brier'], '\n')
cat('Brier score: svm iso', val.prob(svm_iso_recal,test$y)['Brier'], '\n')
cat('Brier score: lr iso', val.prob(lr_iso_recal,test$y)['Brier'], '\n')
### Brier score ###
### Spiegelhalter z test ###
print('Spiegelhalter z test: lr org')
Spiegelhalter_z(test$y, log_pred[,1])
print('Spiegelhalter z test: svm org')
Spiegelhalter_z(test$y, ygrid_norm[,1])
print('Spiegelhalter z test: lr platt')
Spiegelhalter_z(test$y, lr_platt_recal)
print('Spiegelhalter z test: lr iso')
Spiegelhalter_z(test$y, lr_iso_recal)
print('Spiegelhalter z test: svm platt')
Spiegelhalter_z(test$y, svm_platt_recal)
print('Spiegelhalter z test: svm iso')
Spiegelhalter_z(test$y, svm_iso_recal)
### Spiegelhalter z test ###
#### mce ece ####
print('lr org')
print(ece_mce(test$y, log_pred[,1], 10, 'C'))
print('svm org')
print(ece_mce(test$y, ygrid_norm[,1], 10, 'C'))
print('lr platt')
print(ece_mce(test$y, lr_platt_recal, 10, 'C'))
print('lr iso')
print(ece_mce(test$y, lr_iso_recal, 10, 'C'))
print('svm platt')
print(ece_mce(test$y, svm_platt_recal, 10, 'C'))
print('svm iso')
print(ece_mce(test$y, svm_iso_recal, 10, 'C'))
#### mce ece ####
#### cox ####
print('cox: lr org')
print(cox_first_degree(test$y, log_pred[,1]))
print('cox: svm org')
print(cox_first_degree(test$y, ygrid_norm[,1]))
print('cox: lr platt')
print(cox_first_degree(test$y, lr_platt_recal))
print('cox: lr iso')
print(cox_first_degree(test$y, lr_iso_recal))
print('cox: svm platt')
print(cox_first_degree(test$y, svm_platt_recal))
print('cox: svm iso')
print(cox_first_degree(test$y, svm_iso_recal))
#### cox ####
#### ici ####
print('ici: lr org')
print(ici(test$y, log_pred[,1]))
print('ici: svm org')
print(ici(test$y, ygrid_norm[,1]))
print('ici: lr platt')
print(ici(test$y, lr_platt_recal))
print('ici: lr iso')
print(ici(test$y, lr_iso_recal))
print('ici: svm platt')
print(ici(test$y, svm_platt_recal))
print('ici: svm iso')
print(ici(test$y, svm_iso_recal))
#### ici ####
#### reliability diagrams ####
###original LR and SVM with C statistics
reliability_diagram(as.vector(test$y),
list(as.vector(log_pred[,1]),
as.vector(ygrid_norm[,1])),
'C',
c('LR Original', 'SVM Original'),
c('blue', 'red'),
'LR SVM original C')
#original LR and SVM with H statistics
reliability_diagram(as.vector(test$y),
list(as.vector(log_pred[,1]),
as.vector(ygrid_norm[,1])),
'H',
c('LR Original', 'SVM Original'),
c('blue', 'red'),
'LR SVM original H')
#Platt and iso recalibrated LR with C statistics
reliability_diagram(as.vector(test$y),
list(as.vector(log_pred[,1]),
as.vector(lr_platt_recal),
as.vector(lr_iso_recal)),
'C',
c('LR Original', 'LR Platt scaling Recalibration', 'LR Isotonic regression Recalibration'),
c('blue', 'green', 'orange'),
'LR Platt Iso C')
#Platt and iso recalibrated LR with H statistics
reliability_diagram(as.vector(test$y),
list(as.vector(log_pred[,1]),
as.vector(lr_platt_recal),
as.vector(lr_iso_recal)),
'H',
c('LR Original', 'LR Platt scaling Recalibration', 'LR Isotonic regression Recalibration'),
c('blue', 'green', 'orange'),
'LR Platt Iso H')
#Platt and iso recalibrated SVM with C statistics
reliability_diagram(as.vector(test$y),
list(as.vector(ygrid_norm[,1]),
as.vector(svm_platt_recal),
as.vector(svm_iso_recal)),
'C',
c('SVM Original', 'SVM Platt scaling Recalibration', 'SVM Isotonic regression Recal'),
c('red', 'green', 'orange'),
'SVM Platt Iso C')
#Platt and iso recalibrated SVM with H statistics
reliability_diagram(as.vector(test$y),
list(as.vector(ygrid_norm[,1]),
as.vector(svm_platt_recal),
as.vector(svm_iso_recal)),
'H',
c('SVM Original', 'SVM Platt scaling Recalibration', 'SVM Isotonic regression Recal'),
c('red', 'green', 'orange'),
'SVM Platt Iso H')
### measuring calibration ###
}
n5000res = pipe_run(ninstances = 5000)