-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetisOrdGiStarZ_nulls.js
67 lines (57 loc) · 3.75 KB
/
GetisOrdGiStarZ_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
57
58
59
60
61
62
63
64
65
66
67
// Functions:
// GetisOrdGi() - includes values from locations j connected to location i
// but excludes the value at i in the calculation.
// GetisOrdGiStar() - includes the value at location (i).
// GetisOrdGiStarZ()- produces a standard (z) score reather than the Gi or GiStar
// statistic.
// GetisOrdGiStarZ_nulls() - same as GetisOrdGiStarZ but handles null values in
// the data array (the connections matrix should include
// all locations, regardless of whether or not data
// are missing for a variable at that locaion).
// GetisOrdGiZ() - standard (z) score transformation of Gi statistic.
// Functions for spatial randomization test of Gi*:
// permuteGetisOrdGiStar(), testGetisOrdGiStar()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//------------------------------------------------------------------------------//
// Description:
// Calculates Getis-Ord statistics (Gi* and Gi) for determining local hot (and
// cold) spots. Getis-Ord Gi* includes the focal location (i) in the calculation
// whereas Gi does not. The functions GetisOrdGiStarZ() and GetisOrdGiZ() return
// standard variates (z-scores) based on observed and expected Getis-Ord
// statistics. Currently, one function (GetisOrdGiStarZ_nulls) can handle null
// values, and others will be added in the near future. Such functions essentially
// return the same result as if you dropped the location prior to building weights
// and calculating the test statistics.
//------------------------------------------------------------------------------//
// 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.
//------------------------------------------------------------------------------//
// START
function GetisOrdGiStarZ_nulls(dataArray, weightMatrix) {
let nullCount = countNulls(dataArray); // Use helper function to count the number of null values in the dataArray
let nAll = weightMatrix.length; // The full length of the Array, including positions with null values; use this for iterating.
let nX = nAll - nullCount; // n = number of positions minus the number of nulls; use this for calculations.
let totalSum = sumArray(dataArray); // sum values in the dataArray.
let Xbar = totalSum / nX; // get average values in the dataArray.
let s = ((sumArray(dataArray.map(xj => xj ** 2)) / nX) - Xbar ** 2) ** 0.5; // standard deviation of values in the dataArray
let zOut = new Array(nAll); // make blank array to populate with z-scores
for (let i = 0; i < nAll; i++) { // calculate Gi* statistics for each position; note we need to iterate through all of the positions, including those with null values.
if (dataArray[i] == null) { // if the focal value is null, return null
zOut[i] = null;
} else {
let valuesConnected = multiplyArrays(dataArray, weightMatrix[i]); // will return null if either is null
let sumxj = sumArray(valuesConnected); // sum the values at connected locations
let weightRow = weightMatrix[i];
let weightRow_nulls = dataArray.map((x, j) => {if (x == null) {return null} else {return (weightRow[j])}})
let WiStar = sumArray(weightRow_nulls);
let S1iStar = sumArray(weightRow_nulls.map(wij => wij ** 2));
zOut[i] = (sumxj - (Xbar * WiStar)) / (s * (((nX * S1iStar) - WiStar ** 2) / (nX - 1)) ** 0.5)
}
}
return zOut;
}
// END