github.com/gopherd/gonum@v0.0.4/stat/spatial/spatial_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  
    10  	"github.com/gopherd/gonum/mat"
    11  	"github.com/gopherd/gonum/stat/spatial"
    12  )
    13  
    14  func ExampleGlobalMoransI_linear() {
    15  	data := []float64{0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
    16  
    17  	// The locality here describes spatial neighbor
    18  	// relationships.
    19  	locality := mat.NewDense(10, 10, []float64{
    20  		0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
    21  		1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    22  		0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
    23  		0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
    24  		0, 0, 0, 1, 0, 1, 0, 0, 0, 0,
    25  		0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
    26  		0, 0, 0, 0, 0, 1, 0, 1, 0, 0,
    27  		0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
    28  		0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
    29  		0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
    30  	})
    31  
    32  	i, _, z := spatial.GlobalMoransI(data, nil, locality)
    33  
    34  	fmt.Printf("Moran's I=%.4v z-score=%.4v\n", i, z)
    35  
    36  	// Output:
    37  	//
    38  	// Moran's I=0.1111 z-score=0.6335
    39  }
    40  
    41  func ExampleGlobalMoransI_banded() {
    42  	data := []float64{0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
    43  
    44  	// The locality here describes spatial neighbor
    45  	// relationships.
    46  	// This example uses the band matrix representation
    47  	// to improve time and space efficiency.
    48  	locality := mat.NewBandDense(10, 10, 1, 1, []float64{
    49  		0, 0, 1,
    50  		1, 0, 1,
    51  		1, 0, 1,
    52  		1, 0, 1,
    53  		1, 0, 1,
    54  		1, 0, 1,
    55  		1, 0, 1,
    56  		1, 0, 1,
    57  		1, 0, 1,
    58  		1, 0, 0,
    59  	})
    60  
    61  	i, _, z := spatial.GlobalMoransI(data, nil, locality)
    62  
    63  	fmt.Printf("Moran's I=%.4v z-score=%.4v\n", i, z)
    64  
    65  	// Output:
    66  	//
    67  	// Moran's I=0.1111 z-score=0.6335
    68  }
    69  
    70  func ExampleGetisOrdGStar() {
    71  	data := []float64{0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
    72  
    73  	// The locality here describes spatial neighbor
    74  	// relationships including self.
    75  	locality := mat.NewDense(10, 10, []float64{
    76  		1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
    77  		1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
    78  		0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
    79  		0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
    80  		0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
    81  		0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
    82  		0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
    83  		0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
    84  		0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
    85  		0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
    86  	})
    87  
    88  	for i, v := range data {
    89  		fmt.Printf("v=%v G*i=% .4v\n", v, spatial.GetisOrdGStar(i, data, nil, locality))
    90  	}
    91  
    92  	// Output:
    93  	//
    94  	// v=0 G*i=-1.225
    95  	// v=0 G*i=-1.604
    96  	// v=0 G*i=-0.2673
    97  	// v=1 G*i= 1.069
    98  	// v=1 G*i= 2.405
    99  	// v=1 G*i= 1.069
   100  	// v=0 G*i= 1.069
   101  	// v=1 G*i=-0.2673
   102  	// v=0 G*i=-0.2673
   103  	// v=0 G*i=-1.225
   104  }
   105  
   106  func ExampleGetisOrdGStar_banded() {
   107  	data := []float64{0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
   108  
   109  	// The locality here describes spatial neighbor
   110  	// relationships including self.
   111  	// This example uses the band matrix representation
   112  	// to improve time and space efficiency.
   113  	locality := mat.NewBandDense(10, 10, 1, 1, []float64{
   114  		0, 1, 1,
   115  		1, 1, 1,
   116  		1, 1, 1,
   117  		1, 1, 1,
   118  		1, 1, 1,
   119  		1, 1, 1,
   120  		1, 1, 1,
   121  		1, 1, 1,
   122  		1, 1, 1,
   123  		1, 1, 0,
   124  	})
   125  
   126  	for i, v := range data {
   127  		fmt.Printf("v=%v G*i=% .4v\n", v, spatial.GetisOrdGStar(i, data, nil, locality))
   128  	}
   129  
   130  	// Output:
   131  	//
   132  	// v=0 G*i=-1.225
   133  	// v=0 G*i=-1.604
   134  	// v=0 G*i=-0.2673
   135  	// v=1 G*i= 1.069
   136  	// v=1 G*i= 2.405
   137  	// v=1 G*i= 1.069
   138  	// v=0 G*i= 1.069
   139  	// v=1 G*i=-0.2673
   140  	// v=0 G*i=-0.2673
   141  	// v=0 G*i=-1.225
   142  }