TrainROC: Generates ROC from train and test sets.

Description Usage Arguments Examples

Description

Generates an ROC curve based on the train and test set provided in the sample datasets or by another dataset provided by the user.

Usage

1
TrainROC(train, ImportantGenes, GeneNumbers = 7)

Arguments

train
ImportantGenes
GeneNumbers

Examples

  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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (train, ImportantGenes, GeneNumbers = 7) 
{
    index <- sample.int(dim(train)[1], size = floor(0.8 * dim(train)[1]))
    dtrain <- train[index, -1]
    dtest <- train[-index, -c(1, dim(train)[2])]
    TestStatus <- train[-index, dim(train)[2]]
    library(randomForest)
    library(e1071)
    trcol <- colnames(dtrain)
    tecol <- colnames(dtest)
    SelectedSet <- ImportantGenes[1:GeneNumbers]
    trind <- c()
    tsind <- c()
    for (i in 1:length(SelectedSet)) {
        trind <- c(trind, which(trcol == SelectedSet[i]))
        tsind <- c(tsind, which(tecol == SelectedSet[i]))
    }
    dtr <- dtrain[, c(trind, dim(dtrain)[2])]
    dts <- dtest[, tsind]
    rf <- randomForest(as.factor(Status) ~ ., ntree = 1000, data = dtr)
    prf <- predict(rf, newdata = dts, type = "prob")
    prf1 <- prf[, 2]
    threshold <- seq(0, 1, 0.01)
    tp <- c()
    fp <- c()
    for (i in 1:length(threshold)) {
        temp <- ceiling(prf1 - threshold[i])
        conf <- xtabs(~temp + TestStatus)
        if (dim(conf)[1] == 1) {
            if (unique(temp) == 1) {
                tp = c(tp, 1)
                fp = c(fp, 1)
            }
            else {
                tp = c(tp, 0)
                fp = c(fp, 0)
            }
        }
        else {
            tp <- c(tp, conf[2, 2]/(conf[1, 2] + conf[2, 2]))
            fp <- c(fp, conf[2, 1]/(conf[1, 1] + conf[2, 1]))
        }
    }
    AUC_RF <- sum(tp) * 0.01
    pdf("ROC_CURVES_OF_TRAIN_SET.pdf")
    plot(fp, tp, ylab = "True Positives", xlab = "False Positives", 
        main = "Sample ROC of Random Forest", type = "l", col = 4)
    lines(c(0, 1), c(0, 1), lty = 2)
    sv <- svm(as.factor(Status) ~ ., data = dtr, kernel = "radial", 
        type = "C-classification", cross = 10, probability = TRUE)
    psv <- predict(sv, newdata = dts, probability = TRUE)
    prob <- attr(psv, "probabilities")
    prob1 <- prob[, 2]
    tp <- c()
    fp <- c()
    for (i in 1:length(threshold)) {
        temp <- ceiling(prob1 - threshold[i])
        conf <- xtabs(~temp + TestStatus)
        if (dim(conf)[1] == 1) {
            if (unique(temp) == 1) {
                tp = c(tp, 1)
                fp = c(fp, 1)
            }
            else {
                tp = c(tp, 0)
                fp = c(fp, 0)
            }
        }
        else {
            tp <- c(tp, conf[2, 2]/(conf[1, 2] + conf[2, 2]))
            fp <- c(fp, conf[2, 1]/(conf[1, 1] + conf[2, 1]))
        }
    }
    AUC_SVM1 <- sum(tp) * 0.01
    plot(fp, tp, ylab = "True Positives", xlab = "False Positives", 
        main = "Sample ROC of SVM(Gaussian Kernel)", type = "l", 
        col = 4)
    lines(c(0, 1), c(0, 1), lty = 2)
    sv1 <- svm(as.factor(Status) ~ ., data = dtr, kernel = "sigmoid", 
        type = "C-classification", cross = 10, probability = TRUE)
    psv1 <- predict(sv, newdata = dts, probability = TRUE)
    prob <- attr(psv1, "probabilities")
    prob2 <- prob[, 2]
    tp <- c()
    fp <- c()
    for (i in 1:length(threshold)) {
        temp <- ceiling(prob2 - threshold[i])
        conf <- xtabs(~temp + TestStatus)
        if (dim(conf)[1] == 1) {
            if (unique(temp) == 1) {
                tp = c(tp, 1)
                fp = c(fp, 1)
            }
            else {
                tp = c(tp, 0)
                fp = c(fp, 0)
            }
        }
        else {
            tp <- c(tp, conf[2, 2]/(conf[1, 2] + conf[2, 2]))
            fp <- c(fp, conf[2, 1]/(conf[1, 1] + conf[2, 1]))
        }
    }
    AUC_SVM2 <- sum(tp) * 0.01
    plot(fp, tp, ylab = "True Positives", xlab = "False Positives", 
        main = "Sample ROC of SVM (Sigmoidal Kernel)", type = "l", 
        col = 4)
    lines(c(0, 1), c(0, 1), lty = 2)
    ens.p = (prf1 + prob1 + prob2)/3
    tp <- c()
    fp <- c()
    for (i in 1:length(threshold)) {
        temp <- ceiling(ens.p - threshold[i])
        conf <- xtabs(~temp + TestStatus)
        if (dim(conf)[1] == 1) {
            if (unique(temp) == 1) {
                tp = c(tp, 1)
                fp = c(fp, 1)
            }
            else {
                tp = c(tp, 0)
                fp = c(fp, 0)
            }
        }
        else {
            tp <- c(tp, conf[2, 2]/(conf[1, 2] + conf[2, 2]))
            fp <- c(fp, conf[2, 1]/(conf[1, 1] + conf[2, 1]))
        }
    }
    AUC_Ens <- sum(tp) * 0.01
    plot(fp, tp, ylab = "True Positives", xlab = "False Positives", 
        main = "Sample ROC of Ensemble Models", type = "l", col = 4)
    lines(c(0, 1), c(0, 1), lty = 2)
    dev.off()
    Method <- c("Random Forest", "SVM(Gaussian Kernel)", "SVM(Sigmoidal Kernel)", 
        "Ensemble")
    AUCROC <- c(AUC_RF, AUC_SVM1, AUC_SVM2, AUC_Ens)
    AUC <- as.data.frame(cbind(Method, AUCROC))
    write.csv(AUC, "AUCROC_Train.csv", quote = FALSE, row.names = FALSE)
    return(NULL)
  }

bvnlab/SCATTome documentation built on May 13, 2019, 9:05 a.m.