Skip to content

better support of spatiotemporal models #69

@martinfleis

Description

@martinfleis

At the moment, if you define a Graph encoding spatiotemporal neighbourhoods, you can use it to fit the estimator and get a spatiotemporal regression. However, this breaks a bit when trying to do prediction (and score by extension), as we can identify local models to be used only by spatial dimension, as we only pass geometry.

We shall think about more explicit support of temporal dimension alongside the spatial one as the scaffolding is there. It would be a shame not to make use of it.

What I am less sure is how the API should look like. Temporal dimension should be treated in the same way as spatial one but that would mean adding args to init, fit and everything else.

We also need to figure out how to build the spatiotemporal graph with proper weights if we want to go this direction.

This will likely need some research and a few iterations to get it right. Some stuff related to the spatiotemporal graphs might even go to libpysal.

Here's an example how to build a simple spatiotemporal regression right now, with the caveat that the weights are binary and prediction is broken.

import geopandas as gpd
import pandas as pd
from libpysal import graph
from sklearn.metrics import pairwise_distances

from gwlearn.linear_model import GWLinearRegression

accidents = gpd.read_file(
    "https://martinfleischmann.net/sds/point_patterns/data/brno_pedestrian_accidents.gpkg"
)

timestamp = pd.to_datetime(
    accidents[["rok", "mesic", "den"]].rename(
        columns={"rok": "year", "mesic": "month", "den": "day"}
    )
)
accidents["days_since_first"] = (timestamp - timestamp.min()).dt.days

spatial_neighbors = graph.Graph.build_knn(accidents, k=200, coplanar="jitter")

temporal_distances = pairwise_distances(accidents["days_since_first"].values.reshape(-1, 1))
temporal_neighbors = graph.Graph.build_kernel(temporal_distances, metric="precomputed", bandwidth=365)

space_time_neighbors = temporal_neighbors.intersection(spatial_neighbors)

gwr = GWLinearRegression(graph=space_time_neighbors)
gwr.fit(
    X=accidents[["usmrceno_os", "tezce_zran_os", "lehce_zran_os"]],
    y=accidents['hmotna_skoda'])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions