RGWR¶
This page documents 1 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
RGWR¶
Classical robust geographically weighted regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import RGWR |
| Signature | RGWR(kernel: 'Union[str, Callable[[np.ndarray, float], np.ndarray]]' = 'gaussian', bandwidth: 'Union[float, int, str, None]' = 'cv', bandwidth_method: 'str' = 'cv', adaptive: 'bool' = False, bandwidth_range: 'Optional[Tuple[float, float]]' = None, optimization_method: 'str' = 'golden_section', fit_intercept: 'bool' = True, distance_metric: 'str' = 'euclidean', sigma2_v1: 'bool' = True, method: 'str' = 'automatic', max_iter: 'int' = 20, tol: 'float' = 1e-05, cut1: 'float' = 2.0, cut2: 'float' = 3.0, cut_filter: 'float' = 3.0, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/03_rgwr.py |
RGWR ¶
RGWR(
kernel: Union[
str, Callable[[ndarray, float], ndarray]
] = "gaussian",
bandwidth: Union[float, int, str, None] = "cv",
bandwidth_method: str = "cv",
adaptive: bool = False,
bandwidth_range: Optional[Tuple[float, float]] = None,
optimization_method: str = "golden_section",
fit_intercept: bool = True,
distance_metric: str = "euclidean",
sigma2_v1: bool = True,
method: str = "automatic",
max_iter: int = 20,
tol: float = 1e-05,
cut1: float = 2.0,
cut2: float = 3.0,
cut_filter: float = 3.0,
verbose: bool = False,
)
Bases: GWR
Classical robust geographically weighted regression.
RGWR first calibrates a standard Gaussian GWR using the requested kernel
and bandwidth. It then applies one of the robust procedures implemented by
the R package GWmodel:
"automatic"
Repeatedly combine the spatial kernel weights with a global residual
weight vector. Standardized residual magnitudes below cut1 receive
weight 1, values between cut1 and cut2 receive a smooth
bisquare transition, and values above cut2 receive weight 0.
"filtered"
Compute GWmodel-style studentized residuals from the initial GWR hat
matrix, exclude observations whose absolute residual exceeds
cut_filter, and refit once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
Union[str, Callable[[ndarray, float], ndarray]]
|
Spatial kernel name or callable accepted by :class: |
'gaussian'
|
bandwidth
|
Union[float, int, str, None]
|
Numeric bandwidth or automatic-selection criterion. Robust fitting uses the bandwidth selected by the initial standard GWR. |
'cv'
|
bandwidth_method
|
str
|
Criterion used when |
'cv'
|
adaptive
|
bool
|
Whether bandwidths represent nearest-neighbour counts. |
False
|
bandwidth_range
|
Optional[Tuple[float, float]]
|
Optional lower and upper bandwidth search bounds. |
None
|
optimization_method
|
str
|
One-dimensional bandwidth search method. |
'golden_section'
|
fit_intercept
|
bool
|
Whether to include a local intercept. |
True
|
distance_metric
|
str
|
Distance metric used by the spatial kernel. |
'euclidean'
|
sigma2_v1
|
bool
|
Residual-variance convention used for final GWR inference. |
True
|
method
|
str
|
Robust procedure, either |
'automatic'
|
max_iter
|
int
|
Maximum number of automatic robust refits. |
20
|
tol
|
float
|
Relative mean-squared-error tolerance for automatic convergence. |
1e-05
|
cut1
|
float
|
Lower standardized-residual threshold for automatic reweighting. |
2.0
|
cut2
|
float
|
Upper standardized-residual threshold for automatic reweighting. |
3.0
|
cut_filter
|
float
|
Absolute studentized-residual threshold for filtered RGWR. |
3.0
|
verbose
|
bool
|
Whether to print fit progress. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
robust_weights_ |
Observation-level residual weights used in the final robust refit. Filtered weights are exactly 0 or 1. |
|
outlier_mask_ |
Boolean mask identifying zero-weight observations. |
|
downweighted_mask_ |
Boolean mask identifying observations with final robust weights below 1. |
|
n_iter_ |
Number of robust refits after the initial standard GWR. |
|
converged_ |
Whether the automatic relative-MSE criterion was reached. Filtered RGWR is marked converged after its single refit. |
|
weight_history_ |
Robust weight vectors used by successive calibrations, beginning with the all-ones initial GWR weights. |
|
mse_history_ |
Initial and robust-refit mean squared residuals. |
|
convergence_history_ |
Relative MSE changes for automatic RGWR. |
|
initial_studentized_residuals_ |
GWmodel-style studentized residuals from the initial standard GWR. Populated for filtered RGWR. |
|
robust_residual_scores_ |
Final residuals divided by the root mean squared residual, matching the automatic weight-score definition. |
Notes
The robust weights are observation-level weights shared by every local
calibration. At location :math:s_i, the effective weights are
.. math::
w_{ij}^{\mathrm{effective}}
= w_{ij}^{\mathrm{spatial}} r_j,
where :math:r_j is the final residual weight for observation j.
Bandwidth selection is intentionally performed on the initial standard
GWR, matching the standard GWmodel workflow in which gwr.robust is
supplied a bandwidth selected by bw.gwr.
References
Harris, P., Fotheringham, A. S., and Juggins, S. (2010). Robust geographically weighted regression: a technique for quantifying spatial relationships between freshwater acidification critical loads and catchment attributes. Annals of the Association of American Geographers, 100(2), 286-306.
Lu, B., Harris, P., Charlton, M., and Brunsdon, C. (2014). The GWmodel R package: further topics for exploring spatial heterogeneity using geographically weighted models. Geo-spatial Information Science, 17(2), 85-101.
Source code in src/pygwrx/models/rgwr.py
fit ¶
fit(
X: Union[ndarray, DataFrame],
y: Union[ndarray, Series],
coords: Union[ndarray, DataFrame],
*,
compute_hat_matrix: bool = True,
compute_local_r2: bool = True,
compute_inference: bool = True,
compute_hat_matrix_flag: Optional[bool] = None,
verbose: Optional[bool] = None
) -> "RGWR"
Fit robust GWR and return the estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame]
|
Predictor matrix with shape |
required |
y
|
Union[ndarray, Series]
|
Response vector with shape |
required |
coords
|
Union[ndarray, DataFrame]
|
Coordinates with shape |
required |
compute_hat_matrix
|
bool
|
Whether to retain the final robust hat matrix. |
True
|
compute_local_r2
|
bool
|
Whether to compute local coefficients of determination. |
True
|
compute_inference
|
bool
|
Whether to retain parameter covariance factors, standard errors, and t values. |
True
|
compute_hat_matrix_flag
|
Optional[bool]
|
Compatibility alias for
|
None
|
verbose
|
Optional[bool]
|
Optional per-fit override of the estimator verbosity. |
None
|
Returns:
| Type | Description |
|---|---|
'RGWR'
|
The fitted robust estimator. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If robust filtering leaves too few usable observations for local calibration. |
Source code in src/pygwrx/models/rgwr.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
to_frame ¶
Return standard GWR results plus robust diagnostics.
Source code in src/pygwrx/models/rgwr.py
summary ¶
Return the standard GWR summary with robust-fit information.
Source code in src/pygwrx/models/rgwr.py
Runnable examples used on this page¶
examples/models/03_rgwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit robust GWR in automatic down-weighting mode."""
# Allow this script to run directly from any working directory.
import sys
from pathlib import Path
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
if str(_path) not in sys.path:
sys.path.insert(0, str(_path))
import numpy as np
from _common import print_model_result, spatial_regression
from pygwrx import RGWR
X, y, coords = spatial_regression()
y = y.copy()
y[[2, 20]] += np.array([5.0, -4.0])
model = RGWR(bandwidth=24, adaptive=True, max_iter=8).fit(X, y, coords)
print_model_result(model)
print("robust_weights=", model.robust_weights_[:8])
print("predictions=", model.predict(X.iloc[:3], coords.iloc[:3]))