-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale1D.js
64 lines (45 loc) · 2.54 KB
/
scale1D.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Function: scale1D()
// Authors: Corey Devin Anderson and Kirankumar Batchu
// -----------------------------------------------------------------------------
// Description:
// Takes 1D Array and returns a 1D Array containing the mean-centered values
// or standard (z) scores.
// For arrays containing null values, nulls are removed prior to calculating
// means (and/or standard deviations), but are retained in the output array.
// The original version of this function (scale() and scale2()) work on
// 2D Arrays and Dense Matrices and may be less efficient if your task is
// limited to 1D Arrays.
// -----------------------------------------------------------------------------
// Parameters:
// yourArray : an unnested JavaScript Array
// method : rescaling to be applied to each array. Current options are "zscore"
// or "centered". Default is "centered".
// Returns:
// An Array of equal dimensions containing either mean-centerd or standard
// (z) scores.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Function: scale1D()
// START
function scale1D(yourArray, method = "centered") {
// .filter the data to return only those that are not null
let arrayClean = yourArray.filter(function(x) {if (x != null) {return true} else {return false}});
// Get the mean and standard deviation of the null filtered Array
let rowMean = math.mean(arrayClean); // take the mean of the Array using math.js
let rowStd = math.std(arrayClean); // take the standard deviation of the Array using math.js
// .map a new Array of mean-centered values; returns null if null for that component.
// Round each to four decimal places.
// The +(unary plus operator) forces a number to be returned (otherwise you get a string).
let centered = yourArray.map(function(x){if (x == null) {return null} else if (x != null) {return +(x - rowMean)}});
// If the user chooses "zscore"
// divide each mean-centered value by the standard deviation to get an Array of z-scores
if (method == "zscore") {
outArray = centered.map(function(x){if (x == null) {return null} else if (x != null) {return +(x / rowStd)}});
} else if (method == "centered") {
outArray = centered
}
return outArray;
}
// END
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------