Skip to content

Instantly share code, notes, and snippets.

@tspspi
Created December 11, 2021 23:08
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 tspspi/b7d90a020f2dd106d9b1339d484dab4c to your computer and use it in GitHub Desktop.
Save tspspi/b7d90a020f2dd106d9b1339d484dab4c to your computer and use it in GitHub Desktop.
Mini script for bayesian rule
function bayescalc01_updateOutput_InvalidInput(invalidFieldName) {
document.getElementById(invalidFieldName).style.backgroundColor = "lightred";
document.getElementById('bayescalc01_pill').value = '';
document.getElementById('bayescalc01_phealthy').value = '';
document.getElementById('bayescalc01_ppos').value = '';
document.getElementById('bayescalc01_pneg').value = '';
document.getElementById('bayescalc01_correctpos').value = '';
document.getElementById('bayescalc01_falsepos').value = '';
document.getElementById('bayescalc01_correctneg').value = '';
document.getElementById('bayescalc01_falseneg').value = '';
}
function bayescalc01_updateOutput() {
if (document.getElementById('bayescalc01_prevalence') && document.getElementById('bayescalc01_sensitivity') && document.getElementById('bayescalc01_specificity')) {
document.getElementById('bayescalc01_prevalence').style.backgroundColor = "inherit";
document.getElementById('bayescalc01_sensitivity').style.backgroundColor = "inherit";
document.getElementById('bayescalc01_specificity').style.backgroundColor = "inherit";
let prevalence = document.getElementById('bayescalc01_prevalence').value;
let sensitivity = document.getElementById('bayescalc01_sensitivity').value;
let specificity = document.getElementById('bayescalc01_specificity').value;
// Some sanity checking
if(isNaN(prevalence)) { bayescalc01_updateOutput_InvalidInput('bayescalc01_prevalence'); return; }
if(isNaN(sensitivity)) { bayescalc01_updateOutput_InvalidInput('bayescalc01_sensitivity'); return; }
if(isNaN(specificity)) { bayescalc01_updateOutput_InvalidInput('bayescalc01_specificity'); return; }
if((prevalence < 0) || (prevalence > 100000)) { bayescalc01_updateOutput_InvalidInput('bayescalc01_prevalence'); return; }
if((sensitivity < 0) || (sensitivity > 100)) { bayescalc01_updateOutput_InvalidInput('bayescalc01_sensitivity'); return; }
if((specificity < 0) || (specificity > 100)) { bayescalc01_updateOutput_InvalidInput('bayescalc01_specificity'); return; }
// calculation and scaling
let psick = prevalence / 100000;
let phealthy = 1 - psick;
sensitivity = sensitivity / 100;
specificity = specificity / 100;
let ppos = psick * sensitivity + phealthy * (1 - specificity);
let pneg = psick * (1 - sensitivity) + phealthy * specificity;
let pcorrectpos = sensitivity * psick / ppos;
let pfalsepos = (1 - specificity) * phealthy / ppos;
let pcorrectneg = specificity * phealthy / pneg;
let pfalseneg = (1 - sensitivity) * psick / pneg;
document.getElementById('bayescalc01_pill').value = Math.round(psick * 10000) / 100;
document.getElementById('bayescalc01_phealthy').value = Math.round(phealthy * 10000) / 100;
document.getElementById('bayescalc01_ppos').value = Math.round(ppos * 10000) / 100;
document.getElementById('bayescalc01_pneg').value = Math.round(pneg * 10000) / 100;
document.getElementById('bayescalc01_correctpos').value = Math.round(pcorrectpos * 10000) / 100;
document.getElementById('bayescalc01_falsepos').value = Math.round(pfalsepos * 10000) / 100;
document.getElementById('bayescalc01_correctneg').value = Math.round(pcorrectneg * 10000) / 100;
document.getElementById('bayescalc01_falseneg').value = Math.round(pfalseneg * 10000) / 100;
}
}
document.addEventListener("DOMContentLoaded", function(event) {
if (document.getElementById('bayescalc01_prevalence') && document.getElementById('bayescalc01_sensitivity') && document.getElementById('bayescalc01_specificity')) {
let prevalence = document.getElementById('bayescalc01_prevalence').addEventListener('change', bayescalc01_updateOutput);
let sensitivity = document.getElementById('bayescalc01_sensitivity').addEventListener('change', bayescalc01_updateOutput);
let specificity = document.getElementById('bayescalc01_specificity').addEventListener('change', bayescalc01_updateOutput);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment