-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparisonx.R
242 lines (188 loc) · 9.69 KB
/
Comparisonx.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
############ Ground Truth on TRAINING DATA ###################################
############## PREDICTIONS ON TESTING DATA ##################################
### K-means + CoxPH
### K-means + AFT
### K-means + Penalized CoxPH
### K-means + Penalized AFT
### Mixture of Factor Analyzers
#### Sparse KMeans
#### Sparse Hierarchical clustering
Comparisonx = function(){
smod <- Surv(exp(time), censoring)
smod.new <- Surv(exp(time.new), censoring.new)
### Fitting A Penalized Cox Proportional Hazard's Model
reg.pcox <- cv.glmnet(x = Y, y = smod, family = "cox")
lp <- predict(object =reg.pcox, newx = Y, s= "lambda.min")
recovCIndex.na.cox <<- as.numeric(survConcordance(smod ~lp)[1])
######## Prediction with penalized Cox Proportional Hazards Model ###########################################
linear.pred.cox <- c(0)
### see if we can use glmnet
reg.pcox <- cv.glmnet(x = Y, y = Surv(exp(time), censoring), family = "cox")
linear.pred.cox <- predict(object =reg.pcox, newx = Y.new, s= "lambda.min")
predCIndex.na.cox <<- as.numeric(survConcordance(smod.new ~ linear.pred.cox)[1])
#### Fitting a penalized AFT Model ####
reg <- cv.glmnet(x = Y, y = time, family = "gaussian")
linear.aft <- predict(object = reg, newx = Y, s = "lambda.min")
cindex.paft <- as.numeric(survConcordance(smod ~ exp(-linear.aft))[1])
recovCIndex.na.aft <<- as.numeric(cindex.paft)
##### Prediction using the penlized AFT #############################################
linear.pred.paft <- c(0)
### see if we can use glmnet
reg.paft <- cv.glmnet(x = Y, y = time, family = "gaussian")
linear.pred.paft <- predict(object = reg.paft, newx = Y.new, s= "lambda.min")
predCIndex.na.aft <<- as.numeric(survConcordance(smod.new ~ exp(-linear.pred.paft))[1])
#############################################
########### K-means #########################
############ K-Nearest Neighbour ############
#############################################
gr.km <- kmeans(Y, F, nstart =10)
recovRandIndex.km <<- adjustedRandIndex(c.true,as.factor(gr.km$cluster))
label.train <- as.factor(gr.km$cluster)
### One has to to tune the k-NN classifier for k ###
fitControl <- trainControl(method = "repeatedcv", number = 5,repeats = 5)
### Tune the parameter k
knnFit <- caret::train(x = as.data.frame(Y), y = label.train, method = "knn", trControl = fitControl, preProcess = c("center","scale"), tuneLength = 5)
knnPredict <- predict(knnFit,newdata = as.data.frame(Y.new ))
label.test <- knnPredict
predRandIndex.knear <<- adjustedRandIndex(c.true.new, label.test)
###### penCox ###################################################################
######## Penalized Cox PH with k-means clustering###########################################
linear.cox <- c(0)
for ( q in 1:F){
ind <- which((gr.km$cluster) == q)
time.tmp <- time[ind]
censoring.tmp <- censoring[ind]
Y.tmp <- Y[ind,]
coxreg <- list(0)
coxreg$x <- Y.tmp
coxreg$time <- exp(time.tmp)
coxreg$status <- censoring.tmp
reg.pcox <- cv.glmnet(x = Y.tmp, y = Surv(coxreg$time, coxreg$status), family = "cox")
linear.cox[ind] <- predict(object =reg.pcox, newx = Y.tmp, s= "lambda.min")
}
recovCIndex.km.pcox <<- as.numeric(survConcordance(smod ~ linear.cox)[1])
### Prediction with k-means + k-nearest neghbour
linear.kkpcox.prediction <- c(0)
for ( q in 1:F){
ind <- which(label.train == q)
ind.new <- which(label.test == q)
reg.aft <- cv.glmnet(x = Y[ind,], y = Surv(exp(time[ind]),censoring[ind]), family = "cox")
linear.kkpcox.prediction[ind.new] <- predict(object =reg.aft, newx = Y.new[ind.new,], s= "lambda.min")
}
predCIndex.kk.pcox <<- as.numeric(survConcordance(smod.new ~ linear.kkpcox.prediction)[1])
###### penAFT ###################################################################
######## Penalized AFT with k-means clustering ######################################################
linear.aft <- c(0)
for ( q in 1:F){
ind <- which((gr.km$cluster) == q)
L= length(ind)
time.tmp <- time[ind]
censoring.tmp <- censoring[ind]
Y.tmp <- Y[ind,]
reg <- cv.glmnet(x = Y.tmp, y = time.tmp, family = "gaussian")
coeff.pred <- coef(object =reg, newx = Y.tmp, s= "lambda.min")
rel.coeff <- coeff.pred[2:(D+1)]
ind.rel <- which(rel.coeff !=0)
linear.aft[ind] <- predict(object = reg, newx = Y.tmp, s = "lambda.min")
}
recovCIndex.km.paft <<- as.numeric(survConcordance(smod ~ exp(-linear.aft))[1])
#### prediction with penAFT ###################################################################
linear.kkpaft.prediction <- c(0)
for ( q in 1:F){
ind <- which(label.train == q)
ind.new <- which(label.test == q)
reg.aft <- cv.glmnet(x = Y[ind,], y = time[ind], family = "gaussian")
linear.kkpaft.prediction[ind.new] <- predict(object =reg.aft, newx = Y.new[ind.new,], s= "lambda.min")
}
predCIndex.kn.paft <<- as.numeric(survConcordance(smod.new ~ exp(-linear.kkpaft.prediction))[1])
######## Model fitting with Penalized Cox PH with TRue Clustering ###########################################
######## Model prediction with k-nn based on true clustering #####
label.train <- as.factor(c.true)
### One has to to tune the k-NN classifier for k ###
fitControl <- trainControl(method = "repeatedcv", number = 5,repeats = 5)
### Tune the parameter k
knnFit <- caret::train(x = as.data.frame(Y), y = label.train, method = "knn", trControl = fitControl, preProcess = c("center","scale"), tuneLength = 5)
true.knn <- predict(knnFit,newdata = as.data.frame(Y.new ))
predRandIndex.true.knear <<- adjustedRandIndex(c.true.new, true.knn)
linear.pred <- c(0)
cox.pred <- c(0)
for ( q in 1:F){
ind <- which((c.true) == q)
ind.new <- which(true.knn == q)
time.tmp <- time[ind]
censoring.tmp <- censoring[ind]
Y.tmp <- Y[ind,1:D]
Y.tmp.new <- Y.new[ind.new,1:D]
coxreg <- list(0)
coxreg$x <- Y.tmp
coxreg$time <- exp(time.tmp)
coxreg$status <- censoring.tmp
reg.pcox <- cv.glmnet(x = Y.tmp, y = Surv(coxreg$time, coxreg$status), family = "cox")
linear.pred[ind] <- predict(object =reg.pcox, newx = Y.tmp, s= "lambda.min")
cox.pred[ind.new] <- predict(object =reg.pcox, newx = Y.tmp.new, s= "lambda.min")
}
recovCIndex.true.pcox <<- as.numeric(survConcordance(smod ~ linear.pred)[1])
predCIndex.true.knn.pcox <<- as.numeric(survConcordance(smod.new ~ cox.pred)[1])
######## Penalized AFT with TRUE clustering ######################################################
# linear.aft <- c(0)
# pred.aft <- c(0)
#
#
# for ( q in 1:F){
# ind <- which((c.true) == q)
# ind.new <- which(true.knn == q)
# L= length(ind)
#
# time.tmp <- time[ind]
# censoring.tmp <- censoring[ind]
# Y.tmp <- Y[ind,]
# Y.tmp.new <- Y.new[ind.new,1:D]
#
#
# reg <- cv.glmnet(x = Y.tmp, y = time.tmp, family = "gaussian")
# linear.pred <- predict(object =reg, newx = Y.tmp, s= "lambda.min")
# coeff.pred <- coef(object =reg, newx = Y.tmp, s= "lambda.min")
# rel.coeff <- coeff.pred[2:(D+1)]
# ind.rel <- which(rel.coeff !=0)
# linear.aft[ind] <- predict(object = reg, newx = Y.tmp, s = "lambda.min")
# pred.aft[ind.new] <- predict(object = reg, newx = Y.tmp.new, s = "lambda.min")
# }
# recovCIndex.true.paft <<- as.numeric(survConcordance(smod ~ exp(-linear.aft))[1])
# predCIndex.true.knn.paft <<- as.numeric(survConcordance(smod.new ~ exp(-pred.aft))[1])
#
#
############## Testing Mixture of Factor Analyzers ##########################3
# mcfa.fit<- mcfa(Y, g= k, q=2, itmax=250, nkmeans=5, nrandom=5, tol=1.e-3)
########## Seeing if the PCA plot with the corresponding features with releevant features makes sense
#randindexMCFA <<- adjustedRandIndex(mcfa.fit$clust, c.true)
#############################################
########### sparse K-means #########################
#############################################
#############################################
km.perm <- KMeansSparseCluster.permute(x = Y, K= k ,wbounds=c(1.5,2:6),nperms=5)
km.out <- KMeansSparseCluster(x = Y, K=k,wbounds=km.perm$bestw)
recovRandIndexSKM <<- adjustedRandIndex(km.out[[1]]$Cs, c.true)
###################################################
########### sparse hierarchical clustering #########################
#############################################
#############################################
perm.out <- HierarchicalSparseCluster.permute(x = Y, wbounds=c(1.5,2:6), nperms=5)
# Perform sparse hierarchical clustering
sparsehc <- HierarchicalSparseCluster(dists=perm.out$dists, wbound=perm.out$bestw, method="complete")
recovRandIndexSHC <<- adjustedRandIndex(cutree(sparsehc$hc, k = k), c.true)
### Let's see if we can verify if the model likelihood for the real c is indeed the highest
### Use mixture of regression model using mixtools
# library(mixtools)
# out <- regmixEM.loc(y = time, x = Y[,1:D], lambda = NULL, beta = NULL, sigma = NULL,
# k = 2, addintercept = TRUE, kern.l = c("Gaussian"),
# epsilon = 1e-08, maxit = 10000, kernl.g = 0,
# kernl.h = 1, verb = TRUE)
#
############ Predicting New Class Labels using SVM #################################
# gr.km <- kmeans(Y, F, nstart =10)
# label.train <- gr.km$cluster
# svms <- sapply(2^(-10:14), function(cost) cross(ksvm(Y, factor(label.train), C=cost, kernel="vanilladot", kpar=list(), cross=5)))
# mysvm <- ksvm(Y, factor(label.train), C=2^(-10:14)[which.min(svms)], kernel="vanilladot", kpar=list(), cross=10) # accuracy ~97%
# pred.svm <- predict(mysvm, Y.new)
# predRandIndex.svm <<- adjustedRandIndex(c.true.new, pred.svm)
}