-
Notifications
You must be signed in to change notification settings - Fork 0
/
localMoranI_nulls.js
56 lines (45 loc) · 2.14 KB
/
localMoranI_nulls.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
// Function: localMoranI_nulls
// Author: Corey Devin Anderson
//------------------------------------------------------------------------------//
// Requires:
// sumArray()
// multiplyArrays()
// (SEE: localMoranI_helpers.js)
// Description:
// JavaScript functions for calculating Local Moran's I (sensu Anselin 1995). The
// function localMoranI() calculates the values of Local Moran's I at each location
// given a flattened data array and a spatial weights matrix. The function
// localMoranI_nulls can accomodate missing (null) values.
//------------------------------------------------------------------------------//
// Parameters:
// dataArray : a 1D Array containing the values at each location.
// weightMatrix : a square matrix (n x n) represented as a 2D Array containing
// the pairwise weights. Each 1D Array within the 2D Array
// represents the connections for a particular location and should
// be in the same order as the data values in yourArray.
// Returns:
// a 1D Array containing the value of Local Moran's I at each location; locations
// with null values will be returned as null.
//------------------------------------------------------------------------------//
// START
function localMoranI_nulls(dataArray, weightMatrix) {
let nAll = weightMatrix.length;
let nullCount = countNulls(dataArray)
let nX = nAll - nullCount;
let Ybar = sumArray(dataArray) / nX
let yjMinusYbar = dataArray.map(yj => {if (yj == null) {return null} else {return (yj - Ybar)}});
let yjMinusYbarSquared = yjMinusYbar.map(y => {if (y == null) {return null} else {return y **2}})
let m2 = sumArray(yjMinusYbarSquared) / nX
iOut = new Array(nAll)
for (let i = 0; i < nAll; i++) {
if (peakValleyFlat_nulls[i] == null) {
iOut[i] = null;
} else {
let valuesConnected = multiplyArrays(yjMinusYbar, bMat[i]); // This will zero out any values that are not connected to i.
let sumyjMinusYbar = sumArray(valuesConnected);
iOut[i] = +(((peakValleyFlat_nulls[i] - Ybar) * sumyjMinusYbar) / m2).toFixed(2);
}
}
return(iOut)
}
// END