compute_global_spatial_autocorrelation

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