-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanDataValues.js
47 lines (37 loc) · 1.42 KB
/
cleanDataValues.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
alterState(state => {
// Copy and sort the values, rather than operating on the object array
var values = state.data.dataValues
.map(x => x.value)
.concat()
.sort((a, b) => a - b);
console.log('Intial count of dataValues from dhis2: ' + values.length);
// Then find a generous IQR. This is generous because if (values.length / 4)
// is not an int, then really you should average the two elements on either
// side to find q1.
const q1 = values[Math.floor(values.length / 4)];
console.log('q1 value: ' + q1);
// Likewise for q3.
const q3 = values[Math.ceil(values.length * (3 / 4))];
console.log('q3 value: ' + q3);
const iqr = q3 - q1;
console.log('iqr = ' + iqr);
// Then find min and max values
const maxValue = parseFloat(q3) + parseFloat(iqr) * 1.5;
console.log('Max allowed value: ' + maxValue);
const minValue = q1 - iqr * 1.5;
console.log('Min allowed value: ' + minValue);
var outliers = [];
// Then filter anything beyond or beneath these values.
var cleanSet = state.data.dataValues.filter(x => {
if (x.value <= maxValue && x.value >= minValue) {
return true;
}
outliers.push(x);
});
console.log('Removed ' + outliers.length + ' outliers.');
console.log(cleanSet.length + ' dataValues will be imported.');
console.warn('Discarded data for analysis:');
console.warn(JSON.stringify(outliers, null, 2));
state.data.dataValues = cleanSet;
return state;
});