-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetisOrdGiStar.js
89 lines (70 loc) · 3.87 KB
/
GetisOrdGiStar.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
// 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.
// nsim : number of permutations (for testGetisOrdGiStar).
// Notes: increasing the number of permutations (nsim) may cause an infinite
// loop error in codepen.io, or loading delays.
// Parameters for main functions:
// GetisOrdGiStar(dataArray, weightMatrix)
// GetisOrdGi(dataArray, weightMatrix)
// GetisOrdGiStarZ(dataArray, weightMatrix)
// GetisOrdGiZ(dataArray, weightMatrix)
// permuteGetisOrdGiStar(dataArray, weightMatrix)
// testGetisOrdGiStar(dataArray, weightMatrix, nsim = 999)
// rowStandardize(weightMatrix)
//------------------------------------------------------------------------------//
// Getis-Ord Gi*
// Gi* includes location i in the summations; Gi does not.
// Getis-Ord Gi*
// Returns an Array containing the Gi* statistics for each location.
// Requires GetisOrdG_helpers.js
// START
function GetisOrdGiStar(dataArray, weightMatrix) {
let n = weightMatrix.length; // row count = number of locations (n)
let totalSum = sumArray(dataArray); // sum values in data array
let giStarOut = new Array(n); // make blank array to populate
for (let i = 0; i < n; i++) { // calculate Gi* statistics for each location
let sumjConnectedToiIncludingi = sumArray(multiplyArrays(dataArray, weightMatrix[i]))
giStarOut[i] = sumjConnectedToiIncludingi / totalSum;
}
return giStarOut; // return array of Gi* values
}
// END
// Example calls:
// console.log(GetisOrdGiStar(peakValleyFlat, bMat));
// console.log(GetisOrdGiStar(peakValleyFlat, rowStandardize(bMat)));
// For larger data set:
// console.log(GetisOrdGiStar(jsonObject1, rowStandardize(jsonObject2)));
// let t0 = performance.now();
// GetisOrdGiStar(jsonObject1, rowStandardize(jsonObject2));
// let t1 = performance.now();
// console.log("Time taken: " + ((t1 - t0) / 1000) + " seconds")
// Half a second to calculate all Gi* values.