-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateValidationErrors.R
49 lines (37 loc) · 1.93 KB
/
calculateValidationErrors.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
#Calculate validation metrics (AUC, Precision, Recall, Sensitivity, Specificity)
#Write results into a CSV file
# Calculate errors on a provided dataset (ideally a holdout one) ----------
calculateValidationErrors <- function(fitModel,modelName,validationData){
outcome <- matrix(ncol = 11, nrow = 1);
colnames(outcome)<- c("ModelName","AUC","accuracy","trueNegatives","truePositives",
"falseNegatives","falsePositives","precision","recall","specificity","sensitivity");
bugCoveringPredicted <- predict(fitModel,newdata = validationData);
matrixResult<- confusionMatrix(data=bugCoveringPredicted,validationData$bugCoveringLabels, positive="T");
trueNegatives<- matrixResult$table[1,1];
truePositives<- matrixResult$table[2,2];
falseNegatives<- matrixResult$table[1,2];
falsePositives<- matrixResult$table[2,1];
#calculate AUC
aucValue<- roc(response = as.numeric(validationData$bugCoveringLabels),
predictor = as.numeric(bugCoveringPredicted)) %>% auc();
aucValue<-as.numeric(aucValue);
accuracy <- (truePositives + trueNegatives) / (truePositives + trueNegatives + falsePositives + falseNegatives);
trainingError <- 1-accuracy;
precision <- truePositives / (truePositives + falsePositives);
recall <- truePositives / (truePositives + falseNegatives);
specificity <- trueNegatives / (trueNegatives + falsePositives);
sensitivity <- truePositives / (truePositives + falseNegatives);
#create output table
outcome[1,"ModelName"] <- modelName;
outcome[1,"AUC"] <- aucValue;
outcome[1,"accuracy"]<-accuracy;
outcome[1,"trueNegatives"]<-trueNegatives;
outcome[1,"truePositives"]<-truePositives;
outcome[1,"falseNegatives"]<-falseNegatives;
outcome[1,"falsePositives"]<-falsePositives;
outcome[1,"precision"]<-precision;
outcome[1,"recall"]<-recall;
outcome[1,"sensitivity"]<-sensitivity;
outcome[1,"specificity"]<-specificity;
return(outcome);
}