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
|
function.Minimax <-
function(data, marker, status, tag.healthy = 0, direction = c("<", ">"), control = control.cutpoints(), pop.prev, ci.fit = FALSE, conf.level = 0.95, measures.acc){
direction <- match.arg(direction)
if (is.logical(control$maxSp) == FALSE) {
stop("'maxSp' must be a logical-type argument.", call. = FALSE)
}
FN <-(1-measures.acc$Se[,1])*length(data[data[,status] != tag.healthy, marker])
FP <-(1-measures.acc$Sp[,1])*length(data[data[,status] == tag.healthy, marker])
M <- vector()
for(i in 1:length(measures.acc$cutoffs)) {
if (FN[i] > FP[i]) {
M[i] <- FN[i]
} else {
M[i] <- FP[i]
}
}
cMinimax <- measures.acc$cutoffs[which(round(M,10) == round(min(M,na.rm=TRUE),10))]
# If there is more than one cutpoint fulfilling these conditions,
# those which yield maximum Sensitivity or maximum Specificity are chosen:
if (length(cMinimax)> 1) {
### If you seek to maximize Specificity:
if(control$maxSp == TRUE) {
Spnew <- obtain.optimal.measures(cMinimax, measures.acc)$Sp
cutpointsSpnew <- cMinimax[which(round(Spnew[,1],10) == round(max(Spnew[,1],na.rm=TRUE),10))]
if (length(cutpointsSpnew)> 1) {
Senew <- obtain.optimal.measures(cutpointsSpnew, measures.acc)$Se
cMinimax <- cutpointsSpnew[which(round(Senew[,1],10) == round(max(Senew[,1],na.rm=TRUE),10))]
}
if (length(cutpointsSpnew)== 1) {
cMinimax <- cutpointsSpnew
}
}
### If you seek to maximize Sensitivity:
if(control$maxSp == FALSE) {
Senew <- obtain.optimal.measures(cMinimax, measures.acc)$Se
cutpointsSenew <- cMinimax[which(round(Senew[,1],10) == round(max(Senew[,1],na.rm=TRUE),10))]
if (length(cutpointsSenew)> 1) {
Spnew <- obtain.optimal.measures(cutpointsSenew, measures.acc)$Sp
cMinimax <- cutpointsSenew[which(round(Spnew[,1],10) == round(max(Spnew[,1],na.rm=TRUE),10))]
}
if (length(cutpointsSenew)== 1) {
cMinimax <- cutpointsSenew
}
}
}
optimal.M <- min(M,na.rm=TRUE)
optimal.cutoff <- obtain.optimal.measures(cMinimax, measures.acc)
res <- list(measures.acc = measures.acc, optimal.cutoff = optimal.cutoff, criterion = M, optimal.criterion = optimal.M)
res
}
|