M-SGWR Code Implementation
Run the serial M-SGWR model, optimize predictor-specific bandwidths and alpha values with AICc or cross-validation, and extract model results.
pip install sgwrPrepare the data
Arrange the CSV columns as x-coordinate, y-coordinate, dependent variable, and then the predictor variables. The code below reads those fields, constructs coordinate pairs, and uses the predictors to build the attribute-similarity weights.
from sgwr import MSGWR, Sel_BW
import pandas as pd
import numpy as np
df = pd.read_csv(r"Data.csv")
columns = df.columns[3:]
print("Predictors:", columns)
g_y = df[df.columns[2]].values.reshape(-1, 1)
g_x = df[columns].values
u = df[df.columns[0]].values
v = df[df.columns[1]].values
g_coords = list(zip(u, v))
# Predictors used to construct attribute weights
data = g_xIf standardization is appropriate for the analysis, import StandardScaler from sklearn.preprocessing and transform both g_x and g_y before creating the selector.
Optimize with AICc
Create a multiscale selector and run the default AICc-based search. The notebook uses the greedy hill-climbing approach with fixed alpha step sizes; the commented alternative activates divide-and-conquer alpha optimization.
# Divide-and-conquer alpha optimization
# selector = Sel_BW(
# g_coords, g_y, g_x, data,
# multi=True, alphacurve=True
# )
# Greedy hill-climbing alpha optimization
selector = Sel_BW(
g_coords,
g_y,
g_x,
data,
columns,
multi=True
)
msgwr_bw = selector.search()
print("Optimal bandwidths:", msgwr_bw)
print(
"Optimal alphas:",
np.array([value[-1] for value in selector.bw[-1].values()])
)
msgwr_fit = MSGWR(
g_coords,
g_y,
g_x,
data,
selector
).fit()Optimize with cross-validation
To use cross-validation instead of AICc, retain the same data preparation and selector configuration, then set the search criterion to CV before fitting the model.
selector = Sel_BW(
g_coords,
g_y,
g_x,
data,
columns,
multi=True
)
msgwr_bw = selector.search(criterion="CV")
print("Optimal bandwidths:", msgwr_bw)
print(
"Optimal alphas:",
np.array([value[-1] for value in selector.bw[-1].values()])
)
msgwr_fit = MSGWR(
g_coords,
g_y,
g_x,
data,
selector
).fit()Review diagnostics and the model summary
Use the fitted model object to retrieve the principal goodness-of-fit statistics and print the full M-SGWR summary table.
print("R2:", msgwr_fit.R2)
print("Adjusted R2:", msgwr_fit.adj_R2)
print("AIC:", msgwr_fit.aic)
print("AICc:", msgwr_fit.aicc)
print("BIC:", msgwr_fit.bic)
print("RSS:", np.sum(msgwr_fit.resid_response ** 2))
print(msgwr_fit.summary())Extract coefficients and test significance
Extract the local coefficient matrix from params. The filtered t-value methods set non-significant values to zero, either with the model-adjusted threshold or a specified confidence level.
# Local coefficient estimates
local_coefficients = msgwr_fit.params
# Model-adjusted significance threshold
filtered_tvalues = msgwr_fit.filter_tvals()
# Filter using a 95% confidence level
filtered_tvalues_95 = msgwr_fit.filter_tvals(alpha=0.05)