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):

I am new to Python. Can someone suggest me how to do this using PySAL?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With