diff --git a/src/pg/sql/16_getis.sql b/src/pg/sql/16_getis.sql index 23547f1..dc3a25d 100644 --- a/src/pg/sql/16_getis.sql +++ b/src/pg/sql/16_getis.sql @@ -6,10 +6,11 @@ CREATE OR REPLACE FUNCTION column_name TEXT, w_type TEXT DEFAULT 'knn', num_ngbrs INT DEFAULT 5, + permutations INT DEFAULT 99, geom_col TEXT DEFAULT 'the_geom', id_col TEXT DEFAULT 'cartodb_id') -RETURNS TABLE (z_val NUMERIC, p_val NUMERIC, rowid BIGINT) +RETURNS TABLE (z_val NUMERIC, p_val NUMERIC, p_z_sim NUMERIC, rowid BIGINT) AS $$ from crankshaft.clustering import getis_ord - return getis_ord(subquery, column_name, w_type, num_ngbrs, geom_col, id_col) + return getis_ord(subquery, column_name, w_type, num_ngbrs, permutations, geom_col, id_col) $$ LANGUAGE plpythonu; diff --git a/src/py/crankshaft/crankshaft/clustering/getis.py b/src/py/crankshaft/crankshaft/clustering/getis.py index 8cbbd56..1ee425a 100644 --- a/src/py/crankshaft/crankshaft/clustering/getis.py +++ b/src/py/crankshaft/crankshaft/clustering/getis.py @@ -1,10 +1,7 @@ """ -Moran's I geostatistics (global clustering & outliers presence) +Getis-Ord's G geostatistics (hotspot/coldspot analysis) """ -# TODO: Fill in local neighbors which have null/NoneType values with the -# average of the their neighborhood - import pysal as ps import plpy from collections import OrderedDict @@ -15,11 +12,11 @@ import crankshaft.pysal_utils as pu # High level interface --------------------------------------- def getis_ord(subquery, attr, - w_type, num_ngbrs, geom_col, id_col): + w_type, num_ngbrs, permutations, geom_col, id_col): """ - Getis-Ord's G - Implementation building neighbors with a PostGIS database and Getis-Ord's G - hotspot/coldspot analysis with PySAL. + Getis-Ord's G* + Implementation building neighbors with a PostGIS database and PySAL's Getis-Ord's G* + hotspot/coldspot module. Andy Eschbacher """ @@ -38,14 +35,17 @@ def getis_ord(subquery, attr, result = plpy.execute(query) # if there are no neighbors, exit if len(result) == 0: - return pu.empty_zipped_array(3) + return pu.empty_zipped_array(4) except plpy.SPIError, err: plpy.error('Query failed: %s' % err) attr_vals = pu.get_attributes(result) + + ## build PySAL weight object weight = pu.get_weight(result, w_type, num_ngbrs) - # calculate LISA values - getis = ps.esda.getisord.G_Local(attr_vals, weight, star=True) + # calculate Getis-Ord's G* z- and p-values + getis = ps.esda.getisord.G_Local(attr_vals, weight, + star=True, permutations=permutations) - return zip(getis.z_sim, getis.p_sim, weight.id_order) + return zip(getis.z_sim, getis.p_sim, getis.p_z_sim, weight.id_order)