github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/stat/spatial/spatial_areal_example_test.go (about) 1 // Copyright ©2017 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package spatial_test 6 7 import ( 8 "fmt" 9 "math" 10 11 "github.com/jingcheng-WU/gonum/floats" 12 "github.com/jingcheng-WU/gonum/mat" 13 "github.com/jingcheng-WU/gonum/stat/spatial" 14 ) 15 16 // Euclid is a mat.Matrix whose elements refects the Euclidean 17 // distance between a series of unit-separated points strided 18 // to be arranged in an x by y grid. 19 type Euclid struct{ x, y int } 20 21 func (e Euclid) Dims() (r, c int) { return e.x * e.y, e.x * e.y } 22 func (e Euclid) At(i, j int) float64 { 23 d := e.x * e.y 24 if i < 0 || d <= i || j < 0 || d <= j { 25 panic("bounds error") 26 } 27 if i == j { 28 return 0 29 } 30 x := float64(j%e.x - i%e.x) 31 y := float64(j/e.x - i/e.x) 32 return 1 / math.Hypot(x, y) 33 } 34 func (e Euclid) T() mat.Matrix { return mat.Transpose{Matrix: e} } 35 36 func ExampleGlobalMoransI_areal() { 37 locality := Euclid{10, 10} 38 39 data1 := []float64{ 40 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 41 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 42 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 43 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 44 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 45 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 46 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 47 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 49 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 50 } 51 i1, _, z1 := spatial.GlobalMoransI(data1, nil, locality) 52 53 data2 := []float64{ 54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 57 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 58 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 59 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 60 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 61 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 62 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 63 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 64 } 65 i2, _, z2 := spatial.GlobalMoransI(data2, nil, locality) 66 67 fmt.Printf("%v scattered points Moran's I=%.4v z-score=%.4v\n", floats.Sum(data1), i1, z1) 68 fmt.Printf("%v clustered points Moran's I=%.4v z-score=%.4v\n", floats.Sum(data2), i2, z2) 69 70 // Output: 71 // 72 // 24 scattered points Moran's I=-0.02999 z-score=-1.913 73 // 24 clustered points Moran's I=0.09922 z-score=10.52 74 }