Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Outliers: z-score and modified z-score method in JavaScript
// z-score
const { mean, deviation } = require('d3-array')
const zscore = input => {
const arrMean = mean(input)
// here the n-1 : http://duramecho.com/Misc/WhyMinusOneInSd.html
const arrDeviation = deviation(input)
return input.map(i => ({
zscore: (i - arrMean) / arrDeviation,
item: i,
}))
}
const findOutliersZ = input => {
return zscore(input).filter(x => x.zscore >= 3 || x.zscore <= -3).map(x => x.item)
}
/** MODIFIED ZSCORE ***/
const R = require('ramda')
const zscorem = input => {
// const arrMean = mean(input)
// here the n-1 : http://duramecho.com/Misc/WhyMinusOneInSd.html
// const arrDeviation = deviation(input)
const medianY = R.median(input)
medianY //?
// MAD
const medianAbsDeviationY = R.median(
input.map(y => Math.abs(y - medianY))
)
medianAbsDeviationY //?
const MODIFIED_Z_SCORE_CONST = 0.6745
return input.map(y => ({
zscore: MODIFIED_Z_SCORE_CONST * (y - medianY) / medianAbsDeviationY,
item: y,
}))
}
const findOutliersZ = input => {
return zscorem(input).filter(x => x.zscore >= 3.5 || x.zscore <= -3.5).map(x => x.item)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment