# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Compute Moran's I spatial autocorrelation coefficient

Contributed by:
Roman Parker

ResourceFunction["MoranI"][ evaluates Moran's | |

ResourceFunction["MoranI"][ evaluates Moran's |

Moran's *I* can be computed by , where *w*_{ij} is the value at position (*i*,*j*) in *w* (the weights matrix), *n* is the length of the values list, is the mean of the values list and *y*_{i} is the *i*^{th} element of the values list.

Moran's *I* can also be computed using , where *n* and *w* are identical to the previous definition and *z* is equal to *y*-Mean[*y*].

In Moranl[*values*,*weights*], *weights* must be a square matrix with length equal to the length of values and zeroes on the top-left–bottom-right diagonal, whose values represent the distance between the regions.

ResourceFunction["MoranI"] returns a real value *n* between -1 and 1, as follows:

n = 1 | perfect correlation (values with a nonzero weight have identical values) |

n ≈ 0 | no correlation with the weight matrix used (usually corresponding to random data, an improper weights matrix or no correlation) |

n=-1 | perfect anticorrelation (values which have a nonzero weight have exactly opposite values) |

Often, although not always, *weights* is symmetrical along this diagonal, since the distance function used in its computation is usually commutative.

Moran's *I* is primarily used for geography/GIS, but can be applied in other fields.

ResourceFunction["MoranI"] can be used for data in any number of dimensions, since the input is only the values and distances, but multivariate autocorrelation is not supported.

When *weights* is not provided, the weights matrix is predefined such that the weights value at location (*x*,*y*) in the table is 1 if *x* and *y* are "next to" each other in *values*, and 0 otherwise.

There is no good way to select a weights matrix without knowing the source and shape of the data, but as long as the values are in a resonable order, the default *weights* matrix should provide somewhat meaningful results.

AutocorrelationTest and ResourceFunction["MoranI"] both use a values list and calculate the correlation of that list with itself, but unlike AutocorrelationTest, which uses either adjacent elements in the list or a lag of arbitrary length, ResourceFunction["MoranI"] can be used with an arbitrary weights matrix to define the relations between elements.

Moran's *I* allows for setting weight values other than 0 and 1 (especially useful if the weight of correlation is proportional to the distance), while AutocorrelationTest assumes that elements exactly 1 "step" away (or exactly *n* steps, if you are using a lag value) have a weight of 1 and everything else has a weight of 0.

Moran's *I* is very specific to an individual weight matrix, giving completely different results if the weights are changed. Due to this, values returned by ResourceFunction["MoranI"] cannot be compared with each other if the weights matrix is different, making it less useful for surveys across multiple regions.

ResourceFunction["MoranI"] supports symbolic computation.

Define and show our values list:

In[1]:= |

Out[1]= |

Use the default weights matrix to compute MoranI:

In[2]:= |

Out[2]= |

Show a custom weights matrix:

In[3]:= |

Out[3]= |

Compute MoranI using this matrix instead:

In[4]:= |

Out[4]= |

Define 25 square regions in a grid and display them:

In[5]:= |

Out[5]= |

Define a values list where every other cell has a value of 1 and the rest have a value of 0:

In[6]:= |

Out[6]= |

Use a predefined weights matrix instead of a manual one for computing MoranI:

In[7]:= |

Out[7]= |

MoranI supports symbolic computation:

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

Create a list of four points:

In[10]:= |

Out[10]= |

Create a weights matrix based on how close together the points are:

In[11]:= |

Out[12]= |

Create random numbers as values for each point:

In[13]:= |

Out[13]= |

Find the mean spatial autocorrelation of several different value lists over the same weights matrix:

In[14]:= |

Out[14]= |

The weights matrix cannot total to zero, since that would cause Σ_{i}Σ_{j}*w*_{ij} (the denominator of the second fraction) to equal zero and lead to an indeterminate answer:

In[15]:= |

Out[15]= |

The first input cannot have all elements be equal, since that leads to (the first denominator) to also equal zero, giving an indeterminate answer:

In[16]:= |

Out[16]= |

Define a weights matrix corresponding to whether each pair of squares touch and show it:

In[17]:= |

In[18]:= |

Out[18]= |

Compute the extent to which squares touching each other corresponds to them having similar values:

In[19]:= |

Out[19]= |

Show the default weights matrix with length 25:

In[20]:= |

Out[20]= |

Define a function to compute the length of the border between two countries:

In[21]:= |

Compute the border length between every pair of countries in South America:

In[22]:= |

Compute the army size of each country:

In[23]:= |

Find how much sharing a long border is correlated to similar army size:

In[24]:= |

Out[24]= |

- 1.0.1 – 20 September 2021

This work is licensed under a Creative Commons Attribution 4.0 International License