mgwr(
data,
y,
x,
year=None,
*,
kernel='bisquare',
criterion='AICc',
max_iter=200,
tiles='carto-positron',
title=None,
gdf=None,
ref=None,
)
Multiscale geographically weighted regression: where, and at which scale, does x relate to y?
A regression of y on x is fitted at every unit, weighting the other units by the distance between centroids, so each coefficient becomes a surface that can be mapped. MGWR finds one bandwidth per term: a bandwidth close to the number of units means an association that is about the same everywhere (a global process), a small one an association that changes from place to place (a local process). It follows the workflow of the mgwr library (Oshan et al. 2019): the variables are z-standardized, so the coefficients are in standard deviations and comparable; the bandwidths are the numbers of nearest units, found by back-fitting; and each term’s local t-tests are corrected for multiple testing at the 5% level.
The search has a floor: the mgwr library does not try bandwidths below about 40 nearest units plus two per term, so a bandwidth at that floor means “as local as the search goes”, not exactly that many neighbours.
It is slow: the search fits the model again and again, which takes from half a minute to a minute and a half with several hundred units and one or two variables, and longer with more of either.
Parameters
| data |
|
A dataset id, or a panel with geo_id (and year), e.g. from load or combine |
required |
| y |
str |
The numeric column to explain |
required |
| x |
|
The explanatory column, or a list of them |
required |
| year |
|
The year to analyse (default: the last with data for y and x) |
None |
| kernel |
str |
How weights fall with distance: "bisquare", "gaussian" or "exponential" |
'bisquare' |
| criterion |
str |
What the bandwidth search minimises: "AICc", "AIC", "BIC" or "CV" |
'AICc' |
| max_iter |
int |
The most rounds of the bandwidth search (fewer is faster and rougher) |
200 |
| tiles |
str | None |
The basemap style (None: no basemap) |
'carto-positron' |
| title |
str | None |
The title of the residual map; each coefficient map adds its variable |
None |
| gdf |
|
Boundaries to use instead of the project’s (a GeoDataFrame with geo_id) |
None |
Returns
|
result object |
A result with .bw (the bandwidth of each term, in nearest units) and .bw_interval (its 95% interval); .df (one row per unit: <column>_coef, _se, _t and _significant for the intercept const and each column of x, the residual and local_cn, the local condition number, which signals local collinearity above about 30); .figs (a map of the local coefficients per term, by column name, with the units that are not significant in grey); .fig (the residual map); .gt (the summary table) and .gt_bw (the bandwidth table); .r2, .aicc, .adj_alpha and .critical_t (per term), .notes and .interpret(). |
Example
!pip install -q "geo4bolivia[mgwr]"
import geo4bolivia as geo
res = geo.mgwr("sdg", "index_sdg", "urbanization") # about half a minute
print(res.interpret())
res.figs["urbanization"]
MGWR let every term in the local regression of Sustainable Development Index, IDS (0-100) operate at its own spatial scale (variables z-standardized, 343 units); overall R² is 0.629.
Selected bandwidths: the intercept at 44 nearest neighbors (local); Urbanization rate (% of population) at 90 nearest neighbors (local). Smaller bandwidths mean the association shifts over shorter distances.
The Urbanization rate (% of population) association varies locally from 0.215 to 1.01; it is strongest around Ravelo (local coefficient 1.01). 326 of 343 units clear the corrected threshold (|t| ≥ 2.83); non-significant units are greyed on the map.
Coefficients are on the standardized scale (MGWR requires z-standardized variables), so magnitudes are comparable across terms; significance uses per-term corrected alphas.
These are associations, not causal effects. A causal reading needs a research design.