gwr output

This commit is contained in:
Taylor Oshan 2016-11-28 15:53:59 -07:00
parent b39d0150c7
commit 8beb7220b2

View File

@ -16,7 +16,6 @@ def gwr(subquery, dep_var, ind_vars,
""" """
# query_result = subquery # query_result = subquery
# rowid = np.array(query_result[0]['rowid'])
params = {'geom_col': 'the_geom', params = {'geom_col': 'the_geom',
'id_col': 'cartodb_id', 'id_col': 'cartodb_id',
'subquery': subquery, 'subquery': subquery,
@ -31,6 +30,9 @@ def gwr(subquery, dep_var, ind_vars,
plpy.notice(query) plpy.notice(query)
plpy.error('Analysis failed: %s' % err) plpy.error('Analysis failed: %s' % err)
#unique ids and variable names list
rowid = np.array(query_result[0]['rowid'], dtype=np.int)
# TODO: should x, y be centroids? point on surface? # TODO: should x, y be centroids? point on surface?
# lat, long coordinates # lat, long coordinates
x = np.array(query_result[0]['x']) x = np.array(query_result[0]['x'])
@ -38,7 +40,7 @@ def gwr(subquery, dep_var, ind_vars,
coords = zip(x, y) coords = zip(x, y)
# extract dependent variable # extract dependent variable
Y = query_result[0]['dep_var'].reshape((-1, 1)) Y = np.array(query_result[0]['dep_var']).reshape((-1, 1))
n = Y.shape[0] n = Y.shape[0]
k = len(ind_vars) k = len(ind_vars)
@ -49,6 +51,9 @@ def gwr(subquery, dep_var, ind_vars,
X[:, attr] = np.array( X[:, attr] = np.array(
query_result[0][attr_name]).flatten() query_result[0][attr_name]).flatten()
#add intercept variable name
ind_vars.insert(0, 'intercept')
# calculate bandwidth # calculate bandwidth
bw = Sel_BW(coords, Y, X, bw = Sel_BW(coords, Y, X,
fixed=fixed, kernel=kernel).search() fixed=fixed, kernel=kernel).search()
@ -59,13 +64,18 @@ def gwr(subquery, dep_var, ind_vars,
# column called coeffs: # column called coeffs:
# {'pctrural': ..., 'pctpov': ..., ...} # {'pctrural': ..., 'pctpov': ..., ...}
# Follow the same structure for other outputs # Follow the same structure for other outputs
coefficients = model.params.reshape((-1,))
t_vals = model.tvalues.reshape((-1,)) coefficients = []
stand_errs = model.bse.reshape((-1)) stand_errs = []
predicted = np.repeat(model.predy.reshape((-1,)), k+1) t_vals = []
residuals = np.repeat(model.resid_response.reshape((-1,)), k+1) predicted = model.predy
r_squared = np.repeat(model.localR2.reshape((-1,)), k+1) residuals = model.resid_response
rowid = np.tile(rowid, k+1).reshape((-1,)) r_squared = model.localR2
ind_vars.insert(0, 'intercept')
var_name = np.tile(ind_vars, n).reshape((-1,)) for n, row in enumerate(Y):
return zip(coefficients, stand_errs, t_vals, predicted, residuals, r_squared, rowid, var_name) coefficients.append({var: model.params[n,k] for k, var in enumerate(ind_vars)})
stand_errs.append({var: model.bse[n,k] for k, var in enumerate(ind_vars)})
t_vals.append({var: model.tvalues[n,k] for k, var in enumerate(ind_vars)})
plpy.notice(str(zip(coefficients, stand_errs, t_vals, predicted, residuals, r_squared, rowid)))
return zip(coefficients, stand_errs, t_vals, predicted, residuals, r_squared, rowid)