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, spherical=False, hat_matrix=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
- coordsarray-like
n*2, collection of n sets of (x,y) coordinates of observatons; also used as calibration locations is ‘points’ is set to None
- yarray
n*1, dependent variable
- Xarray
n*k, independent variable, exlcuding the constant
- bwscalar
bandwidth value consisting of either a distance or N nearest neighbors; user specified or obtained using Sel_BW
- familyfamily object
underlying probability model; provides distribution-specific calculations
- offsetarray
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_v1boolean
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))
- kernelstring
type of kernel function used to weight observations; available options: ‘gaussian’ ‘bisquare’ ‘exponential’
- fixedboolean
True for distance based kernel function and False for adaptive (nearest neighbor) kernel function (default)
- constantboolean
True to include intercept (default) in model and False to exclude intercept.
- sphericalboolean
True for shperical coordinates (long-lat), False for projected coordinates (defalut).
- hat_matrixboolean
True to store full n by n hat matrix, False to not store full hat matrix to minimize memory footprint (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
- coordsarray-like
n*2, collection of n sets of (x,y) coordinates used for calibration locations
- yarray
n*1, dependent variable
- Xarray
n*k, independent variable, exlcuding the constant
- bwscalar
bandwidth value consisting of either a distance or N nearest neighbors; user specified or obtained using Sel_BW
- familyfamily object
underlying probability model; provides distribution-specific calculations
- offsetarray
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_v1boolean
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))
- kernelstring
type of kernel function used to weight observations; available options: ‘gaussian’ ‘bisquare’ ‘exponential’
- fixedboolean
True for distance based kernel function and False for adaptive (nearest neighbor) kernel function (default)
- constantboolean
True to include intercept (default) in model and False to exclude intercept
- sphericalboolean
True for shperical coordinates (long-lat), False for projected coordinates (defalut).
- hat_matrixboolean
True to store full n by n hat matrix, False to not store full hat matrix to minimize memory footprint (defalut).
- ninteger
number of observations
- kinteger
number of independent variables
- mean_yfloat
mean of y
- std_yfloat
standard deviation of y
- fit_paramsdict
parameters passed into fit method to define estimation routine
- pointsarray-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
- Parray
n*k, independent variables used to make prediction; exlcuding the constant; default to None unless specified in predict method
- exog_scalescalar
estimated scale using sampled locations; defualt is None unless specified in predict method
- exog_residarray-like
estimated residuals using sampled locations; defualt is None unless specified in predict method
Methods
fit
(self[, ini_params, tol, max_iter, …])Method that fits a model with a particular estimation routine.
predict
(self, points, P[, exog_scale, …])Method that predicts values of the dependent variable at un-sampled locations
df_model
df_resid