-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathROC.R
70 lines (48 loc) · 1.92 KB
/
ROC.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
ROC <- function(classifier, train_feature, train_label,test_data, K = 1) {
# if classifer not in given choice
dat = cbind(train_feature, train_label)
if (classifier == "Logistic")
{
glm_result = glm(paste("train_label ~ ", paste(names(
train_feature
), collapse = "+"), sep = ""),
data = dat,
family = 'binomial')
preds <- predict(glm_result, type = "response")
t = pROC::coords(roc(train_label, preds), "best", transpose = FALSE)
preds <- predict(glm_result, test_data, type = "response")
roc_result = roc(test_data$Cloud01, preds, print.auc = T, plot = T, print.thres = t[[1]])
}
if (classifier == "LDA")
{
LDA_result = lda(as.formula(paste(
"train_label ~ ", paste(names(train_feature), collapse = "+"), sep = ""
)), data = dat)
preds = predict(LDA_result, dat)$class
roc_result <- roc(train_label, as.numeric(preds))
}
if (classifier == "QDA")
{
QDA_result = qda(as.formula(paste(
"train_label ~ ", paste(names(train_feature), collapse = "+"), sep = ""
)), data = dat)
preds = predict(QDA_result, dat)$class
roc_result <- roc(train_label, as.numeric(preds))
}
if (classifier == "NB")
{
NB_result = naiveBayes(as.formula(paste(
"train_label ~ ", paste(names(train_feature), collapse = "+"), sep = ""
)), data = dat)
preds = predict(NB_result, dat)
roc_result <- roc(train_label, as.numeric(preds))
}
if (classifier == "KNN")
{
KNN_result=knn(dat %>% dplyr::select(-train_label),
dat %>% dplyr::select(-train_label),
cl=dat$train_label,k=K,use.all=T)
roc_result <- roc(train_label, as.numeric(KNN_result))
}
return(roc_result)
}