%matplotlib inline
import matplotlib.pyplot as plt
from libpysal.weights.contiguity import Queen
from libpysal import examples
import numpy as np
import pandas as pd
import geopandas as gpd
import os
import splot
link_to_data = examples.get_path('Guerry.shp')
gdf = gpd.read_file(link_to_data)
For this example we will focus on the Donatns (charitable donations per capita) variable. We will calculate Contiguity weights w
with libpysals
Queen.from_dataframe(gdf)
. Then we transform our weights to be row-standardized.
y = gdf['Donatns'].values
w = Queen.from_dataframe(gdf)
w.transform = 'r'
We calculate Moran's I. A test for global autocorrelation for a continuous attribute.
from esda.moran import Moran
w = Queen.from_dataframe(gdf)
moran = Moran(y, w)
moran.I
Our value for the statistic is interpreted against a reference distribution under the null hypothesis of complete spatial randomness. PySAL uses the approach of random spatial permutations.
from splot.esda import moran_scatterplot
fig, ax = moran_scatterplot(moran, aspect_equal=True)
plt.show()
from splot.esda import plot_moran
plot_moran(moran, zstandard=True, figsize=(10,4))
plt.show()
Our observed value is statistically significant:
moran.p_sim
In addition to visualizing Global autocorrelation statistics, splot has options to visualize local autocorrelation statistics. We compute the local Moran m
. Then, we plot the spatial lag and the Donatns variable in a Moran Scatterplot.
from splot.esda import moran_scatterplot
from esda.moran import Moran_Local
# calculate Moran_Local and plot
moran_loc = Moran_Local(y, w)
fig, ax = moran_scatterplot(moran_loc)
ax.set_xlabel('Donatns')
ax.set_ylabel('Spatial Lag of Donatns')
plt.show()
fig, ax = moran_scatterplot(moran_loc, p=0.05)
ax.set_xlabel('Donatns')
ax.set_ylabel('Spatial Lag of Donatns')
plt.show()
We can distinguish the specific type of local spatial autocorrelation in High-High, Low-Low, High-Low, Low-High. Where the upper right quadrant displays HH, the lower left, LL, the upper left LH and the lower left HL.
These types of local spatial autocorrelation describe similarities or dissimilarities between a specific polygon with its neighboring polygons. The upper left quadrant for example indicates that polygons with low values are surrounded by polygons with high values (LH). The lower right quadrant shows polygons with high values surrounded by neighbors with low values (HL). This indicates an association of dissimilar values.
Let's now visualize the areas we found to be significant on a map:
from splot.esda import lisa_cluster
lisa_cluster(moran_loc, gdf, p=0.05, figsize = (9,9))
plt.show()
from splot.esda import plot_local_autocorrelation
plot_local_autocorrelation(moran_loc, gdf, 'Donatns')
plt.show()
plot_local_autocorrelation(moran_loc, gdf, 'Donatns', quadrant=1)
plt.show()
Additionally, to assessing the correlation of one variable over space. It is possible to inspect the relationwhip of two variables and their position in space with so called Bivariate Moran Statistics. These can be found in esda.moran.Moran_BV
.
from esda.moran import Moran_BV, Moran_Local_BV
from splot.esda import plot_moran_bv_simulation, plot_moran_bv
Next to y
we will also be looking at the suicide rate x
.
x = gdf['Suicids'].values
Before we dive into Bivariate Moran startistics, let's make a quick overview which esda.moran
objects are supported by moran_scatterplot
:
moran = Moran(y,w)
moran_bv = Moran_BV(y, x, w)
moran_loc = Moran_Local(y, w)
moran_loc_bv = Moran_Local_BV(y, x, w)
fig, axs = plt.subplots(2, 2, figsize=(15,10),
subplot_kw={'aspect': 'equal'})
moran_scatterplot(moran, ax=axs[0,0])
moran_scatterplot(moran_loc, p=0.05, ax=axs[1,0])
moran_scatterplot(moran_bv, ax=axs[0,1])
moran_scatterplot(moran_loc_bv, p=0.05, ax=axs[1,1])
plt.show()
As you can see an easy moran_scatterplot
call provides you with loads of options. Now what are Bivariate Moran Statistics?
Bivariate Moran Statistics describe the correlation between one variable and the spatial lag of another variable. Therefore, we have to be careful interpreting our results. Bivariate Moran Statistics do not take the inherent correlation between the two variables at the same location into account. They much more offer a tool to measure the degree one polygon with a specific attribute is correlated with its neighboring polygons with a different attribute.
splot
can offer help interpreting the results by providing visualizations of reference distributions and a Moran Scatterplot:
plot_moran_bv(moran_bv)
plt.show()
Similar to univariate local Moran statistics pysal
and splot
offer tools to asses local autocorrelation for bivariate analysis:
from esda.moran import Moran_Local_BV
moran_loc_bv = Moran_Local_BV(x, y, w)
fig, ax = moran_scatterplot(moran_loc_bv, p=0.05)
ax.set_xlabel('Donatns')
ax.set_ylabel('Spatial lag of Suicids')
plt.show()
plot_local_autocorrelation(moran_loc_bv, gdf, 'Suicids')
plt.show()