6/30/2020
Analytics/despiking.js at master · oluwafemi2016/Analytics · GitHub
oluwafemi2016 / Analytics
Code
Issues
Pull requests
Actions
Projects 1
Join GitHub today
Security
Insigh
Dismiss
GitHub is home to over 50 million developers
working together to host and review code, manage
projects, and build software together.
Sign up
master
Analytics / despiking.js /
Jump to
oluwafemi2016 Create despiking.js …
History
1 contributor
Raw
Blame
67 lines (63 sloc)
2.54 KB
1
//DESPIKE
2
//1. This functions removes outliers from a 1D dataset
3
//2. The cut-offs are: low outliers => beneath first quartile and high outliers => above third qua
4
//3. Replaces outliers with linear interpolation of neighboring non-outliers
5
6
//Required Dependency
7
const outliers = require("outliers");
8
9
function despike(array) {
10
//Clean dirty array
11
const array_clean = [];
12
array.forEach((val) => {
13
if (!isNaN(val)) {
14
array_clean.push(val);
15
16
}
});
https://github.com/oluwafemi2016/Analytics/blob/master/despiking.js
1/3
6/30/2020
Analytics/despiking.js at master · oluwafemi2016/Analytics · GitHub
17
console.log("array_clean", array_clean)
18
//Remove Outliers from cleaned array
19
const array_filtered = array_clean.filter(outliers());
20
console.log("array_filtered", array_filtered, array_filtered.length)
21
//Next: linearly interpolate between neighboring elements to fill in the space of outliers
22
//Get outliers
23
const outlier = outliers(array_clean);
24
console.log("outliers", outlier)
25
//Get indices of outliers in array_clean
26
const Index_outliers = [];
27
for (let j = 0; j < outlier.length; j++) {
28
Index_outliers.push(array_clean.indexOf(outlier[j]));
29
}
30
console.log("Index_Outliers", Index_outliers)
31
//Splice into array_clean_filtered the interpolated values of neighbors to outliers
32
//Caveat: if one of the neighbors is undefined/especially at edges/ends, then pick a non-NaN n
33
const interp = [];
34
for (let j = 0; j < Index_outliers.length; j++) {
35
if (Index_outliers[j] == 0) {
36
interp[j] = array_clean[1];
37
} else if (Index_outliers[j] == array_clean.length - 1) {
38
interp[j] = array_clean[array_clean.length - 2];
39
} else {
40
interp[j] = (array_clean[Index_outliers[j] - 1] + array_clean[Index_outliers[j] + 1])
41
}
42
array_filtered.splice(Index_outliers[j], 0, interp[j]);
43
}
44
console.log("interp", interp)
45
console.log("array_filtered 2", array_filtered)
46
47
//Get Indices of all NaN elements in dirty array
48
const Index_NaN = [];
49
array.filter(function (arr, indx) {
50
if (isNaN(arr)) {
51
Index_NaN.push(indx)
52
}
53
});
54
console.log("Index_NaN", Index_NaN)
55
//With the known Index substitute/splice the NaNs into array filtered
56
for (let j = 0; j < Index_NaN.length; j++) {
57
array_filtered.splice(Index_NaN[j], 0, NaN);
58
}
59
const despiked_array = array_filtered;
60
return (despiked_array)
61
}
62
63
//Testing Only
64
const A = [NaN, -120, 14, 12.5, NaN, 50, 13, 1400, 16, 18.6, -600, 65, 66, 1200, NaN, NaN]
https://github.com/oluwafemi2016/Analytics/blob/master/despiking.js
2/3
6/30/2020
Analytics/despiking.js at master · oluwafemi2016/Analytics · GitHub
65
const desp = despike(A);
66
console.log("desp", desp);
67
//console.log("outliers", outliers(A))
https://github.com/oluwafemi2016/Analytics/blob/master/despiking.js
3/3