spint.gravity.Gravity

class spint.gravity.Gravity(flows, o_vars, d_vars, cost, cost_func, constant=True, framework='GLM', SF=None, CD=None, Lag=None, Quasi=False)[source]

Unconstrained (traditional gravity) gravity-type spatial interaction model

Parameters
flowsarray of integers

n x 1; observed flows between O origins and D destinations

costarray

n x 1; cost to overcome separation between each origin and destination associated with a flow; typically distance or time

cost_funcstring or function that has scalar input and output

functional form of the cost function; ‘exp’ | ‘pow’ | custom function

o_varsarray (optional)

n x p; p attributes for each origin of n flows; default is None

d_varsarray (optional)

n x p; p attributes for each destination of n flows; default is None

constantboolean

True to include intercept in model; True by default

frameworkstring

estimation technique; currently only ‘GLM’ is avaialble

Quasiboolean

True to estimate QuasiPoisson model; should result in same parameters as Poisson but with altered covariance; default to true which estimates Poisson model

SFarray

n x 1; eigenvector spatial filter to include in the model; default to None which does not include a filter; not yet implemented

CDarray

n x 1; competing destination term that accounts for the likelihood that alternative destinations are considered along with each destination under consideration for every OD pair; defaults to None which does not include a CD term; not yet implemented

LagW object

spatial weight for n observations (OD pairs) used to construct a spatial autoregressive model and estimator; defaults to None which does not include an autoregressive term; not yet implemented

Attributes
farray

n x 1; observed flows; dependent variable; y

ninteger

number of observations

kinteger

number of parameters

carray

n x 1; cost to overcome separation between each origin and destination associated with a flow; typically distance or time

cffunction

cost function; used to transform cost variable

ovarray

n x p(o); p attributes for each origin of n flows

dvarray

n x p(d); p attributes for each destination of n flows

constantboolean

True to include intercept in model; True by default

yarray

n x 1; dependent variable used in estimation including any transformations

Xarray

n x k, design matrix used in estimation

paramsarray

n x k, k estimated beta coefficients; k = p(o) + p(d) + 1

yhatarray

n x 1, predicted value of y (i.e., fittedvalues)

cov_paramsarray

Variance covariance matrix (kxk) of betas

std_errarray

k x 1, standard errors of betas

pvaluesarray

k x 1, two-tailed pvalues of parameters

tvaluesarray

k x 1, the tvalues of the standard errors

deviancefloat

value of the deviance function evalued at params; see family.py for distribution-specific deviance

resid_devarray

n x 1, residual deviance of model

llffloat

value of the loglikelihood function evalued at params; see family.py for distribution-specific loglikelihoods

llnullfloat

value of the loglikelihood function evaluated with only an intercept; see family.py for distribution-specific loglikelihoods

AICfloat

Akaike information criterion

D2float

percentage of explained deviance

adj_D2float

adjusted percentage of explained deviance

pseudo_R2float

McFadden’s pseudo R2 (coefficient of determination)

adj_pseudoR2float

adjusted McFadden’s pseudo R2

SRMSEfloat

standardized root mean square error

SSIfloat

Sorensen similarity index

resultsobject

Full results from estimated model. May contain addtional diagnostics

Example
——-
>>> import numpy as np
>>> import libpysal
>>> from spint.gravity import Gravity
>>> db = libpysal.io.open(libpysal.examples.get_path(‘nyc_bikes_ct.csv’))
>>> cost = np.array(db.by_col(‘tripduration’)).reshape((-1,1))
>>> flows = np.array(db.by_col(‘count’)).reshape((-1,1))
>>> o_cap = np.array(db.by_col(‘o_cap’)).reshape((-1,1))
>>> d_cap = np.array(db.by_col(‘d_cap’)).reshape((-1,1))
>>> model = Gravity(flows, o_cap, d_cap, cost, ‘exp’)
>>> model.params
array([ 3.80050153e+00, 5.54103854e-01, 3.94282921e-01, -2.27091686e-03])

Methods

fit(self[, framework, Quasi])

Method that fits a particular count model usign the appropriate estimation technique.

local(self, loc_index, locs)

Calibrate local models for subsets of data from a single location to all other locations

SRMSE

SSI

reshape

__init__(self, flows, o_vars, d_vars, cost, cost_func, constant=True, framework='GLM', SF=None, CD=None, Lag=None, Quasi=False)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

SRMSE(self)

SSI(self)

__init__(self, flows, o_vars, d_vars, cost, …)

Initialize self.

fit(self[, framework, Quasi])

Method that fits a particular count model usign the appropriate estimation technique.

local(self, loc_index, locs)

Calibrate local models for subsets of data from a single location to all other locations

reshape(self, array)