-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathperf.mint.splsda.R
307 lines (244 loc) · 11.1 KB
/
perf.mint.splsda.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
## ------------------------------------------------------------------------ ##
#### perf.mint ####
## ------------------------------------------------------------------------ ##
## --------------------------- perf.mint(s)pls ---------------------------- ##
#' @rdname perf
#' @export
perf.mint.pls <- function(object,
validation = c("Mfold", "loo"),
folds = 10,
progressBar = FALSE,
...)
{
stop("Yet to be implemented")
}
#' @rdname perf
#' @export
perf.mint.spls <- perf.mint.pls
## -------------------------- perf.mint(s)plsda --------------------------- ##
#' @rdname perf
#' @export
perf.mint.plsda <- function (object,
dist = c("all", "max.dist", "centroids.dist", "mahalanobis.dist"),
auc = FALSE,
progressBar = FALSE,
signif.threshold = 0.01,
...
)
{ #-- checking general input parameters --------------------------------------#
#---------------------------------------------------------------------------#
#------------------#
#-- check entries --#
X = object$X
Y = object$Y
study = object$study
ncomp = object$ncomp
scale = object$scale
keepX = apply(object$loadings$X, 2, function(x){sum(x!=0)})
tol = object$tol
max.iter = object$max.iter
dist = match.arg(dist, choices = c("all", "max.dist", "centroids.dist", "mahalanobis.dist"), several.ok = TRUE)
if (any(dist == "all"))
{
nmthdd = 3
dist = c("max.dist", "centroids.dist", "mahalanobis.dist")
} else {
nmthdd = length(dist)
}
if (!is.logical(progressBar))
stop("'progressBar' must be either TRUE or FALSE")
#-- check significance threshold
signif.threshold <- .check_alpha(signif.threshold)
#-- end checking --#
#------------------#
near.zero.var = !is.null(object$nzv) # if near.zero.var was used, we set it to TRUE. if not used, object$nzv is NULL
# -------------------------------------
# added: first check for near zero var on the whole data set
if (near.zero.var == TRUE)
{
nzv = nearZeroVar(X)
if (length(nzv$Position > 0))
{
warning("Zero- or near-zero variance predictors.\nReset predictors matrix to not near-zero variance predictors.\nSee $nzv for problematic predictors.")
X = X[, -nzv$Position, drop=TRUE]
if (ncol(X)==0)
stop("No more predictors after Near Zero Var has been applied!")
}
}
# and then we start from the X data set with the nzv removed
prediction.all = class.all = list()
for(ijk in dist)
{
class.all[[ijk]] = matrix(nrow = nrow(X), ncol = ncomp,
dimnames = list(rownames(X), c(paste0('comp', 1 : ncomp))))
}
if(auc)
{
auc.mean=list()
auc.mean.study=list()
}
study.specific = global = list()
for (study_i in 1:nlevels(study)) #LOO on the study factor
{
study.specific[[study_i]] =list()
study.specific[[study_i]]$BER = global$BER = matrix(0,nrow = ncomp, ncol = length(dist),
dimnames = list(c(paste0('comp', 1 : ncomp)), dist))
study.specific[[study_i]]$overall = global$overall = matrix(0,nrow = ncomp, ncol = length(dist),
dimnames = list(c(paste0('comp', 1 : ncomp)), dist))
study.specific[[study_i]]$error.rate.class = list()
for(ijk in dist)
study.specific[[study_i]]$error.rate.class[[ijk]] = global$error.rate.class[[ijk]] = matrix(0,nrow = nlevels(Y), ncol = ncomp,
dimnames = list(levels(Y),c(paste0('comp', 1 : ncomp))))
}
names(study.specific) =levels(study)
# successively tune the components until ncomp: comp1, then comp2, ...
for(comp in 1 : ncomp)
{
already.tested.X = keepX[1:comp]
if (progressBar == TRUE)
cat("\ncomp",comp, "\n")
#-- set up a progress bar --#
if (progressBar == TRUE)
{
pb = txtProgressBar(style = 3)
} else {
pb = FALSE
}
M = nlevels(study)
names.study = levels(study)
features = NULL
prediction.comp = matrix(0, nrow = nrow(X), ncol = nlevels(Y), dimnames = list(rownames(X), levels(Y)))
class.comp = list()
for(ijk in dist)
class.comp[[ijk]] = matrix(0, nrow = nrow(X), ncol = 1)# prediction of all samples for each test.keepX and nrep at comp fixed
if(auc)
auc.mean.study[[comp]] = list()
for (study_i in 1:M) #LOO on the study factor
{
if (progressBar == TRUE)
setTxtProgressBar(pb, (study_i-1)/M)
omit = which(study %in% names.study[study_i])
X.train = X[-omit,]
Y.train = factor(Y[-omit])
study.learn.CV = factor(as.character(study[-omit]))
X.test = X[omit, , drop = FALSE] #note: drop is useless as there should always be more than a single sample in a study
Y.test = Y[omit]
study.test.CV = factor(as.character(study[omit]))
#---------------------------------------#
#-- near.zero.var ----------------------#
if(near.zero.var == TRUE)
{
remove.zero = nearZeroVar(X.train)$Position
if (length(remove.zero) > 0)
{
X.train = X.train[, -c(remove.zero),drop = FALSE]
X.test = X.test[, -c(remove.zero),drop = FALSE]
}
}
#-- near.zero.var ----------------------#
#---------------------------------------#
if (progressBar == TRUE)
setTxtProgressBar(pb, (study_i-1)/M)
object.res = mint.splsda(X.train, Y.train, study = study.learn.CV, ncomp = comp,
keepX = already.tested.X,
scale = scale)
test.predict.sw <- predict.mixo_spls(object.res, newdata = X.test, dist = dist, study.test = study.test.CV)
prediction.comp[omit, match(levels(Y.train),levels(Y))] = test.predict.sw$predict[, , comp]
for(ijk in dist)
class.comp[[ijk]][omit,1] = test.predict.sw$class[[ijk]][, comp] #levels(Y)[test.predict.sw$class[[ijk]][, ncomp]]
if (progressBar == TRUE)
setTxtProgressBar(pb, (study_i)/M)
# result per study
#BER
study.specific[[study_i]]$BER[comp,] = sapply(test.predict.sw$class, function(x){
conf = get.confusion_matrix(truth = Y[omit], all.levels = levels(Y), predicted = x[,comp])
get.BER(conf)
})
#overall
study.specific[[study_i]]$overall[comp,] = sapply(test.predict.sw$class, function(x){
conf = get.confusion_matrix(truth = Y[omit], all.levels = levels(Y), predicted = x[,comp])
out = sum(apply(conf, 1, sum) - diag(conf)) / length(Y[omit])
})
#classification for each level of Y
temp = lapply(test.predict.sw$class, function(x){
conf = get.confusion_matrix(truth = Y[omit], all.levels = levels(Y), predicted = x[,comp])
out = (apply(conf, 1, sum) - diag(conf)) / summary(Y[omit])
})
for (ijk in dist)
study.specific[[study_i]]$error.rate.class[[ijk]][,comp] = temp[[ijk]]
#AUC per study
if(auc)
{
data = list()
data$outcome = Y[omit]
data$data = prediction.comp[omit, ]
auc.mean.study[[comp]][[study_i]] = statauc(data)
}
# average of ER and BER across studies, weighted by study sample size
global$BER[comp,] <- global$BER[comp,] + study.specific[[study_i]]$BER[comp, ] * table(study)[study_i]/length(study)
global$overall[comp,] <- global$overall[comp,] + study.specific[[study_i]]$overall[comp, ] * table(study)[study_i]/length(study)
} # end study_i 1:M (M folds)
for (ijk in dist)
{
#prediction of each samples for each fold and each repeat, on each comp
class.all[[ijk]][,comp] = class.comp[[ijk]][,1]
}
prediction.all[[comp]] = prediction.comp
#classification for each level of Y
temp = lapply(class.comp, function(x){
conf = get.confusion_matrix(truth = factor(Y), predicted = x)
out = (apply(conf, 1, sum) - diag(conf)) / summary(Y)
})
for (ijk in dist)
global$error.rate.class[[ijk]][,comp] = temp[[ijk]]
#AUC global
if(auc)
{
names(auc.mean.study[[comp]]) = names.study
data = list()
data$outcome = Y
data$data = prediction.comp
auc.mean[[comp]] = statauc(data)
}
} # end comp
names(prediction.all) = paste0('comp', 1:ncomp)
result = list(study.specific.error = study.specific,
global.error = global,
predict = prediction.all,
class = class.all)
if(auc)
{
names(auc.mean) = names(auc.mean.study) = paste0('comp', 1:ncomp)
result$auc = auc.mean
result$auc.study = auc.mean.study
}
if (progressBar == TRUE)
cat('\n')
# calculating the number of optimal component based on t.tests and the error.rate.all, if more than 3 error.rates(repeat>3)
if(nlevels(study) > 2 & ncomp >1)
{
measure = c("overall","BER")
ncomp_opt = matrix(NA, nrow = length(measure), ncol = length(dist),
dimnames = list(measure, dist))
for (measure_i in measure)
{
for (ijk in dist)
{
mat.error.rate = sapply(study.specific, function(x){x[[measure_i]][,ijk]})
ncomp_opt[measure_i, ijk] = t.test.process(t(mat.error.rate), alpha = signif.threshold)
}
}
} else {
ncomp_opt = NULL
}
result$choice.ncomp = ncomp_opt
# added
if (near.zero.var == TRUE)
result$nzvX = nzv$Position
class(result) = c("perf","perf.mint.splsda.mthd")
result$call = match.call()
return(invisible(result))
}
#' @rdname perf
#' @export
perf.mint.splsda <- perf.mint.plsda