-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroc.R
More file actions
131 lines (106 loc) · 3.33 KB
/
Copy pathroc.R
File metadata and controls
131 lines (106 loc) · 3.33 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
library("pROC")
imagesDirectory <- "images/roc/"
dir.create(imagesDirectory,
recursive = TRUE,
showWarnings = FALSE)
source("load-maldiquant-cancer-fiedler.R")
binnedPeaksMatrix[is.na(binnedPeaksMatrix)] <- 0
# ---------------------------------------------------------------------------
#
# 1. ROC analysis
#
# ---------------------------------------------------------------------------
#
# 1.1 ROC analysis for biomarkers
#
# ---------------------------------------------------------------------------
png(paste0(imagesDirectory, "roc-biomarker.png"),
width = 800,
height = 800)
# plot the biomarker
rocobj <-
plot.roc(binnedPeaksMatrix.conditions, binnedPeaksMatrix[, "1450.33683095267"])
dev.off()
# show area under roc curve
rocobj$auc
# show best threshold
coords(rocobj,
"best",
ret = c("threshold", "sensitivity", "specificity"))
# compare 2 biomarkers
png(
paste0(imagesDirectory, "roc-2biomarkers.png"),
width = 800,
height = 800
)
rocobj <-
plot.roc(binnedPeaksMatrix.conditions, binnedPeaksMatrix[, "1450.33683095267"], col =
"red")
legend(
"bottomright",
col = c("#ff0000", "#0000ff"),
legend = c("peak 1450.33683095267", "peak 1546.52890935556"),
lwd = 2
)
rocobj2 <-
plot.roc(binnedPeaksMatrix.conditions,
binnedPeaksMatrix[, "1546.52890935556"],
col = "blue",
add = TRUE)
dev.off()
# show area under roc curve of the second biomarker
rocobj2$auc
# ---------------------------------------------------------------------------
#
# 1.2 ROC analysis for machine learning models
#
# ---------------------------------------------------------------------------
library("caret")
# ---------------------------------------------------------------------------
#
# 1.2.1 Data preparation
#
# ---------------------------------------------------------------------------
data <- as.data.frame(binnedPeaksMatrix)
data <- cbind(data, binnedPeaksMatrix.conditions)
colnames(data)[ncol(data)] <- "condition"
set.seed(2019)
trainSamplesIndexes <-
createDataPartition(y = data$condition,
p = 0.7,
list = FALSE)
train <- data[trainSamplesIndexes, ]
test <- data[-trainSamplesIndexes, ]
# Replace column names with V1, V2, ..., Vn to use them in some models that do
# not accept variable names starting with numbers
data.colnames <-
c(sapply(1:(ncol(data) - 1), function(x)
paste0("V", x)), "condition")
train.fixedColnames <- train
colnames(train.fixedColnames) <- data.colnames
test.fixedColnames <- test
colnames(test.fixedColnames) <- data.colnames
# ---------------------------------------------------------------------------
#
# 1.2.2 ROC for a Artificial Neural Network
#
# ---------------------------------------------------------------------------
nn <-
train(
condition ~ .,
data = train.fixedColnames,
method = "mlp",
trControl = trainControl(method = "none"),
preProcess = c("center", "scale")
)
nn.test.result <-
predict(nn, type = "prob", newdata = test.fixedColnames)
testSamples.cancerProb <- nn.test.result[, 1]
testSamples.class <-
binnedPeaksMatrix.conditions[match(rownames(nn.test.result), rownames(binnedPeaksMatrix))]
png(paste0(imagesDirectory, "roc-ann.png"),
width = 800,
height = 800)
rocobj <- plot.roc(testSamples.class, testSamples.cancerProb)
rocobj$auc
dev.off()