[c4e594]: / InformationTheory-PredictiveRankings.R

Download this file

353 lines (267 with data), 17.2 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
347
348
###################################################################
########## Predictive Rankings ####################################
###################################################################
###################################################################
########## Output Categorical - Covariates Categorical ############
###################################################################
#### First Order Rankings - INFO ####
#####################################
INFO.Output_Categorical.Covariates_Categorical <- function(data,labels,treatment){
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
### Calculate the I(Y;T|X) for each Xs
for (index_feature in 1:num_features){
mi_scores[index_feature] = condinformation_normalised(treatment,labels,data[,index_feature])# condinformation(treatment,labels,data[,index_feature],method="shrink")
}
#### Prepare the return functions
sorted_scores <- sort(mi_scores, decreasing=T,method='shell',index.return=TRUE)
ranking_scores <- sort(sorted_scores$ix, decreasing=F,method='shell',index.return=TRUE)
results <- list("scores" = mi_scores, "ranking" = sorted_scores$ix, "ranking_scores" = ranking_scores$ix)
return(results)
}
#######################################
#### Second order rankings - INFO+ ####
#######################################
INFOplus.Output_Categorical.Covariates_Categorical <- function(data,labels,treatment, top_k){
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
ranking_scores <- rep(0, num_features)
ranking <- rep(0, num_features)
selected_features <- 0
### First Step: Select the first covariate (this is equivalent of using the INFO and take the first features)
VT.First <- INFO.Output_Categorical.Covariates_Categorical(data,labels,treatment)
selected_features[1]<-VT.First$ranking[1]
ranking_scores[selected_features[1]] <- 1
mi_scores[selected_features[1]] <- VT.First$scores[VT.First$ranking[1]]
#### Second Step: Iteretavely rank the features, by estimating the second order criterion for each one of them, and take the features with the highest score
not_selected_features <- setdiff(1:num_features,selected_features)
score_per_feature <- array(0,dim=c(1,num_features))
score_per_feature[selected_features[1]]<-NA
count_cmi <- num_features
for (count in 2:top_k){
### Check the score of each feature not selected so far
for (index_feature_ns in 1:length(not_selected_features)){
## To calculate this score we should calculate the conditional mutual information with the features selected
conditioning_features <- do.call(interaction,data[,c(not_selected_features[index_feature_ns], selected_features[count-1])])
score_per_feature[not_selected_features[index_feature_ns]] <- score_per_feature[not_selected_features[index_feature_ns]] + condinformation_normalised(treatment,labels,conditioning_features)# condinformation(treatment,labels,conditioning_features,method="shrink")
count_cmi <- count_cmi+1
}
selected_features[count] <- which.max(score_per_feature) ### It ignores the NA, for that reason I check all of the features (the already selected they have score NA)
ranking_scores[selected_features[count]] <- count
mi_scores[selected_features[count]] <- score_per_feature[selected_features[count]]
score_per_feature[selected_features[count]]<-NA
not_selected_features <- setdiff(1:num_features,selected_features)
}
ranking_scores[ranking_scores==0] <- (top_k+1):num_features
results <- list("scores" = mi_scores, "ranking" = selected_features, "ranking_scores" = ranking_scores,"count_cmi" = count_cmi)
return(results)
}
###################################################################
########## Output Categorical - Covariates Continuous# ############
###################################################################
#### First Order Rankings - INFO ####
#####################################
INFO.Output_Categorical.Covariates_Continuous <- function(data,labels,treatment){
num_features <- ncol(data)
### First step - Discretization
for (index_feature in 1:num_features){
### Use Scott's rule to discretize
data[,index_feature] = discretize( data[,index_feature], disc="equalwidth", nbins=nclass.scott(data[,index_feature]))
}
### Second step - Derive ranking, by normalising with the conditional entropy
mi_scores <- rep(0, num_features)
### Calculate the I(Y;T|X) for each Xs
for (index_feature in 1:num_features){
mi_scores[index_feature] = condinformation_normalised(treatment,labels,data[,index_feature])
}
#### Prepare the return functions
sorted_scores <- sort(mi_scores, decreasing=T,method='shell',index.return=TRUE)
ranking_scores <- sort(sorted_scores$ix, decreasing=F,method='shell',index.return=TRUE)
results <- list("scores" = mi_scores, "ranking" = sorted_scores$ix, "ranking_scores" = ranking_scores$ix)
return(results)
}
#######################################
#### Second order rankings - INFO+ ####
#######################################
INFOplus.Output_Categorical.Covariates_Continuous <- function(data,labels,treatment, top_k){
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
ranking_scores <- rep(0, num_features)
ranking <- rep(0, num_features)
selected_features <- 0
### First Step: Select the first covariate (this is equivalent of using the INFO and take the first features)
VT.First <- INFO.Output_Categorical.Covariates_Continuous(data,labels,treatment)
selected_features[1]<-VT.First$ranking[1]
ranking_scores[selected_features[1]] <- 1
mi_scores[selected_features[1]] <- VT.First$scores[VT.First$ranking[1]]
### Discretization
for (index_feature in 1:num_features){
### Use Scott's rule to discretize
data[,index_feature] = discretize( data[,index_feature], disc="equalwidth", nbins=nclass.scott(data[,index_feature]))
}
#### Second Step: Iteratively rank the features, by estimating the second order criterion for each one of them, and take the features with the highest score
not_selected_features <- setdiff(1:num_features,selected_features)
score_per_feature <- array(0,dim=c(1,num_features))
score_per_feature[selected_features[1]]<-NA
count_cmi <- num_features
for (count in 2:top_k){
### Check the score of each feature not selected so far
for (index_feature_ns in 1:length(not_selected_features)){
## To calculate this score we should calculate the conditional mutual information with the features selected
conditioning_features <- do.call(interaction,data[,c(not_selected_features[index_feature_ns], selected_features[count-1])])
score_per_feature[not_selected_features[index_feature_ns]] <- score_per_feature[not_selected_features[index_feature_ns]] + condinformation_normalised(treatment,labels,conditioning_features)
count_cmi <- count_cmi+1
}
selected_features[count] <- which.max(score_per_feature) ### It ignores the NA, for that reason I check all of the features (the already selected they have score NA)
ranking_scores[selected_features[count]] <- count
mi_scores[selected_features[count]] <- score_per_feature[selected_features[count]]
score_per_feature[selected_features[count]]<-NA
not_selected_features <- setdiff(1:num_features,selected_features)
}
ranking_scores[ranking_scores==0] <- (top_k+1):num_features
results <- list("scores" = mi_scores, "ranking" = selected_features, "ranking_scores" = ranking_scores,"count_cmi" = count_cmi)
return(results)
}
# The normalized conditional mutual information with respect to conditional entropy
condinformation_normalised <- function(treatment, labels, features)
{
cmi_normalised <- condinformation(treatment,labels,features,method="shrink") / sqrt(condentropy(treatment, features, method="shrink")*condentropy(labels, features, method="shrink"))
return(cmi_normalised)
}
###################################################################
########## Output Survival - Covariates Categorical ###############
###################################################################
#### Estimate conditional mutual informaiton with survival outputs
condinformation_survival_normalised <- function(treatment, labels, features, times, censor_groups){
sample_size <- length(labels)
time_disc <- sort(unique(times))
# Follow SIDES approach to use an extra parameter
times_steps <- seq(0, length(time_disc), length.out = censor_groups + 1)
cmi_normalised<-integer(length(times_steps)-1)
for (index_steps in 2:(length(times_steps))){
labels_step <- integer(sample_size)
labels_step[which(times<=time_disc[times_steps[index_steps]])] <- labels[which(times<=time_disc[times_steps[index_steps]])]
cmi_normalised[index_steps] <- condinformation(treatment,labels_step,features,method="shrink")/sqrt(condentropy(treatment, features, method="shrink")*condentropy(labels_step, features, method="shrink"))
}
return( mean(cmi_normalised,na.rm=TRUE))
}
#####################################
#### First Order Rankings - INFO ####
#####################################
INFO.Output_Survival.Covariates_Categorical <- function(data,labels,treatment,times,censor_groups){
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
### Calculate the I(Y;T|X) for each Xs
for (index_feature in 1:num_features){
mi_scores[index_feature] = condinformation_survival_normalised(treatment,labels,data[,index_feature], times, censor_groups)
}
#### Prepare the return functions
sorted_scores <- sort(mi_scores, decreasing=T,method='shell',index.return=TRUE)
ranking_scores <- sort(sorted_scores$ix, decreasing=F,method='shell',index.return=TRUE)
results <- list("scores" = mi_scores, "ranking" = sorted_scores$ix, "ranking_scores" = ranking_scores$ix)
return(results)
}
#######################################
#### Second order rankings - INFO+ ####
#######################################
INFOplus.Output_Survival.Covariates_Categorical <- function(data,labels, treatment, times, censor_groups, top_k){
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
ranking_scores <- rep(0, num_features)
ranking <- rep(0, num_features)
selected_features <- 0
### First Step: Select the first covariate (this is equivalent of using the INFO and take the first features)
VT.First <- INFO.Output_Survival.Covariates_Categorical (data,labels,treatment,times, censor_groups)
selected_features[1]<-VT.First$ranking[1]
ranking_scores[selected_features[1]] <- 1
mi_scores[selected_features[1]] <- VT.First$scores[VT.First$ranking[1]]
#### Second Step: Iteratively rank the features, by estimating the second order criterion for each one of them, and take the features with the highest score
not_selected_features <- setdiff(1:num_features,selected_features)
score_per_feature <- array(0,dim=c(1,num_features))
score_per_feature[selected_features[1]]<-NA
count_cmi <- num_features
for (count in 2:top_k){
### Check the score of each feature not selected so far
for (index_feature_ns in 1:length(not_selected_features)){
## To calculate this score we should calculate the conditional mutual information with the features selected
conditioning_features <- do.call(interaction,data[,c(not_selected_features[index_feature_ns], selected_features[count-1])])
score_per_feature[not_selected_features[index_feature_ns]] <- score_per_feature[not_selected_features[index_feature_ns]] + condinformation_survival_normalised(treatment,labels,conditioning_features,times, censor_groups) #/ condentropy(treatment + 2*labels, data[,not_selected_features[index_feature_ns]], method="shrink")
count_cmi <- count_cmi+1
}
selected_features[count] <- which.max(score_per_feature) ### It ignores the NA, for that reason I check all of the features (the already selected they have score NA)
ranking_scores[selected_features[count]] <- count
mi_scores[selected_features[count]] <- score_per_feature[selected_features[count]]
score_per_feature[selected_features[count]]<-NA
not_selected_features <- setdiff(1:num_features,selected_features)
}
ranking_scores[ranking_scores==0] <- (top_k+1):num_features
results <- list("scores" = mi_scores, "ranking" = selected_features, "ranking_scores" = ranking_scores,"count_cmi" = count_cmi)
return(results)
}
###################################################################
########## Output Survival - Covariates Continuous ################
###################################################################
#### First order rankings - INFO ####
#####################################
INFO.Output_Survival.Covariates_Continuous <- function(data, labels, treatment, times, censor_groups){
### First step - Discretization
for (index_feature in 1:num_features){
### Use Scott's rule to discretize
data[,index_feature] = discretize( data[,index_feature], disc="equalwidth", nbins=nclass.scott(data[,index_feature]))
}
### Second step - Derive ranking, by normalizing with the conditional entropy
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
### Calculate the I(Y;T|X) for each Xs
for (index_feature in 1:num_features){
mi_scores[index_feature] = condinformation_survival_normalised(treatment,labels,data[,index_feature], times, censor_groups)
}
#### Prepare the return functions
sorted_scores <- sort(mi_scores, decreasing=T,method='shell',index.return=TRUE)
ranking_scores <- sort(sorted_scores$ix, decreasing=F,method='shell',index.return=TRUE)
results <- list("scores" = mi_scores, "ranking" = sorted_scores$ix, "ranking_scores" = ranking_scores$ix)
return(results)
}
#######################################
#### Second order rankings - INFO+ ####
#######################################
INFOplus.Output_Survival.Covariates_Continuous <- function(data, labels, treatment, times, censor_groups, top_k){
### First step - Discretization
for (index_feature in 1:num_features){
### Use Scott's rule to discretize
data[,index_feature] = discretize( data[,index_feature], disc="equalwidth", nbins=nclass.scott(data[,index_feature]))
}
### Second step - Derive ranking, by normalising with the conditional entropy
num_features <- ncol(data)
mi_scores <- rep(0, num_features)
ranking_scores <- rep(0, num_features)
ranking <- rep(0, num_features)
selected_features <- 0
### First Step: Select the first covariate (this is equivalent of using the INFO and take the first features)
VT.First <- INFO.Output_Survival.Covariates_Continuous(data,labels,treatment, times, censor_groups)
selected_features[1]<-VT.First$ranking[1]
ranking_scores[selected_features[1]] <- 1
mi_scores[selected_features[1]] <- VT.First$scores[VT.First$ranking[1]]
#### Second Step: Iteretavely rank the features, by estimating the second order criterion for each one of them, and take the features with the highest score
not_selected_features <- setdiff(1:num_features,selected_features)
score_per_feature <- array(0,dim=c(1,num_features))
score_per_feature[selected_features[1]]<-NA
count_cmi <- num_features
for (count in 2:top_k){
### Check the score of each feature not selected so far
for (index_feature_ns in 1:length(not_selected_features)){
## To calculate this score we should calculate the conditional mutual information with the features selected
conditioning_features <- do.call(interaction,data[,c(not_selected_features[index_feature_ns], selected_features[count-1])])
score_per_feature[not_selected_features[index_feature_ns]] <- score_per_feature[not_selected_features[index_feature_ns]] + condinformation_survival_normalised(treatment, labels, conditioning_features, times, censor_groups)
count_cmi <- count_cmi+1
}
selected_features[count] <- which.max(score_per_feature) ### It ignores the NA, for that reason I check all of the features (the already selected they have score NA)
ranking_scores[selected_features[count]] <- count
mi_scores[selected_features[count]] <- score_per_feature[selected_features[count]]
score_per_feature[selected_features[count]]<-NA
not_selected_features <- setdiff(1:num_features,selected_features)
}
ranking_scores[ranking_scores==0] <- (top_k+1):num_features
results <- list("scores" = mi_scores, "ranking" = selected_features, "ranking_scores" = ranking_scores,"count_cmi" = count_cmi)
return(results)
}