khajlk
khajlk

Reputation: 861

Python - How do I compute interactive spatial autocorrelation (Moran I) using PySAL?

I have a points table my_table my in PostgreSQL database with geometry column and other attributes. I have some sample data of my_table like follows (attributes of my_table).

id  val1
1   72.54513286
2   73.67371014
3   74.204424
4   73.76017279
5   77.7912762
6   77.78789496
7   65.51822878
8   65.5182287
9   74.65885753
10  74.65885753
11  61.18084042
12  60.75827621
13  64.27716322
14  63.69432836
15  75.790405
16  60.95270235
17  79.12399503
18  62.9667706
19  78.1265630

Using Python PySAL package, I would like to analyse that whether values in column val1 are sptially autocorrelated (Moran I) (by interatively plotting them). My expected output of interactive spatial autocorrelation could be like (image source, here):

expected_output

I am new to Python. Can someone suggest me how to do this using PySAL?

Upvotes: 3

Views: 11642

Answers (2)

I have been using PySAL to compute Moran's I in my projects, and I find it relatively straightforward.

To compute Moran's I, you need two objects:

  1. A weight matrix that represents the degree of relationship between the cells for which you want to verify autocorrelation.
  2. The data itself.

In my projects, the cells I examine for autocorrelation are arranged like a mosaic. Therefore, I compute the weight matrix using the Contiguity Based Weights method, which is quite effective.

Here's an example of how to compute Moran's I for a two-dimensional data matrix, Z:

 from libpysal.weights import lat2W
 from esda.moran import Moran
 import numpy as np

 # Use your matrix here, instead of this random one
 Z = np.random.rand(200,150)

 # Create the matrix of weigthts 
 w = lat2W(Z.shape[0], Z.shape[1])

 # Create the pysal Moran object 
 mi = Moran(Z, w)

 # Verify Moran's I results 
 print(mi.I) 
 print(mi.p_norm)

I suggest starting with a random matrix, Z. In this instance, the outcome of Moran's I should approximate 0, serving as an effective test. Once you've verified it works with the random Z data, you can then substitute in your actual data.

Upvotes: 9

ziggy jones
ziggy jones

Reputation: 401

I guess you mean correlated not auto-correlated?

https://en.wikipedia.org/wiki/Autocorrelation

Can you use Pandas instead?

https://pandas.pydata.org/pandas-docs/stable/visualization.html

Pandas correlate

import pandas
import matplotlib.pyplot as plt

data = pandas.read_csv("C:\\Users\\4Sight\\Desktop\\test.csv", sep=" +", usecols=("val1", "val2"))

print data

print data.columns.values

print data["val1"].corr(data["val2"])

plt.figure()
data.plot()
plt.show()

Upvotes: -3

Related Questions