@turf/moran-index 7.0.0 → 7.1.0-alpha.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -28,8 +28,8 @@ the z-score can be calculated based on a normal or random assumption.
28
28
 
29
29
  ### Parameters
30
30
 
31
- * `fc` **[FeatureCollection][3]\<any>**
32
- * `options` **[Object][4]**
31
+ * `fc` **[FeatureCollection][3]\<any>**&#x20;
32
+ * `options` **[Object][4]**&#x20;
33
33
 
34
34
  * `options.inputField` **[string][5]** the property name, must contain numeric values
35
35
  * `options.threshold` **[number][6]** the distance threshold (optional, default `100000`)
@@ -49,7 +49,7 @@ const result = turf.moranIndex(dataset, {
49
49
  });
50
50
  ```
51
51
 
52
- Returns **[MoranIndex][8]**
52
+ Returns **[MoranIndex][8]**&#x20;
53
53
 
54
54
  ## mean
55
55
 
@@ -57,9 +57,9 @@ get mean of a list
57
57
 
58
58
  ### Parameters
59
59
 
60
- * `y` **[Array][9]<[number][6]>**
60
+ * `y` **[Array][9]<[number][6]>**&#x20;
61
61
 
62
- Returns **[number][6]**
62
+ Returns **[number][6]**&#x20;
63
63
 
64
64
  ## variance
65
65
 
@@ -67,9 +67,9 @@ get variance of a list
67
67
 
68
68
  ### Parameters
69
69
 
70
- * `y` **[Array][9]<[number][6]>**
70
+ * `y` **[Array][9]<[number][6]>**&#x20;
71
71
 
72
- Returns **[number][6]**
72
+ Returns **[number][6]**&#x20;
73
73
 
74
74
  ## MoranIndex
75
75
 
@@ -1,7 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // index.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
5
2
  var _distanceweight = require('@turf/distance-weight');
6
3
  var _meta = require('@turf/meta');
7
4
  function moranIndex(fc, options) {
@@ -56,7 +53,6 @@ function moranIndex(fc, options) {
56
53
  zNorm
57
54
  };
58
55
  }
59
- __name(moranIndex, "moranIndex");
60
56
  function mean(y) {
61
57
  let sum = 0;
62
58
  for (const item of y) {
@@ -64,7 +60,6 @@ function mean(y) {
64
60
  }
65
61
  return sum / y.length;
66
62
  }
67
- __name(mean, "mean");
68
63
  function variance(y) {
69
64
  const yMean = mean(y);
70
65
  let sum = 0;
@@ -73,7 +68,6 @@ function variance(y) {
73
68
  }
74
69
  return sum / y.length;
75
70
  }
76
- __name(variance, "variance");
77
71
  var turf_moran_index_default = moranIndex;
78
72
 
79
73
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../index.ts"],"names":["moranIndex"],"mappings":";;;;AACA,SAAS,kBAAkB,qBAAqB;AAChD,SAAS,mBAAmB;AA6C5B,SAAS,WACP,IACA,SAaA;AA9DF;AA+DE,QAAM,aAAa,QAAQ;AAC3B,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,IAAI,QAAQ,KAAK;AACvB,QAAM,UAAS,aAAQ,WAAR,YAAkB;AACjC,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,mBAAkB,aAAQ,oBAAR,YAA2B;AAEnD,QAAM,SAAS,cAAc,IAAI;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,IAAc,CAAC;AACrB,cAAY,IAAI,CAAC,YAAY;AAC3B,UAAM,gBAAgB,QAAQ,cAAc,CAAC;AAE7C,MAAE,KAAK,cAAc,UAAU,CAAC;AAAA,EAClC,CAAC;AAED,QAAM,QAAQ,KAAK,CAAC;AACpB,QAAM,OAAO,SAAS,CAAC;AACvB,MAAI,YAAY;AAChB,MAAI,KAAK;AACT,MAAI,KAAK;AACT,MAAI,KAAK;AACT,QAAM,IAAI,OAAO;AAEjB,WAAS,IAAI,GAAG,IAAI,GAAG,KAAK;AAC1B,QAAI,QAAQ;AACZ,aAAS,IAAI,GAAG,IAAI,GAAG,KAAK;AAC1B,mBAAa,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,IAAI,UAAU,EAAE,CAAC,IAAI;AACrD,YAAM,OAAO,CAAC,EAAE,CAAC;AACjB,YAAM,KAAK,IAAI,OAAO,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,EAAE,CAAC,GAAG,CAAC;AAC7C,eAAS,OAAO,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,EAAE,CAAC;AAAA,IACrC;AACA,UAAM,KAAK,IAAI,OAAO,CAAC;AAAA,EACzB;AACA,OAAK,MAAM;AAEX,QAAMA,cAAa,YAAY,KAAK;AACpC,QAAM,qBAAqB,MAAM,IAAI;AACrC,QAAM,OAAO,IAAI,IAAI,KAAK,IAAI,KAAK,KAAK,KAAK;AAC7C,QAAM,QAAQ,IAAI,MAAM,IAAI,MAAM,KAAK;AACvC,QAAM,QAAQ,OAAO,OAAO,qBAAqB;AACjD,QAAM,UAAU,KAAK,KAAK,KAAK;AAC/B,QAAM,SAASA,cAAa,sBAAsB;AAElD,SAAO;AAAA,IACL;AAAA,IACA,YAAAA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAxES;AAgFT,SAAS,KAAK,GAAqB;AACjC,MAAI,MAAM;AACV,aAAW,QAAQ,GAAG;AACpB,WAAO;AAAA,EACT;AACA,SAAO,MAAM,EAAE;AACjB;AANS;AAaT,SAAS,SAAS,GAAqB;AACrC,QAAM,QAAQ,KAAK,CAAC;AACpB,MAAI,MAAM;AACV,aAAW,QAAQ,GAAG;AACpB,WAAO,KAAK,IAAI,OAAO,OAAO,CAAC;AAAA,EACjC;AACA,SAAO,MAAM,EAAE;AACjB;AAPS;AAkBT,IAAO,2BAAQ","sourcesContent":["import { FeatureCollection } from \"geojson\";\nimport { distanceWeight as spatialWeight } from \"@turf/distance-weight\";\nimport { featureEach } from \"@turf/meta\";\n\n/**\n * Moran's I measures patterns of attribute values associated with features.\n * The method reveal whether similar values tend to occur near each other,\n * or whether high or low values are interspersed.\n *\n * Moran's I > 0 means a clusterd pattern.\n * Moran's I < 0 means a dispersed pattern.\n * Moran's I = 0 means a random pattern.\n *\n * In order to test the significance of the result. The z score is calculated.\n * A positive enough z-score (ex. >1.96) indicates clustering,\n * while a negative enough z-score (ex. <-1.96) indicates a dispersed pattern.\n *\n * the z-score can be calculated based on a normal or random assumption.\n *\n * **Bibliography***\n *\n * 1. [Moran's I](https://en.wikipedia.org/wiki/Moran%27s_I)\n *\n * 2. [pysal](http://pysal.readthedocs.io/en/latest/index.html)\n *\n * 3. Andy Mitchell, The ESRI Guide to GIS Analysis Volume 2: Spatial Measurements & Statistics.\n *\n * @name moranIndex\n * @param {FeatureCollection<any>} fc\n * @param {Object} options\n * @param {string} options.inputField the property name, must contain numeric values\n * @param {number} [options.threshold=100000] the distance threshold\n * @param {number} [options.p=2] the Minkowski p-norm distance parameter\n * @param {boolean} [options.binary=false] whether transfrom the distance to binary\n * @param {number} [options.alpha=-1] the distance decay parameter\n * @param {boolean} [options.standardization=true] wheter row standardization the distance\n * @returns {MoranIndex}\n * @example\n *\n * const bbox = [-65, 40, -63, 42];\n * const dataset = turf.randomPoint(100, { bbox: bbox });\n *\n * const result = turf.moranIndex(dataset, {\n * inputField: 'CRIME',\n * });\n */\n\nfunction moranIndex(\n fc: FeatureCollection<any>,\n options: {\n inputField: string;\n threshold?: number;\n p?: number;\n binary?: boolean;\n alpha?: number;\n standardization?: boolean;\n }\n): {\n moranIndex: number;\n expectedMoranIndex: number;\n stdNorm: number;\n zNorm: number;\n} {\n const inputField = options.inputField;\n const threshold = options.threshold || 100000;\n const p = options.p || 2;\n const binary = options.binary ?? false;\n const alpha = options.alpha || -1;\n const standardization = options.standardization ?? true;\n\n const weight = spatialWeight(fc, {\n alpha,\n binary,\n p,\n standardization,\n threshold,\n });\n\n const y: number[] = [];\n featureEach(fc, (feature) => {\n const feaProperties = feature.properties || {};\n // validate inputField exists\n y.push(feaProperties[inputField]);\n });\n\n const yMean = mean(y);\n const yVar = variance(y);\n let weightSum = 0;\n let s0 = 0;\n let s1 = 0;\n let s2 = 0;\n const n = weight.length;\n // validate y.length is the same as weight.length\n for (let i = 0; i < n; i++) {\n let subS2 = 0;\n for (let j = 0; j < n; j++) {\n weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);\n s0 += weight[i][j];\n s1 += Math.pow(weight[i][j] + weight[j][i], 2);\n subS2 += weight[i][j] + weight[j][i];\n }\n s2 += Math.pow(subS2, 2);\n }\n s1 = 0.5 * s1;\n\n const moranIndex = weightSum / s0 / yVar;\n const expectedMoranIndex = -1 / (n - 1);\n const vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);\n const vDen = (n - 1) * (n + 1) * (s0 * s0);\n const vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;\n const stdNorm = Math.sqrt(vNorm);\n const zNorm = (moranIndex - expectedMoranIndex) / stdNorm;\n\n return {\n expectedMoranIndex,\n moranIndex,\n stdNorm,\n zNorm,\n };\n}\n\n/**\n * get mean of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction mean(y: number[]): number {\n let sum = 0;\n for (const item of y) {\n sum += item;\n }\n return sum / y.length;\n}\n/**\n * get variance of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction variance(y: number[]): number {\n const yMean = mean(y);\n let sum = 0;\n for (const item of y) {\n sum += Math.pow(item - yMean, 2);\n }\n return sum / y.length;\n}\n\n/**\n * @typedef {Object} MoranIndex\n * @property {number} moranIndex the moran's Index of the observed feature set\n * @property {number} expectedMoranIndex the moran's Index of the random distribution\n * @property {number} stdNorm the standard devitaion of the random distribution\n * @property {number} zNorm the z-score of the observe samples with regard to the random distribution\n */\n\nexport { moranIndex };\nexport default moranIndex;\n"]}
1
+ {"version":3,"sources":["../../index.ts"],"names":["moranIndex"],"mappings":";AACA,SAAS,kBAAkB,qBAAqB;AAChD,SAAS,mBAAmB;AA6C5B,SAAS,WACP,IACA,SAaA;AA9DF;AA+DE,QAAM,aAAa,QAAQ;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,QAAQ,OAAO,OAAO,qBAAqB;AACjD,QAAM,UAAU,KAAK,KAAK,KAAK;AAC/B,QAAM,SAASA,cAAa,sBAAsB;AAElD,SAAO;AAAA,IACL;AAAA,IACA,YAAAA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQA,SAAS,KAAK,GAAqB;AACjC,MAAI,MAAM;AACV,aAAW,QAAQ,GAAG;AACpB,WAAO;AAAA,EACT;AACA,SAAO,MAAM,EAAE;AACjB;AAOA,SAAS,SAAS,GAAqB;AACrC,QAAM,QAAQ,KAAK,CAAC;AACpB,MAAI,MAAM;AACV,aAAW,QAAQ,GAAG;AACpB,WAAO,KAAK,IAAI,OAAO,OAAO,CAAC;AAAA,EACjC;AACA,SAAO,MAAM,EAAE;AACjB;AAWA,IAAO,2BAAQ","sourcesContent":["import { FeatureCollection } from \"geojson\";\nimport { distanceWeight as spatialWeight } from \"@turf/distance-weight\";\nimport { featureEach } from \"@turf/meta\";\n\n/**\n * Moran's I measures patterns of attribute values associated with features.\n * The method reveal whether similar values tend to occur near each other,\n * or whether high or low values are interspersed.\n *\n * Moran's I > 0 means a clusterd pattern.\n * Moran's I < 0 means a dispersed pattern.\n * Moran's I = 0 means a random pattern.\n *\n * In order to test the significance of the result. The z score is calculated.\n * A positive enough z-score (ex. >1.96) indicates clustering,\n * while a negative enough z-score (ex. <-1.96) indicates a dispersed pattern.\n *\n * the z-score can be calculated based on a normal or random assumption.\n *\n * **Bibliography***\n *\n * 1. [Moran's I](https://en.wikipedia.org/wiki/Moran%27s_I)\n *\n * 2. [pysal](http://pysal.readthedocs.io/en/latest/index.html)\n *\n * 3. Andy Mitchell, The ESRI Guide to GIS Analysis Volume 2: Spatial Measurements & Statistics.\n *\n * @name moranIndex\n * @param {FeatureCollection<any>} fc\n * @param {Object} options\n * @param {string} options.inputField the property name, must contain numeric values\n * @param {number} [options.threshold=100000] the distance threshold\n * @param {number} [options.p=2] the Minkowski p-norm distance parameter\n * @param {boolean} [options.binary=false] whether transfrom the distance to binary\n * @param {number} [options.alpha=-1] the distance decay parameter\n * @param {boolean} [options.standardization=true] wheter row standardization the distance\n * @returns {MoranIndex}\n * @example\n *\n * const bbox = [-65, 40, -63, 42];\n * const dataset = turf.randomPoint(100, { bbox: bbox });\n *\n * const result = turf.moranIndex(dataset, {\n * inputField: 'CRIME',\n * });\n */\n\nfunction moranIndex(\n fc: FeatureCollection<any>,\n options: {\n inputField: string;\n threshold?: number;\n p?: number;\n binary?: boolean;\n alpha?: number;\n standardization?: boolean;\n }\n): {\n moranIndex: number;\n expectedMoranIndex: number;\n stdNorm: number;\n zNorm: number;\n} {\n const inputField = options.inputField;\n const threshold = options.threshold || 100000;\n const p = options.p || 2;\n const binary = options.binary ?? false;\n const alpha = options.alpha || -1;\n const standardization = options.standardization ?? true;\n\n const weight = spatialWeight(fc, {\n alpha,\n binary,\n p,\n standardization,\n threshold,\n });\n\n const y: number[] = [];\n featureEach(fc, (feature) => {\n const feaProperties = feature.properties || {};\n // validate inputField exists\n y.push(feaProperties[inputField]);\n });\n\n const yMean = mean(y);\n const yVar = variance(y);\n let weightSum = 0;\n let s0 = 0;\n let s1 = 0;\n let s2 = 0;\n const n = weight.length;\n // validate y.length is the same as weight.length\n for (let i = 0; i < n; i++) {\n let subS2 = 0;\n for (let j = 0; j < n; j++) {\n weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);\n s0 += weight[i][j];\n s1 += Math.pow(weight[i][j] + weight[j][i], 2);\n subS2 += weight[i][j] + weight[j][i];\n }\n s2 += Math.pow(subS2, 2);\n }\n s1 = 0.5 * s1;\n\n const moranIndex = weightSum / s0 / yVar;\n const expectedMoranIndex = -1 / (n - 1);\n const vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);\n const vDen = (n - 1) * (n + 1) * (s0 * s0);\n const vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;\n const stdNorm = Math.sqrt(vNorm);\n const zNorm = (moranIndex - expectedMoranIndex) / stdNorm;\n\n return {\n expectedMoranIndex,\n moranIndex,\n stdNorm,\n zNorm,\n };\n}\n\n/**\n * get mean of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction mean(y: number[]): number {\n let sum = 0;\n for (const item of y) {\n sum += item;\n }\n return sum / y.length;\n}\n/**\n * get variance of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction variance(y: number[]): number {\n const yMean = mean(y);\n let sum = 0;\n for (const item of y) {\n sum += Math.pow(item - yMean, 2);\n }\n return sum / y.length;\n}\n\n/**\n * @typedef {Object} MoranIndex\n * @property {number} moranIndex the moran's Index of the observed feature set\n * @property {number} expectedMoranIndex the moran's Index of the random distribution\n * @property {number} stdNorm the standard devitaion of the random distribution\n * @property {number} zNorm the z-score of the observe samples with regard to the random distribution\n */\n\nexport { moranIndex };\nexport default moranIndex;\n"]}
package/dist/esm/index.js CHANGED
@@ -1,6 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
1
  // index.ts
5
2
  import { distanceWeight as spatialWeight } from "@turf/distance-weight";
6
3
  import { featureEach } from "@turf/meta";
@@ -56,7 +53,6 @@ function moranIndex(fc, options) {
56
53
  zNorm
57
54
  };
58
55
  }
59
- __name(moranIndex, "moranIndex");
60
56
  function mean(y) {
61
57
  let sum = 0;
62
58
  for (const item of y) {
@@ -64,7 +60,6 @@ function mean(y) {
64
60
  }
65
61
  return sum / y.length;
66
62
  }
67
- __name(mean, "mean");
68
63
  function variance(y) {
69
64
  const yMean = mean(y);
70
65
  let sum = 0;
@@ -73,7 +68,6 @@ function variance(y) {
73
68
  }
74
69
  return sum / y.length;
75
70
  }
76
- __name(variance, "variance");
77
71
  var turf_moran_index_default = moranIndex;
78
72
  export {
79
73
  turf_moran_index_default as default,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { FeatureCollection } from \"geojson\";\nimport { distanceWeight as spatialWeight } from \"@turf/distance-weight\";\nimport { featureEach } from \"@turf/meta\";\n\n/**\n * Moran's I measures patterns of attribute values associated with features.\n * The method reveal whether similar values tend to occur near each other,\n * or whether high or low values are interspersed.\n *\n * Moran's I > 0 means a clusterd pattern.\n * Moran's I < 0 means a dispersed pattern.\n * Moran's I = 0 means a random pattern.\n *\n * In order to test the significance of the result. The z score is calculated.\n * A positive enough z-score (ex. >1.96) indicates clustering,\n * while a negative enough z-score (ex. <-1.96) indicates a dispersed pattern.\n *\n * the z-score can be calculated based on a normal or random assumption.\n *\n * **Bibliography***\n *\n * 1. [Moran's I](https://en.wikipedia.org/wiki/Moran%27s_I)\n *\n * 2. [pysal](http://pysal.readthedocs.io/en/latest/index.html)\n *\n * 3. Andy Mitchell, The ESRI Guide to GIS Analysis Volume 2: Spatial Measurements & Statistics.\n *\n * @name moranIndex\n * @param {FeatureCollection<any>} fc\n * @param {Object} options\n * @param {string} options.inputField the property name, must contain numeric values\n * @param {number} [options.threshold=100000] the distance threshold\n * @param {number} [options.p=2] the Minkowski p-norm distance parameter\n * @param {boolean} [options.binary=false] whether transfrom the distance to binary\n * @param {number} [options.alpha=-1] the distance decay parameter\n * @param {boolean} [options.standardization=true] wheter row standardization the distance\n * @returns {MoranIndex}\n * @example\n *\n * const bbox = [-65, 40, -63, 42];\n * const dataset = turf.randomPoint(100, { bbox: bbox });\n *\n * const result = turf.moranIndex(dataset, {\n * inputField: 'CRIME',\n * });\n */\n\nfunction moranIndex(\n fc: FeatureCollection<any>,\n options: {\n inputField: string;\n threshold?: number;\n p?: number;\n binary?: boolean;\n alpha?: number;\n standardization?: boolean;\n }\n): {\n moranIndex: number;\n expectedMoranIndex: number;\n stdNorm: number;\n zNorm: number;\n} {\n const inputField = options.inputField;\n const threshold = options.threshold || 100000;\n const p = options.p || 2;\n const binary = options.binary ?? false;\n const alpha = options.alpha || -1;\n const standardization = options.standardization ?? true;\n\n const weight = spatialWeight(fc, {\n alpha,\n binary,\n p,\n standardization,\n threshold,\n });\n\n const y: number[] = [];\n featureEach(fc, (feature) => {\n const feaProperties = feature.properties || {};\n // validate inputField exists\n y.push(feaProperties[inputField]);\n });\n\n const yMean = mean(y);\n const yVar = variance(y);\n let weightSum = 0;\n let s0 = 0;\n let s1 = 0;\n let s2 = 0;\n const n = weight.length;\n // validate y.length is the same as weight.length\n for (let i = 0; i < n; i++) {\n let subS2 = 0;\n for (let j = 0; j < n; j++) {\n weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);\n s0 += weight[i][j];\n s1 += Math.pow(weight[i][j] + weight[j][i], 2);\n subS2 += weight[i][j] + weight[j][i];\n }\n s2 += Math.pow(subS2, 2);\n }\n s1 = 0.5 * s1;\n\n const moranIndex = weightSum / s0 / yVar;\n const expectedMoranIndex = -1 / (n - 1);\n const vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);\n const vDen = (n - 1) * (n + 1) * (s0 * s0);\n const vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;\n const stdNorm = Math.sqrt(vNorm);\n const zNorm = (moranIndex - expectedMoranIndex) / stdNorm;\n\n return {\n expectedMoranIndex,\n moranIndex,\n stdNorm,\n zNorm,\n };\n}\n\n/**\n * get mean of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction mean(y: number[]): number {\n let sum = 0;\n for (const item of y) {\n sum += item;\n }\n return sum / y.length;\n}\n/**\n * get variance of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction variance(y: number[]): number {\n const yMean = mean(y);\n let sum = 0;\n for (const item of y) {\n sum += Math.pow(item - yMean, 2);\n }\n return sum / y.length;\n}\n\n/**\n * @typedef {Object} MoranIndex\n * @property {number} moranIndex the moran's Index of the observed feature set\n * @property {number} expectedMoranIndex the moran's Index of the random distribution\n * @property {number} stdNorm the standard devitaion of the random distribution\n * @property {number} zNorm the z-score of the observe samples with regard to the random distribution\n */\n\nexport { moranIndex };\nexport default moranIndex;\n"],"mappings":";;;;AACA,SAAS,kBAAkB,qBAAqB;AAChD,SAAS,mBAAmB;AA6C5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7C,QAAM,QAAQ,IAAI,MAAM,IAAI,MAAM,KAAK;AACvC,QAAM,QAAQ,OAAO,OAAO,qBAAqB;AACjD,QAAM,UAAU,KAAK,KAAK,KAAK;AAC/B,QAAM,SAASA,cAAa,sBAAsB;AAElD,SAAO;AAAA,IACL;AAAA,IACA,YAAAA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAxES;AAgFT,SAAS,KAAK,GAAqB;AACjC,MAAI,MAAM;AACV,aAAW,QAAQ,GAAG;AACpB,WAAO;AAAA,EACT;AACA,SAAO,MAAM,EAAE;AACjB;AANS;AAaT,SAAS,SAAS,GAAqB;AACrC,QAAM,QAAQ,KAAK,CAAC;AACpB,MAAI,MAAM;AACV,aAAW,QAAQ,GAAG;AACpB,WAAO,KAAK,IAAI,OAAO,OAAO,CAAC;AAAA,EACjC;AACA,SAAO,MAAM,EAAE;AACjB;AAPS;AAkBT,IAAO,2BAAQ;","names":["moranIndex"]}
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { FeatureCollection } from \"geojson\";\nimport { distanceWeight as spatialWeight } from \"@turf/distance-weight\";\nimport { featureEach } from \"@turf/meta\";\n\n/**\n * Moran's I measures patterns of attribute values associated with features.\n * The method reveal whether similar values tend to occur near each other,\n * or whether high or low values are interspersed.\n *\n * Moran's I > 0 means a clusterd pattern.\n * Moran's I < 0 means a dispersed pattern.\n * Moran's I = 0 means a random pattern.\n *\n * In order to test the significance of the result. The z score is calculated.\n * A positive enough z-score (ex. >1.96) indicates clustering,\n * while a negative enough z-score (ex. <-1.96) indicates a dispersed pattern.\n *\n * the z-score can be calculated based on a normal or random assumption.\n *\n * **Bibliography***\n *\n * 1. [Moran's I](https://en.wikipedia.org/wiki/Moran%27s_I)\n *\n * 2. [pysal](http://pysal.readthedocs.io/en/latest/index.html)\n *\n * 3. Andy Mitchell, The ESRI Guide to GIS Analysis Volume 2: Spatial Measurements & Statistics.\n *\n * @name moranIndex\n * @param {FeatureCollection<any>} fc\n * @param {Object} options\n * @param {string} options.inputField the property name, must contain numeric values\n * @param {number} [options.threshold=100000] the distance threshold\n * @param {number} [options.p=2] the Minkowski p-norm distance parameter\n * @param {boolean} [options.binary=false] whether transfrom the distance to binary\n * @param {number} [options.alpha=-1] the distance decay parameter\n * @param {boolean} [options.standardization=true] wheter row standardization the distance\n * @returns {MoranIndex}\n * @example\n *\n * const bbox = [-65, 40, -63, 42];\n * const dataset = turf.randomPoint(100, { bbox: bbox });\n *\n * const result = turf.moranIndex(dataset, {\n * inputField: 'CRIME',\n * });\n */\n\nfunction moranIndex(\n fc: FeatureCollection<any>,\n options: {\n inputField: string;\n threshold?: number;\n p?: number;\n binary?: boolean;\n alpha?: number;\n standardization?: boolean;\n }\n): {\n moranIndex: number;\n expectedMoranIndex: number;\n stdNorm: number;\n zNorm: number;\n} {\n const inputField = options.inputField;\n const threshold = options.threshold || 100000;\n const p = options.p || 2;\n const binary = options.binary ?? false;\n const alpha = options.alpha || -1;\n const standardization = options.standardization ?? true;\n\n const weight = spatialWeight(fc, {\n alpha,\n binary,\n p,\n standardization,\n threshold,\n });\n\n const y: number[] = [];\n featureEach(fc, (feature) => {\n const feaProperties = feature.properties || {};\n // validate inputField exists\n y.push(feaProperties[inputField]);\n });\n\n const yMean = mean(y);\n const yVar = variance(y);\n let weightSum = 0;\n let s0 = 0;\n let s1 = 0;\n let s2 = 0;\n const n = weight.length;\n // validate y.length is the same as weight.length\n for (let i = 0; i < n; i++) {\n let subS2 = 0;\n for (let j = 0; j < n; j++) {\n weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);\n s0 += weight[i][j];\n s1 += Math.pow(weight[i][j] + weight[j][i], 2);\n subS2 += weight[i][j] + weight[j][i];\n }\n s2 += Math.pow(subS2, 2);\n }\n s1 = 0.5 * s1;\n\n const moranIndex = weightSum / s0 / yVar;\n const expectedMoranIndex = -1 / (n - 1);\n const vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);\n const vDen = (n - 1) * (n + 1) * (s0 * s0);\n const vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;\n const stdNorm = Math.sqrt(vNorm);\n const zNorm = (moranIndex - expectedMoranIndex) / stdNorm;\n\n return {\n expectedMoranIndex,\n moranIndex,\n stdNorm,\n zNorm,\n };\n}\n\n/**\n * get mean of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction mean(y: number[]): number {\n let sum = 0;\n for (const item of y) {\n sum += item;\n }\n return sum / y.length;\n}\n/**\n * get variance of a list\n * @param {number[]} y\n * @returns {number}\n *\n */\nfunction variance(y: number[]): number {\n const yMean = mean(y);\n let sum = 0;\n for (const item of y) {\n sum += Math.pow(item - yMean, 2);\n }\n return sum / y.length;\n}\n\n/**\n * @typedef {Object} MoranIndex\n * @property {number} moranIndex the moran's Index of the observed feature set\n * @property {number} expectedMoranIndex the moran's Index of the random distribution\n * @property {number} stdNorm the standard devitaion of the random distribution\n * @property {number} zNorm the z-score of the observe samples with regard to the random distribution\n */\n\nexport { moranIndex };\nexport default moranIndex;\n"],"mappings":";AACA,SAAS,kBAAkB,qBAAqB;AAChD,SAAS,mBAAmB;AA6C5B,SAAS,WACP,IACA,SAaA;AA9DF;AA+DE,QAAM,aAAa,QAAQ;AAC3B,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,IAAI,QAAQ,KAAK;AACvB,QAAM,UAAS,aAAQ,WAAR,YAAkB;AACjC,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,mBAAkB,aAAQ,oBAAR,YAA2B;AAEnD,QAAM,SAAS,cAAc,IAAI;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,IAAc,CAAC;AACrB,cAAY,IAAI,CAAC,YAAY;AAC3B,UAAM,gBAAgB,QAAQ,cAAc,CAAC;AAE7C,MAAE,KAAK,cAAc,UAAU,CAAC;AAAA,EAClC,CAAC;AAED,QAAM,QAAQ,KAAK,CAAC;AACpB,QAAM,OAA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AAWA,IAAO,2BAAQ;","names":["moranIndex"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/moran-index",
3
- "version": "7.0.0",
3
+ "version": "7.1.0-alpha.70+948cdafaf",
4
4
  "description": "turf moran-index module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -64,10 +64,11 @@
64
64
  "write-json-file": "^5.0.0"
65
65
  },
66
66
  "dependencies": {
67
- "@turf/distance-weight": "^7.0.0",
68
- "@turf/helpers": "^7.0.0",
69
- "@turf/meta": "^7.0.0",
67
+ "@turf/distance-weight": "^7.1.0-alpha.70+948cdafaf",
68
+ "@turf/helpers": "^7.1.0-alpha.70+948cdafaf",
69
+ "@turf/meta": "^7.1.0-alpha.70+948cdafaf",
70
+ "@types/geojson": "^7946.0.10",
70
71
  "tslib": "^2.6.2"
71
72
  },
72
- "gitHead": "3d3a7917025fbabe191dbddbc89754b86f9c7739"
73
+ "gitHead": "948cdafaf70606d2e27fcc79973fa48ee1182067"
73
74
  }