Geographically and Temporally Weighted Regression (GTWR)¶
Model Overview¶
Geographically and Temporally Weighted Regression (GTWR) extends GWR by incorporating temporal information via a spatio-temporal distance metric. GTWR assigns a spatio-temporal ratio parameter \(\lambda \in [0, 1]\) that balances the influence of spatial proximity versus temporal proximity on the weighting scheme.
Key features:
Supports automatic bandwidth selection via CV or AIC criteria.
Supports OpenMP parallel computation.
Provides standard errors and diagnostic statistics.
Constructor¶
from pygwmodel import GTWR, BandwidthWeight
from pygwmodel.spatial_weight import CRSSTDistance
algorithm = GTWR(
data, # GeoDataFrame
'PURCHASE', # dependent variable
['FLOORSZ', 'UNEMPLOY', 'PROF'], # independent variables
times='TIME', # temporal column
weight=BandwidthWeight(36.0, adaptive=True),
distance=CRSSTDistance(lambda_=0.5)
)
Parameters:
Parameter |
Type |
Description |
|---|---|---|
|
|
Input data with geometry |
|
|
Dependent variable column name |
|
|
Independent variable column names |
|
|
Column for temporal stamps |
|
|
Bandwidth configuration |
|
|
Spatio-temporal distance (default CRSSTDistance) |
|
|
Include intercept term (default True) |
Code Examples¶
Basic Fit¶
from pygwmodel import GTWR, BandwidthWeight
from pygwmodel.spatial_weight import CRSSTDistance
algorithm = GTWR(
data, 'PURCHASE', ['FLOORSZ', 'UNEMPLOY', 'PROF'],
times='TIME',
weight=BandwidthWeight(36.0, adaptive=True),
distance=CRSSTDistance(lambda_=0.5)
).fit()
print(algorithm.diagnostic)
# {'RSS': ..., 'AIC': ..., 'AICc': ..., 'RSquare': ...}
print(algorithm.result_layer.columns)
# Index(['Intercept', 'FLOORSZ', 'UNEMPLOY', 'PROF',
# 'Intercept_SE', 'FLOORSZ_SE', 'UNEMPLOY_SE', 'PROF_SE',
# 'fitted'])
Bandwidth Auto-Selection¶
algorithm = GTWR(
data, 'PURCHASE', ['FLOORSZ', 'UNEMPLOY', 'PROF'],
times='TIME',
weight=BandwidthWeight(36.0, adaptive=True),
distance=CRSSTDistance(lambda_=0.5)
).fit(optimize_bandwidth=GTWR.BandwidthSelectionCriterionType.CV)
print(f"Optimal bandwidth: {algorithm.weight.bandwidth}")
Prediction¶
prediction = algorithm.predict(data)
print(prediction.columns)
# Index(['Intercept', 'FLOORSZ', 'UNEMPLOY', 'PROF',
# 'y_hat', 'residual'])
Parallel Execution¶
from pygwmodel import GTWR, ParallelType
algorithm = GTWR(..., weight=...)
algorithm.enable_parallel(ParallelType.OpenMP, threads=4).fit()