# mgwr.gwr.GWR¶

class mgwr.gwr.GWR(coords, y, X, bw, family=<spglm.family.Gaussian object>, offset=None, sigma2_v1=True, kernel='bisquare', fixed=False, constant=True, dmat=None, sorted_dmat=None, spherical=False)[source]

Geographically weighted regression. Can currently estimate Gaussian, Poisson, and logistic models(built on a GLM framework). GWR object prepares model input. Fit method performs estimation and returns a GWRResults object.

Parameters: coords : array-like n*2, collection of n sets of (x,y) coordinates of observatons; also used as calibration locations is ‘points’ is set to None y : array n*1, dependent variable X : array n*k, independent variable, exlcuding the constant bw : scalar bandwidth value consisting of either a distance or N nearest neighbors; user specified or obtained using Sel_BW family : family object underlying probability model; provides distribution-specific calculations offset : array n*1, the offset variable at the ith location. For Poisson model this term is often the size of the population at risk or the expected size of the outcome in spatial epidemiology Default is None where Ni becomes 1.0 for all locations; only for Poisson models sigma2_v1 : boolean specify form of corrected denominator of sigma squared to use for model diagnostics; Acceptable options are: ‘True’: n-tr(S) (defualt) ‘False’: n-2(tr(S)+tr(S’S)) kernel : string type of kernel function used to weight observations; available options: ‘gaussian’ ‘bisquare’ ‘exponential’ fixed : boolean True for distance based kernel function and False for adaptive (nearest neighbor) kernel function (default) constant : boolean True to include intercept (default) in model and False to exclude intercept. dmat : array n*n, distance matrix between calibration locations used to compute weight matrix. Defaults to None and is primarily for avoiding duplicate computation during bandwidth selection. sorted_dmat : array n*n, sorted distance matrix between calibration locations used to compute weight matrix. Defaults to None and is primarily for avoiding duplicate computation during bandwidth selection. spherical : boolean True for shperical coordinates (long-lat), False for projected coordinates (defalut).

Examples

#basic model calibration

>>> import libpysal as ps
>>> from mgwr.gwr import GWR
>>> data = ps.io.open(ps.examples.get_path('GData_utm.csv'))
>>> coords = list(zip(data.by_col('X'), data.by_col('Y')))
>>> y = np.array(data.by_col('PctBach')).reshape((-1,1))
>>> rural = np.array(data.by_col('PctRural')).reshape((-1,1))
>>> pov = np.array(data.by_col('PctPov')).reshape((-1,1))
>>> african_amer = np.array(data.by_col('PctBlack')).reshape((-1,1))
>>> X = np.hstack([rural, pov, african_amer])
>>> model = GWR(coords, y, X, bw=90.000, fixed=False, kernel='bisquare')
>>> results = model.fit()
>>> print(results.params.shape)
(159, 4)


#predict at unsampled locations

>>> index = np.arange(len(y))
>>> test = index[-10:]
>>> X_test = X[test]
>>> coords_test = np.array(coords)[test]
>>> model = GWR(coords, y, X, bw=94, fixed=False, kernel='bisquare')
>>> results = model.predict(coords_test, X_test)
>>> print(results.params.shape)
(10, 4)

Attributes: coords : array-like n*2, collection of n sets of (x,y) coordinates used for calibration locations y : array n*1, dependent variable X : array n*k, independent variable, exlcuding the constant bw : scalar bandwidth value consisting of either a distance or N nearest neighbors; user specified or obtained using Sel_BW family : family object underlying probability model; provides distribution-specific calculations offset : array n*1, the offset variable at the ith location. For Poisson model this term is often the size of the population at risk or the expected size of the outcome in spatial epidemiology Default is None where Ni becomes 1.0 for all locations sigma2_v1 : boolean specify form of corrected denominator of sigma squared to use for model diagnostics; Acceptable options are: ‘True’: n-tr(S) (defualt) ‘False’: n-2(tr(S)+tr(S’S)) kernel : string type of kernel function used to weight observations; available options: ‘gaussian’ ‘bisquare’ ‘exponential’ fixed : boolean True for distance based kernel function and False for adaptive (nearest neighbor) kernel function (default) constant : boolean True to include intercept (default) in model and False to exclude intercept dmat : array n*n, distance matrix between calibration locations used to compute weight matrix. Defaults to None and is primarily for avoiding duplicate computation during bandwidth selection. sorted_dmat : array n*n, sorted distance matrix between calibration locations used to compute weight matrix. Defaults to None and is primarily for avoiding duplicate computation during bandwidth selection. spherical : boolean True for shperical coordinates (long-lat), False for projected coordinates (defalut). n : integer number of observations k : integer number of independent variables mean_y : float mean of y std_y : float standard deviation of y fit_params : dict parameters passed into fit method to define estimation routine W : array n*n, spatial weights matrix for weighting all observations from each calibration point points : array-like n*2, collection of n sets of (x,y) coordinates used for calibration locations instead of all observations; defaults to None unles specified in predict method P : array n*k, independent variables used to make prediction; exlcuding the constant; default to None unless specified in predict method exog_scale : scalar estimated scale using sampled locations; defualt is None unless specified in predict method exog_resid : array-like estimated residuals using sampled locations; defualt is None unless specified in predict method

Methods

 fit([ini_params, tol, max_iter, solve, …]) Method that fits a model with a particular estimation routine. predict(points, P[, exog_scale, exog_resid, …]) Method that predicts values of the dependent variable at un-sampled locations
 df_model df_resid
__init__(coords, y, X, bw, family=<spglm.family.Gaussian object>, offset=None, sigma2_v1=True, kernel='bisquare', fixed=False, constant=True, dmat=None, sorted_dmat=None, spherical=False)[source]

Initialize class