values = [8160,8160,6160,22684,0,0,60720,1380,1380,57128]
how can I remove outliers like 0, 57218, 60720 and 22684?
Is there a library whic
Here is the implementation to filter upper outliers from a given collection. This approach follows a similar methodology as the provided answers above.
The if
case will be checking the length of collection if it is 4n
or 4n + 1
. In that case, we need to get an average of two elements to have our quartiles.
Otherwise, in cases of 4n + 2
and 4n + 3
, we directly can access the upper/lower quartile.
const outlierDetector = collection => {
const size = collection.length;
let q1, q3;
if (size < 2) {
return collection;
}
const sortedCollection = collection.slice().sort((a, b) => a - b);
if ((size - 1) / 4 % 1 === 0 || size / 4 % 1 === 0) {
q1 = 1 / 2 * (sortedCollection[Math.floor(size / 4) - 1] + sortedCollection[Math.floor(size / 4)]);
q3 = 1 / 2 * (sortedCollection[Math.ceil(size * 3 / 4) - 1] + sortedCollection[Math.ceil(size * 3 / 4)]);
} else {
q1 = sortedCollection[Math.floor(size / 4)];
q3 = sortedCollection[Math.floor(size * 3 / 4)];
}
const iqr = q3 - q1;
const maxValue = q3 + iqr * 1.5;
return sortedCollection.filter(value => value >= maxValue);
};