Skip to content

Local Moran's I

local_morans_i(gdf, column, weight_type='queen', k=4, permutations=999)

Execute Local Moran's I calculation for the data.

Parameters:

Name Type Description Default
gdf GeoDataFrame

The geodataframe that contains the data to be examined with local morans I.

required
column str

The column to be used in the analysis.

required
weight_type Literal[queen, knn]

The type of spatial weights matrix to be used. Defaults to "queen".

'queen'
k int

Number of nearest neighbors for the KNN weights matrix. Defaults to 4.

4
permutations int

Number of permutations for significance testing. Defaults to 999.

999

Returns:

Type Description
GeoDataFrame

Geodataframe appended with two new columns: one with Local Moran's I statistic and one with p-value for the statistic.

Raises:

Type Description
EmptyDataFrameException

The input geodataframe is empty.

Source code in eis_toolkit/exploratory_analyses/local_morans_i.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@beartype
def local_morans_i(
    gdf: gpd.GeoDataFrame,
    column: str,
    weight_type: Literal["queen", "knn"] = "queen",
    k: int = 4,
    permutations: int = 999,
) -> gpd.GeoDataFrame:
    """Execute Local Moran's I calculation for the data.

    Args:
        gdf: The geodataframe that contains the data to be examined with local morans I.
        column: The column to be used in the analysis.
        weight_type: The type of spatial weights matrix to be used. Defaults to "queen".
        k: Number of nearest neighbors for the KNN weights matrix. Defaults to 4.
        permutations: Number of permutations for significance testing. Defaults to 999.

    Returns:
        Geodataframe appended with two new columns: one with Local Moran's I
          statistic and one with p-value for the statistic.

    Raises:
        EmptyDataFrameException: The input geodataframe is empty.
    """
    if gdf.shape[0] == 0:
        raise exceptions.EmptyDataFrameException("Geodataframe is empty.")

    if column not in gdf.columns:
        raise exceptions.InvalidParameterValueException(f"Column '{column}' not found in the GeoDataFrame.")

    if k < 1:
        raise exceptions.InvalidParameterValueException("k must be > 0.")

    if permutations < 100:
        raise exceptions.InvalidParameterValueException("permutations must be > 99.")

    calculations = _local_morans_i(gdf, column, weight_type, k, permutations)

    return calculations