compute_global_spatial_autocorrelation

Format

compute_global_spatial_autocorrelation(table, column, weights=None, weights_def=None,
    save_weights_as=None, spatial_col=None, crs=None)

Parameters

The parameters for this pre-defined function are described in the following table.

Parameter Description
table Name of the spatial table.
column The column of interest whose values are used to compute the global autocorrelation index.
weights Required when trying to use spatial weights already stored in the data store. Internally it calls the function olm.ds.load. The supported parameters are ds_name and obj_name, indicating the data store name and object name, respectively.
weights_def Required if the parameter weights is not specified. Establishes the relationship between neighboring locations.

This is passed as a json object specifying the type of the weights definition and its parameters. Each parameter is defined in detail in the API Reference documentation.

The following lists the supported types and parameters:

  • KNN: [k]
  • Kernel: [bandwidth, fixed, k, function]
  • DistanceBand: [threshold, p, alpha, binary]
  • Queen
  • Rook
save_weights_as Only used if weights_def is specified. Specifies how the spatial weights are stored in the data store. The value is a json file that determines the parameters of oml.ds.save. The supported parameters are: [ds_name, obj_name, overwrite_ds, append, overwrite_obj, grantable, compression]. Some parameter names slightly differ from those in the oml.ds.save function. The parameter overwrite_obj is used to indicate whether an already existing object should be replaced with the current object.
spatial_col Specifies the column containing the geometries. The column can be specified in the table’s metadata. If not specified, the column name is retrieved from the table.
crs Specifies the Coordinate Reference System. If not specified, it is inferred from the table.

Example

The following example shows how to calculate Moran’s I statistic from a specific table column using spatial weights already saved in a data store. It uses the median_income column and the spatial weights obtained from the code example in compute_spatial_weights.

select *
    from table( 
        pyqEval(
            par_lst => '{  
                "oml_connect": true, 
                "table": "oml_user.la_block_groups", 
                "column": "median_income",
                "weights": {"ds_name":"spatial", "obj_name": "la_bg_knn4"}
            }',
            out_fmt => '{ "I": "NUMBER", "expected_I": "NUMBER",  "p_value": "NUMBER", "z_value": "NUMBER" }',
            scr_name => 'compute_global_spatial_autocorrelation'
        )
    );

The output contains the following fields:

  • The value of Moran’s I statistic.
  • The expected value under normality assumption.
  • The p-value.
  • The z-value.

The preceding example result will be similar to:

I	expected_I	p_value	z_value
0.6658882028	-0.0002910361	0.001	58.1778030148

If the spatial weights are not previously saved in a datastore, it is possible to calculate the Moran’s I statistic and the spatial weights according to the weights_def parameter. The following code calculates Moran’s I statistic of the MEDIAN_INCOME column and uses the Queen strategy (two observations are neighbors if they share at least a common vertex) to calculate the spatial weights, which are stored in the spatial datastore with the object name la_bg_queen.

select *
    from table( 
        pyqEval(
            '{  
                "oml_connect": true, 
                "table": "oml_user.la_block_groups", 
                "column": "median_income",
                "weights_def": {"type":"Queen"},
                "save_weights_as": {"ds_name":"spatial", "obj_name": "la_bg_queen", "append": true, "overwrite_obj": true}
            }',
            '{ "I": "NUMBER", "expected_I": "NUMBER",  "p_value": "NUMBER", "z_value": "NUMBER" }',
            'compute_global_spatial_autocorrelation'
        )
    );

The output of the Moran’s I statistic - the expected value under normality assumption, the p-value, and the z-value are as shown.

I	expected_I	p_value	z_value
0.6765793161	-0.0002910361	0.001	64.9421284293

You can list all the objects in a datastore using the oml.ds.describe function. The following code lists all the objects in the spatial datastore.

oml.ds.describe(name='spatial')

The output consists of all the objects in the spatial datastore, containing the previously created la_bg_knn4 and la_bg_queen objects.

object_name         class    size  length  row_count  col_count
0   la_bg_knn4  OMLDSWrapper  696002       1          1          1
1  la_bg_queen  OMLDSWrapper  295285       1          1          1