Skip to content

Instantly share code, notes, and snippets.

@ngopal
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngopal/0f880c80af9de3ef3213 to your computer and use it in GitHub Desktop.
Save ngopal/0f880c80af9de3ef3213 to your computer and use it in GitHub Desktop.
A small javascript library to create a confusion matrix and calculate values of true positives, true negatives, false positives, false negatives, sensitivity, specificity, positive predictive value, negative predictive value, positive likelihood, negative likelihood, prevalence, false positive rate, false discovery rate, false negative rate, Mat…
// Written by Nikhil Gopal
// http://www.nikhilgopal.com
function TwoCellCalculation(k,j) {
return(k/(k+j));
}
function ConfusionMatrix(a,b,c,d) {
this.n = a+b+c+d;
this.true_positives = a;
this.false_positives = b;
this.false_negatives = c;
this.true_negatives = d;
this.sensitivity = TwoCellCalculation(a,c);
this.specificity = TwoCellCalculation(b,d);
this.ppv = TwoCellCalculation(a,b);
this.npv = TwoCellCalculation(c,d);
this.positive_likelihood = this.sensitivity / (1-this.specificity);
this.negative_likelihood = 1/this.positive_likelihood;
this.prevalence = (a+c)/(a+b+c+d);
this.fpr = 1 - this.specificity;
this.fdr = 1 - this.ppv;
this.fnr = this.false_negatives / (this.false_negatives+this.true_positives);
this.mcc = ((this.true_positives*this.true_negatives)-(this.false_positives*this.false_negatives))/Math.sqrt((this.true_positives+this.false_positives)*(this.true_positives+this.false_negatives)*(this.true_negatives+this.false_positives)*(this.true_negatives+this.false_negatives));
this.acc = (this.true_positives+this.true_negatives)/this.n;
this.f1 = 2*(this.true_positives)/(2*this.true_positives+this.false_positives+this.false_negatives);
this.informedness = this.sensitivity+this.specificity-1;
this.markedness = this.ppv + this.npv - 1;
return(this);
}
var A = ConfusionMatrix(400,430,230,740);
$('sensitivity').html( A.ppv );
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="ConfusionMatrix.js"></script>
<meta charset="utf-8">
<title>Confusion Matrix</title>
</head>
<body>
<sensitivity></sensitivity>
<a href="http://en.wikipedia.org/wiki/Sensitivity_and_specificity">More info</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment