@turf/moran-index 6.5.0 → 7.0.0-alpha.1
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 +48 -29
- package/dist/es/index.js +41 -43
- package/dist/js/index.d.ts +1 -1
- package/dist/js/index.js +44 -48
- package/package.json +11 -9
package/README.md
CHANGED
|
@@ -9,77 +9,96 @@ The method reveal whether similar values tend to occur near each other,
|
|
|
9
9
|
or whether high or low values are interspersed.
|
|
10
10
|
|
|
11
11
|
Moran's I > 0 means a clusterd pattern.
|
|
12
|
-
Moran's I
|
|
12
|
+
Moran's I < 0 means a dispersed pattern.
|
|
13
13
|
Moran's I = 0 means a random pattern.
|
|
14
14
|
|
|
15
15
|
In order to test the significance of the result. The z score is calculated.
|
|
16
16
|
A positive enough z-score (ex. >1.96) indicates clustering,
|
|
17
|
-
while a negative enough z-score (ex.
|
|
17
|
+
while a negative enough z-score (ex. <-1.96) indicates a dispersed pattern.
|
|
18
18
|
|
|
19
19
|
the z-score can be calculated based on a normal or random assumption.
|
|
20
20
|
|
|
21
|
-
**Bibliography
|
|
21
|
+
**Bibliography**\*
|
|
22
22
|
|
|
23
|
-
1. [Moran's I]
|
|
23
|
+
1. [Moran's I][1]
|
|
24
24
|
|
|
25
|
-
2. [pysal]
|
|
25
|
+
2. [pysal][2]
|
|
26
26
|
|
|
27
27
|
3. Andy Mitchell, The ESRI Guide to GIS Analysis Volume 2: Spatial Measurements & Statistics.
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
### Parameters
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- `options.inputField` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the property name, must contain numeric values
|
|
34
|
-
- `options.threshold` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the distance threshold (optional, default `100000`)
|
|
35
|
-
- `options.p` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the Minkowski p-norm distance parameter (optional, default `2`)
|
|
36
|
-
- `options.binary` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether transfrom the distance to binary (optional, default `false`)
|
|
37
|
-
- `options.alpha` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the distance decay parameter (optional, default `-1`)
|
|
38
|
-
- `options.standardization` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** wheter row standardization the distance (optional, default `true`)
|
|
31
|
+
* `fc` **[FeatureCollection][3]\<any>**
|
|
32
|
+
* `options` **[Object][4]**
|
|
39
33
|
|
|
40
|
-
**
|
|
34
|
+
* `options.inputField` **[string][5]** the property name, must contain numeric values
|
|
35
|
+
* `options.threshold` **[number][6]** the distance threshold (optional, default `100000`)
|
|
36
|
+
* `options.p` **[number][6]** the Minkowski p-norm distance parameter (optional, default `2`)
|
|
37
|
+
* `options.binary` **[boolean][7]** whether transfrom the distance to binary (optional, default `false`)
|
|
38
|
+
* `options.alpha` **[number][6]** the distance decay parameter (optional, default `-1`)
|
|
39
|
+
* `options.standardization` **[boolean][7]** wheter row standardization the distance (optional, default `true`)
|
|
40
|
+
|
|
41
|
+
### Examples
|
|
41
42
|
|
|
42
43
|
```javascript
|
|
43
44
|
const bbox = [-65, 40, -63, 42];
|
|
44
45
|
const dataset = turf.randomPoint(100, { bbox: bbox });
|
|
45
46
|
|
|
46
|
-
const result = moranIndex(
|
|
47
|
+
const result = turf.moranIndex(dataset, {
|
|
47
48
|
inputField: 'CRIME',
|
|
48
49
|
});
|
|
49
50
|
```
|
|
50
51
|
|
|
51
|
-
Returns **[MoranIndex]
|
|
52
|
+
Returns **[MoranIndex][8]**
|
|
52
53
|
|
|
53
54
|
## mean
|
|
54
55
|
|
|
55
56
|
get mean of a list
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
### Parameters
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
* `y` **[Array][9]<[number][6]>**
|
|
60
61
|
|
|
61
|
-
Returns **[number]
|
|
62
|
+
Returns **[number][6]**
|
|
62
63
|
|
|
63
64
|
## variance
|
|
64
65
|
|
|
65
66
|
get variance of a list
|
|
66
67
|
|
|
67
|
-
|
|
68
|
+
### Parameters
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
* `y` **[Array][9]<[number][6]>**
|
|
70
71
|
|
|
71
|
-
Returns **[number]
|
|
72
|
+
Returns **[number][6]**
|
|
72
73
|
|
|
73
74
|
## MoranIndex
|
|
74
75
|
|
|
75
|
-
Type: [Object]
|
|
76
|
+
Type: [Object][4]
|
|
77
|
+
|
|
78
|
+
### Properties
|
|
79
|
+
|
|
80
|
+
* `moranIndex` **[number][6]** the moran's Index of the observed feature set
|
|
81
|
+
* `expectedMoranIndex` **[number][6]** the moran's Index of the random distribution
|
|
82
|
+
* `stdNorm` **[number][6]** the standard devitaion of the random distribution
|
|
83
|
+
* `zNorm` **[number][6]** the z-score of the observe samples with regard to the random distribution
|
|
84
|
+
|
|
85
|
+
[1]: https://en.wikipedia.org/wiki/Moran%27s_I
|
|
86
|
+
|
|
87
|
+
[2]: http://pysal.readthedocs.io/en/latest/index.html
|
|
88
|
+
|
|
89
|
+
[3]: https://tools.ietf.org/html/rfc7946#section-3.3
|
|
90
|
+
|
|
91
|
+
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
92
|
+
|
|
93
|
+
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
94
|
+
|
|
95
|
+
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
96
|
+
|
|
97
|
+
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
76
98
|
|
|
77
|
-
|
|
99
|
+
[8]: #moranindex
|
|
78
100
|
|
|
79
|
-
|
|
80
|
-
- `expectedMoranIndex` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the moran's Index of the random distribution
|
|
81
|
-
- `stdNorm` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the standard devitaion of the random distribution
|
|
82
|
-
- `zNorm` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the z-score of the observe samples with regard to the random distribution
|
|
101
|
+
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
83
102
|
|
|
84
103
|
<!-- This file is automatically generated. Please don't edit it directly:
|
|
85
104
|
if you find an error, edit the source file (likely index.js), and re-run
|
package/dist/es/index.js
CHANGED
|
@@ -43,36 +43,36 @@ import { featureEach } from "@turf/meta";
|
|
|
43
43
|
* });
|
|
44
44
|
*/
|
|
45
45
|
export default function (fc, options) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
alpha
|
|
54
|
-
binary
|
|
55
|
-
p
|
|
56
|
-
standardization
|
|
57
|
-
threshold
|
|
46
|
+
const inputField = options.inputField;
|
|
47
|
+
const threshold = options.threshold || 100000;
|
|
48
|
+
const p = options.p || 2;
|
|
49
|
+
const binary = options.binary || false;
|
|
50
|
+
const alpha = options.alpha || -1;
|
|
51
|
+
const standardization = options.standardization || true;
|
|
52
|
+
const weight = spatialWeight(fc, {
|
|
53
|
+
alpha,
|
|
54
|
+
binary,
|
|
55
|
+
p,
|
|
56
|
+
standardization,
|
|
57
|
+
threshold,
|
|
58
58
|
});
|
|
59
|
-
|
|
60
|
-
featureEach(fc,
|
|
61
|
-
|
|
59
|
+
const y = [];
|
|
60
|
+
featureEach(fc, (feature) => {
|
|
61
|
+
const feaProperties = feature.properties || {};
|
|
62
62
|
// validate inputField exists
|
|
63
63
|
y.push(feaProperties[inputField]);
|
|
64
64
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
65
|
+
const yMean = mean(y);
|
|
66
|
+
const yVar = variance(y);
|
|
67
|
+
let weightSum = 0;
|
|
68
|
+
let s0 = 0;
|
|
69
|
+
let s1 = 0;
|
|
70
|
+
let s2 = 0;
|
|
71
|
+
const n = weight.length;
|
|
72
72
|
// validate y.length is the same as weight.length
|
|
73
|
-
for (
|
|
74
|
-
|
|
75
|
-
for (
|
|
73
|
+
for (let i = 0; i < n; i++) {
|
|
74
|
+
let subS2 = 0;
|
|
75
|
+
for (let j = 0; j < n; j++) {
|
|
76
76
|
weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);
|
|
77
77
|
s0 += weight[i][j];
|
|
78
78
|
s1 += Math.pow(weight[i][j] + weight[j][i], 2);
|
|
@@ -81,18 +81,18 @@ export default function (fc, options) {
|
|
|
81
81
|
s2 += Math.pow(subS2, 2);
|
|
82
82
|
}
|
|
83
83
|
s1 = 0.5 * s1;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
84
|
+
const moranIndex = weightSum / s0 / yVar;
|
|
85
|
+
const expectedMoranIndex = -1 / (n - 1);
|
|
86
|
+
const vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);
|
|
87
|
+
const vDen = (n - 1) * (n + 1) * (s0 * s0);
|
|
88
|
+
const vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;
|
|
89
|
+
const stdNorm = Math.sqrt(vNorm);
|
|
90
|
+
const zNorm = (moranIndex - expectedMoranIndex) / stdNorm;
|
|
91
91
|
return {
|
|
92
|
-
expectedMoranIndex
|
|
93
|
-
moranIndex
|
|
94
|
-
stdNorm
|
|
95
|
-
zNorm
|
|
92
|
+
expectedMoranIndex,
|
|
93
|
+
moranIndex,
|
|
94
|
+
stdNorm,
|
|
95
|
+
zNorm,
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
@@ -102,9 +102,8 @@ export default function (fc, options) {
|
|
|
102
102
|
*
|
|
103
103
|
*/
|
|
104
104
|
function mean(y) {
|
|
105
|
-
|
|
106
|
-
for (
|
|
107
|
-
var item = y_1[_i];
|
|
105
|
+
let sum = 0;
|
|
106
|
+
for (const item of y) {
|
|
108
107
|
sum += item;
|
|
109
108
|
}
|
|
110
109
|
return sum / y.length;
|
|
@@ -116,10 +115,9 @@ function mean(y) {
|
|
|
116
115
|
*
|
|
117
116
|
*/
|
|
118
117
|
function variance(y) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
for (
|
|
122
|
-
var item = y_2[_i];
|
|
118
|
+
const yMean = mean(y);
|
|
119
|
+
let sum = 0;
|
|
120
|
+
for (const item of y) {
|
|
123
121
|
sum += Math.pow(item - yMean, 2);
|
|
124
122
|
}
|
|
125
123
|
return sum / y.length;
|
package/dist/js/index.d.ts
CHANGED
package/dist/js/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const distance_weight_1 = tslib_1.__importDefault(require("@turf/distance-weight"));
|
|
5
|
+
const meta_1 = require("@turf/meta");
|
|
8
6
|
/**
|
|
9
7
|
* Moran's I measures patterns of attribute values associated with features.
|
|
10
8
|
* The method reveal whether similar values tend to occur near each other,
|
|
@@ -48,36 +46,36 @@ var meta_1 = require("@turf/meta");
|
|
|
48
46
|
* });
|
|
49
47
|
*/
|
|
50
48
|
function default_1(fc, options) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
alpha
|
|
59
|
-
binary
|
|
60
|
-
p
|
|
61
|
-
standardization
|
|
62
|
-
threshold
|
|
49
|
+
const inputField = options.inputField;
|
|
50
|
+
const threshold = options.threshold || 100000;
|
|
51
|
+
const p = options.p || 2;
|
|
52
|
+
const binary = options.binary || false;
|
|
53
|
+
const alpha = options.alpha || -1;
|
|
54
|
+
const standardization = options.standardization || true;
|
|
55
|
+
const weight = distance_weight_1.default(fc, {
|
|
56
|
+
alpha,
|
|
57
|
+
binary,
|
|
58
|
+
p,
|
|
59
|
+
standardization,
|
|
60
|
+
threshold,
|
|
63
61
|
});
|
|
64
|
-
|
|
65
|
-
meta_1.featureEach(fc,
|
|
66
|
-
|
|
62
|
+
const y = [];
|
|
63
|
+
meta_1.featureEach(fc, (feature) => {
|
|
64
|
+
const feaProperties = feature.properties || {};
|
|
67
65
|
// validate inputField exists
|
|
68
66
|
y.push(feaProperties[inputField]);
|
|
69
67
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
const yMean = mean(y);
|
|
69
|
+
const yVar = variance(y);
|
|
70
|
+
let weightSum = 0;
|
|
71
|
+
let s0 = 0;
|
|
72
|
+
let s1 = 0;
|
|
73
|
+
let s2 = 0;
|
|
74
|
+
const n = weight.length;
|
|
77
75
|
// validate y.length is the same as weight.length
|
|
78
|
-
for (
|
|
79
|
-
|
|
80
|
-
for (
|
|
76
|
+
for (let i = 0; i < n; i++) {
|
|
77
|
+
let subS2 = 0;
|
|
78
|
+
for (let j = 0; j < n; j++) {
|
|
81
79
|
weightSum += weight[i][j] * (y[i] - yMean) * (y[j] - yMean);
|
|
82
80
|
s0 += weight[i][j];
|
|
83
81
|
s1 += Math.pow(weight[i][j] + weight[j][i], 2);
|
|
@@ -86,18 +84,18 @@ function default_1(fc, options) {
|
|
|
86
84
|
s2 += Math.pow(subS2, 2);
|
|
87
85
|
}
|
|
88
86
|
s1 = 0.5 * s1;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
const moranIndex = weightSum / s0 / yVar;
|
|
88
|
+
const expectedMoranIndex = -1 / (n - 1);
|
|
89
|
+
const vNum = n * n * s1 - n * s2 + 3 * (s0 * s0);
|
|
90
|
+
const vDen = (n - 1) * (n + 1) * (s0 * s0);
|
|
91
|
+
const vNorm = vNum / vDen - expectedMoranIndex * expectedMoranIndex;
|
|
92
|
+
const stdNorm = Math.sqrt(vNorm);
|
|
93
|
+
const zNorm = (moranIndex - expectedMoranIndex) / stdNorm;
|
|
96
94
|
return {
|
|
97
|
-
expectedMoranIndex
|
|
98
|
-
moranIndex
|
|
99
|
-
stdNorm
|
|
100
|
-
zNorm
|
|
95
|
+
expectedMoranIndex,
|
|
96
|
+
moranIndex,
|
|
97
|
+
stdNorm,
|
|
98
|
+
zNorm,
|
|
101
99
|
};
|
|
102
100
|
}
|
|
103
101
|
exports.default = default_1;
|
|
@@ -108,9 +106,8 @@ exports.default = default_1;
|
|
|
108
106
|
*
|
|
109
107
|
*/
|
|
110
108
|
function mean(y) {
|
|
111
|
-
|
|
112
|
-
for (
|
|
113
|
-
var item = y_1[_i];
|
|
109
|
+
let sum = 0;
|
|
110
|
+
for (const item of y) {
|
|
114
111
|
sum += item;
|
|
115
112
|
}
|
|
116
113
|
return sum / y.length;
|
|
@@ -122,10 +119,9 @@ function mean(y) {
|
|
|
122
119
|
*
|
|
123
120
|
*/
|
|
124
121
|
function variance(y) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
for (
|
|
128
|
-
var item = y_2[_i];
|
|
122
|
+
const yMean = mean(y);
|
|
123
|
+
let sum = 0;
|
|
124
|
+
for (const item of y) {
|
|
129
125
|
sum += Math.pow(item - yMean, 2);
|
|
130
126
|
}
|
|
131
127
|
return sum / y.length;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/moran-index",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-alpha.1",
|
|
4
4
|
"description": "turf moran-index module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"exports": {
|
|
29
29
|
"./package.json": "./package.json",
|
|
30
30
|
".": {
|
|
31
|
+
"types": "./dist/js/index.d.ts",
|
|
31
32
|
"import": "./dist/es/index.js",
|
|
32
33
|
"require": "./dist/js/index.js"
|
|
33
34
|
}
|
|
@@ -38,13 +39,13 @@
|
|
|
38
39
|
"dist"
|
|
39
40
|
],
|
|
40
41
|
"scripts": {
|
|
41
|
-
"bench": "
|
|
42
|
+
"bench": "tsx bench.js",
|
|
42
43
|
"build": "npm-run-all build:*",
|
|
43
44
|
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
|
|
44
45
|
"build:js": "tsc",
|
|
45
|
-
"docs": "
|
|
46
|
+
"docs": "tsx ../../scripts/generate-readmes",
|
|
46
47
|
"test": "npm-run-all test:*",
|
|
47
|
-
"test:tape": "
|
|
48
|
+
"test:tape": "tsx test.js"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"@types/tape": "*",
|
|
@@ -52,15 +53,16 @@
|
|
|
52
53
|
"load-json-file": "*",
|
|
53
54
|
"npm-run-all": "*",
|
|
54
55
|
"tape": "*",
|
|
55
|
-
"ts-node": "*",
|
|
56
56
|
"tslint": "*",
|
|
57
|
+
"tsx": "*",
|
|
57
58
|
"typescript": "*",
|
|
58
59
|
"write-json-file": "*"
|
|
59
60
|
},
|
|
60
61
|
"dependencies": {
|
|
61
|
-
"@turf/distance-weight": "^
|
|
62
|
-
"@turf/helpers": "^
|
|
63
|
-
"@turf/meta": "^
|
|
62
|
+
"@turf/distance-weight": "^7.0.0-alpha.1",
|
|
63
|
+
"@turf/helpers": "^7.0.0-alpha.1",
|
|
64
|
+
"@turf/meta": "^7.0.0-alpha.1",
|
|
65
|
+
"tslib": "^2.3.0"
|
|
64
66
|
},
|
|
65
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "cf7a0c507b017ca066acffd0ce23bda5b393fb5a"
|
|
66
68
|
}
|