accuracy in r machine learning code example

Example: specificity metric in r

specificityMetric<- function (Actual, Predicted){
  TP <- 0 # Actual normal point + Predicted normal point
  FN <- 0 # Actual normal point + Predicted fault point
  FP <- 0 # Actual fault point + predicted normal point
  TN <- 0 # Actual fault point + Predicted fault point
  for (i in 1:length(Actual)){
    if (Actual[i]==1 & Predicted[i]==0){
      FP=FP+1
    }
    if (Actual[i]==1 & Predicted[i]==1){
      TN=TN+1
    }
  }
  
  return ((TN)/(TN + FP))
}

print(specificityMetric( c(1,1,0,0,1,0,1,1,1,1), c(1,1,0,0,1,0,1,1,1,0) ))

Tags:

Misc Example