outlier2
Version:
Find outliers in dataset
21 lines (15 loc) • 664 B
JavaScript
// Iglewicz and Hoaglin method
// Values with a Z-score > 3.5 are considered potential outliers
// Based on https://github.com/alyssaq/stats-analysis
const stat = require('../stat-func');
module.exports = function(arr, opts, callback) {
let threshold = opts && opts.threshold || 3.5;
let median = stat.median(arr);
let MAD = stat.median(arr.map((e) => Math.abs(e - median)));
let check = (e) => Math.abs((0.6745 * (e - median)) / MAD) > threshold;
let res = (opts && !!opts.indexes) ?
arr.map((e, i) => check(e) && i).filter((e) => e !== false):
arr.filter(check);
return (callback) ? callback(null, res) : res;
}