← Back to Software
Python Package Guidance

SGWR Code Implementation

Run the SGWR model sequentially or with MPI-parallel processing, configure its kernel, and extract the primary model diagnostics.

Install the packagepip install sgwr
01

Sequential implementation

For the default adaptive bisquare kernel, initialize the joint bandwidth-and-alpha search and then pass the optimized parameters to the SGWR model:

Python · Adaptive bisquare kernel
selector = ALPHA((u, v), y, X, data)
BW, Alpha = selector.fit()

model = SGWR(
    (u, v),
    y,
    X,
    BW,
    data,
    Alpha
).fit()

The first step initializes the parameter-search procedure. The selector.fit() method jointly identifies the optimized bandwidth (BW) and alpha (Alpha). These optimized values are then passed to the SGWR model for final estimation.

02

Fixed Gaussian kernel

For a fixed Gaussian kernel, use the same implementation and specify the additional kernel arguments in both the search and estimation stages:

Python · Fixed Gaussian kernel
selector = ALPHA(
    (u, v),
    y,
    X,
    data,
    fixed=True,
    kernel="gaussian"
)

BW, Alpha = selector.fit()

model = SGWR(
    (u, v),
    y,
    X,
    BW,
    data,
    Alpha,
    fixed=True,
    kernel="gaussian"
).fit()
03

Model diagnostics

After fitting the model, common goodness-of-fit and model-comparison statistics can be obtained directly from the fitted model object:

Python · Diagnostic outputs
R2 = model.R2
Adj_R2 = model.adj_R2
AIC = model.aic
AICc = model.aicc
RSS = np.sum(model.resid_response ** 2)

These outputs provide the primary statistics needed to evaluate SGWR model fit and compare alternative model specifications.

04

Parallel MPI implementation

For larger datasets, the package provides an MPI implementation that distributes SGWR computations across multiple processors. Users must first install MPI and mpi4py. The input CSV must follow this column order:

longitudelatitudedependent variablepredictor 1predictor 2…

The basic parallel command is:

Terminal · Basic execution
fastsgwr run -np x -data path_to_data

Here, x is the number of processors or CPU cores and path_to_data is the path to the input dataset. By default, the parallel implementation uses the adaptive bisquare kernel and does not standardize the variables.

05

Command-line options

Optional flags can be added to select the kernel, standardize variables, run conventional GWR, or access the earlier SGWR implementation:

Terminal · Optional configurations
# Fixed Gaussian kernel
fastsgwr run -np x -data path_to_data -fixed

# Standardize variables
fastsgwr run -np x -data path_to_data -standardize

# Run conventional GWR
fastsgwr run -np x -data path_to_data -gwr

# Fixed Gaussian kernel with standardization
fastsgwr run -np x -data path_to_data -fixed -standardize

# Access the earlier SGWR implementation
fastsgwr run -np x -data path_to_data -biga

The order of optional suffixes does not affect execution; for example, -fixed -standardize and -standardize -fixed are equivalent. After execution, the software prints key diagnostics, including R², adjusted R², and AICc, and saves the detailed model output as a CSV file in the input-data directory.