# Spatial autocorrelation between two variables using Python

I have two variables in the data for which I want to calculate the spatial autocorrelation. I want to check how they are spatially related with each other.

I have already calculated the linear correlation between them. For spatial autocorrelation, I have calculated the Local Moran I value for a single variable.

How should I proceed further?

``````linear_corr = df['CH4'].corr(df['drain_area'])
``````

For spatial autocorrelation, but it is only for a single value

``````w = weights.distance.KNN.from_dataframe(df, k=4)
w.transform = "R"
lisa = esda.moran.Moran_Local(df["CH4"], w)
``````
• can you add the import statement corresponding to `weights` and `esda` ? Commented May 31, 2023 at 11:58

For spatial autocorrelation, there are some definitions as simple as a phenomenon that indicate that observations close to each other are more similar between them than to distant ones (Bivand et al., 2008; O’Sullivan and Unwin, 2010). It is important to note that, it is for the same variable, that is why it is AUTO-correlation and that it is across space, that is why it is spatial, but could also be across time. Some examples and explanations for the comparison between autocorrelation and correlation are available in Siabato and Guzmán-Manrique (2019).

Then, spatial autocorrelation between two variables sounds difficult. Maybe, what you are looking for is how the location of one variable explains the other. If that is the case, one possibility is to use modelling one variable using the coordinates of the other variable, which could help in controlling the spatial patterns identified in your previous analysis. The specifications of your model, and how simple it could be, would depend on your variables. One approach for analysing spatially correlated data is presented by Gałecki and Burzykowski (2013) in the chapter "Linear Model with Fixed Effects and Correlated Errors". It has an excellent theoretical development.

The references:

• O’Sullivan, D. and Unwin, D. J. (2003). Geographical Information Analysis. Wiley, Hoboken, NJ.
• Bivand, R.S., Pebesma, Edzer J., Gomez-Rubio, V., Pebesma, Edzer Jan, 2008. Applied spatial data analysis with R. Springer, New York.
• Siabato, W., Guzmán-Manrique, J., 2019. La autocorrelación espacial y el desarrollo de la geografía cuantitativa. Cuadernos de Geografía: Revista Colombiana de Geografía 28, 1-22.
• Gałecki, A. and Burzykowski, T. Linear mixed-effects model. Springer New York, 2013.
• This doesn't really answer the question (which is admittedly unclear), since it seems like they are looking for concrete next steps in Python. Commented Aug 23, 2023 at 14:18
• yes, but concrete next steps in Phyton would not be possible if there is a poor definition of the question. So First I tried to clarify it. Then how to approach the topic from a conceptual point of view, as there are noting specific variables or procedures. The the question could be reframed. Though that could be helpful.
– Alex
Commented Aug 24, 2023 at 11:25