[1] "The calculated accuracy of the model is: 0.8066"
4- Classification Error Rate
error_func <-function(df, actual, predicted){ cm <-table(df[[actual]], df[[predicted]]) tp <- cm[2,2] tn <- cm[1,1] fp <- cm[1,2] fn <- cm[2,1](fp + fn)/(tp + tn + fp +fn)}computed_err <-error_func(data, "class", "scored.class")print(paste("The calculated classification error rate of the model is:", round(error_func(data, "class", "scored.class"),4)))
[1] "The calculated classification error rate of the model is: 0.1934"
We can verify by adding the accuracy and the error rate which should sum to 1
print(computed_acc + computed_err)
[1] 1
5- Precision
precision_func <-function(df, actual, predicted) { cm <-table(df[[actual]], df[[predicted]]) TP <- cm[2,2]; FP <- cm[1,2] TP / (TP + FP)} calculated_prec<-precision_func(data, "class", "scored.class")print(paste("The calculated precision of the model is:", round(precision_func(data, "class", "scored.class"),4)))
[1] "The calculated precision of the model is: 0.8438"
6- Sensitivity (Recall)
sensitivity_func <-function(df, actual, predicted) { cm <-table(df[[actual]], df[[predicted]]) TP <- cm[2,2]; FN <- cm[2,1] TP / (TP + FN)}calculated_sensitivity<-sensitivity_func(data, "class", "scored.class")print(paste("The calculated Sensiticity (recall rate) of the model is:", round(sensitivity_func(data, "class", "scored.class"),4)))
[1] "The calculated Sensiticity (recall rate) of the model is: 0.4737"
7- Specificity
specificity_func <-function(df, actual, predicted) { cm <-table(df[[actual]], df[[predicted]]) TN <- cm[1,1]; FP <- cm[1,2] TN / (TN + FP)} calculated_specif<-specificity_func(data, "class", "scored.class")print(paste("The calculated Specificity of the model is:", round(specificity_func(data, "class", "scored.class"),4)))
[1] "The calculated Specificity of the model is: 0.9597"
8- F1 Score
f1_score_func <-function(df, actual, predicted) { p <-precision_func(df, actual, predicted) r <-sensitivity_func(df, actual, predicted)2* (p * r) / (p + r)}calculated_f1<-f1_score_func(data, "class", "scored.class")print(paste("The calculated F1 score of the model is:", round(f1_score_func(data, "class", "scored.class"),4)))
[1] "The calculated F1 score of the model is: 0.6067"
The F1-score will always be between 0 and 1 because it is based on precision and recall, which are both proportions that range from 0 to 1. When either precision or recall is 0, the F1-score is also 0. When both are 1, the F1-score reaches its maximum value of 1. Therefore, since it is the harmonic mean of two values that cannot be less than 0 or greater than 1, the F1-score will always fall between 0 and 1.
The computed ROC plot and the pROC plot display very similar curves, with the custom AUC at 0.8488 and the pROC AUC at 0.8503, indicating highly consistent results in the calculation of the area under the curve. Overall, both the ROC plots and AUC values demonstrate that the model is performing well, particularly in accurately identifying positive cases.
The packages work well and should be used to produce classification metrics efficiently and reliably.