index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import spatialWeight from "@turf/distance-weight";
  2. import { featureEach } from "@turf/meta";
  3. /**
  4. * Moran's I measures patterns of attribute values associated with features.
  5. * The method reveal whether similar values tend to occur near each other,
  6. * or whether high or low values are interspersed.
  7. *
  8. * Moran's I > 0 means a clusterd pattern.
  9. * Moran's I < 0 means a dispersed pattern.
  10. * Moran's I = 0 means a random pattern.
  11. *
  12. * In order to test the significance of the result. The z score is calculated.
  13. * A positive enough z-score (ex. >1.96) indicates clustering,
  14. * while a negative enough z-score (ex. <-1.96) indicates a dispersed pattern.
  15. *
  16. * the z-score can be calculated based on a normal or random assumption.
  17. *
  18. * **Bibliography***
  19. *
  20. * 1. [Moran's I](https://en.wikipedia.org/wiki/Moran%27s_I)
  21. *
  22. * 2. [pysal](http://pysal.readthedocs.io/en/latest/index.html)
  23. *
  24. * 3. Andy Mitchell, The ESRI Guide to GIS Analysis Volume 2: Spatial Measurements & Statistics.
  25. *
  26. * @name moranIndex
  27. * @param {FeatureCollection<any>} fc
  28. * @param {Object} options
  29. * @param {string} options.inputField the property name, must contain numeric values
  30. * @param {number} [options.threshold=100000] the distance threshold
  31. * @param {number} [options.p=2] the Minkowski p-norm distance parameter
  32. * @param {boolean} [options.binary=false] whether transfrom the distance to binary
  33. * @param {number} [options.alpha=-1] the distance decay parameter
  34. * @param {boolean} [options.standardization=true] wheter row standardization the distance
  35. * @returns {MoranIndex}
  36. * @example
  37. *
  38. * const bbox = [-65, 40, -63, 42];
  39. * const dataset = turf.randomPoint(100, { bbox: bbox });
  40. *
  41. * const result = turf.moranIndex(dataset, {
  42. * inputField: 'CRIME',
  43. * });
  44. */
  45. export default function (fc, options) {
  46. var inputField = options.inputField;
  47. var threshold = options.threshold || 100000;
  48. var p = options.p || 2;
  49. var binary = options.binary || false;
  50. var alpha = options.alpha || -1;
  51. var standardization = options.standardization || true;
  52. var weight = spatialWeight(fc, {
  53. alpha: alpha,
  54. binary: binary,
  55. p: p,
  56. standardization: standardization,
  57. threshold: threshold,
  58. });
  59. var y = [];
  60. featureEach(fc, function (feature) {
  61. var feaProperties = feature.properties || {};
  62. // validate inputField exists
  63. y.push(feaProperties[inputField]);
  64. });
  65. var yMean = mean(y);
  66. var yVar = variance(y);
  67. var weightSum = 0;
  68. var s0 = 0;
  69. var s1 = 0;
  70. var s2 = 0;
  71. var n = weight.length;
  72. // validate y.length is the same as weight.length
  73. for (var i = 0; i < n; i++) {
  74. var subS2 = 0;
  75. for (var j = 0; j < n; j++) {
  76. weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);
  77. s0 += weight[i][j];
  78. s1 += Math.pow(weight[i][j] + weight[j][i], 2);
  79. subS2 += weight[i][j] + weight[j][i];
  80. }
  81. s2 += Math.pow(subS2, 2);
  82. }
  83. s1 = 0.5 * s1;
  84. var moranIndex = weightSum / s0 / yVar;
  85. var expectedMoranIndex = -1 / (n - 1);
  86. var vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);
  87. var vDen = (n - 1) * (n + 1) * (s0 * s0);
  88. var vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;
  89. var stdNorm = Math.sqrt(vNorm);
  90. var zNorm = (moranIndex - expectedMoranIndex) / stdNorm;
  91. return {
  92. expectedMoranIndex: expectedMoranIndex,
  93. moranIndex: moranIndex,
  94. stdNorm: stdNorm,
  95. zNorm: zNorm,
  96. };
  97. }
  98. /**
  99. * get mean of a list
  100. * @param {number[]} y
  101. * @returns {number}
  102. *
  103. */
  104. function mean(y) {
  105. var sum = 0;
  106. for (var _i = 0, y_1 = y; _i < y_1.length; _i++) {
  107. var item = y_1[_i];
  108. sum += item;
  109. }
  110. return sum / y.length;
  111. }
  112. /**
  113. * get variance of a list
  114. * @param {number[]} y
  115. * @returns {number}
  116. *
  117. */
  118. function variance(y) {
  119. var yMean = mean(y);
  120. var sum = 0;
  121. for (var _i = 0, y_2 = y; _i < y_2.length; _i++) {
  122. var item = y_2[_i];
  123. sum += Math.pow(item - yMean, 2);
  124. }
  125. return sum / y.length;
  126. }
  127. /**
  128. * @typedef {Object} MoranIndex
  129. * @property {number} moranIndex the moran's Index of the observed feature set
  130. * @property {number} expectedMoranIndex the moran's Index of the random distribution
  131. * @property {number} stdNorm the standard devitaion of the random distribution
  132. * @property {number} zNorm the z-score of the observe samples with regard to the random distribution
  133. */