-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetisOrdG_helpers.js
106 lines (83 loc) · 2.53 KB
/
GetisOrdG_helpers.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Helper functions for Getis-Ord Gi and Gi* (originally developed for Mahalanobis1,2)
// START
function multiplyArrays(a, b) {
let c = a.map((x, i) => {if (x == null || b[i] == null) {return null} else {return (x * b[i])}});
return c;
}
// END
// START
function sumArray(a) {
let sum = 0;
a.forEach(element => sum = element + sum);
return sum;
}
// END
// New helper:
// START
function divideByScalar(yourArray, yourScalar) {
let outArray = yourArray.map((x) => x / yourScalar);
return(outArray);
}
// END
// let testArray = bMat[0];
// let testScalar = sumArray(bMat[0]);
// console.log(divideByScalar(testArray, testScalar));
// START
function divideByScalar2(yourArray, yourScalar) {
let outArray = yourArray.map((x) => {if (x == null) {return null} else {return (x / yourScalar)}});
return(outArray);
}
// END
// Need to add null element.
// let testArrayLength = testArray.length
// let testScalar = sumArray(bMat[0]);
// console.log(divideByScalar2(testArray, testScalar));
// New helpers:
// START
function rowStandardize(weightMatrix) {
let weightMatrixCopy = weightMatrix.slice(0);
let weightMatrixOut = weightMatrixCopy;
let nRows = weightMatrixCopy.length;
for (let i = 0; i < nRows; i++) {
let rowSum = sumArray(weightMatrixCopy[i]);
weightMatrixOut[i] = divideByScalar(weightMatrixCopy[i], rowSum)
}
return(weightMatrixOut);
}
// END
// START
function removeElement(yourArray, index) {
let clone = yourArray.slice(0) // make copy with .slice()
delete clone[index] // delete element by index
let cloneMinusIndex = clone.filter(i => true)
return(cloneMinusIndex)
}
// END
// START
function countNulls(x) {
let sum = 0;
x.forEach(element => {if (element == null) {sum = 1 + sum}});
return sum;
}
//------------------------------------------------------------------------------
// This is used in the permutation test of the Gi/Gi* statistic.
// Fisher-Yates shuffle algorithm
// From:
// https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
// https://bost.ocks.org/mike/shuffle/
// This was a quick solution from the internet; there could other options.
// START
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
// END