Skip to contents

Computes the Local Moran's I statistic for each spatiotemporal unit (spatiotemporal segment) using a memory-efficient, iterative approach. This function can handle both segmented_network and spatiotemporal_network objects.

Usage

calculate_local_moran(network_object, dist_threshold = 1, time_threshold = 2)

Arguments

network_object

A segmented_network or spatiotemporal_network object with events assigned via set_events().

dist_threshold

The spatial distance threshold to define neighbors.

time_threshold

The temporal distance threshold (in hours) to define neighbors.

Value

The input network object with a new component $moran_results. This is a data frame containing the Local Moran's I statistic (I), z-scores (z), spatially lagged z-scores (lagged_z), and cluster classification (classification) for each spatiotemporal segment.

Examples

# First, create a network and assign events
network_with_events <- sample_roads |>
  create_road_network() |>
  create_spatiotemporal_network(spatial_length = 0.5) |>
  set_events(sample_accidents)

# Then, calculate Local Moran's I
moran_result <- calculate_local_moran(
  network_with_events,
  dist_threshold = 1,
  time_threshold = 2
)

# View the results
head(moran_result$moran_results)
#>   segment_index time spatiotemporal_segment_id x  segment_id            z
#> 1             1    0                         1 0 sl_00000001 -0.007309942
#> 2             2    0                         2 0 sl_00000002 -0.007309942
#> 3             3    0                         3 0 sl_00000003 -0.007309942
#> 4             4    0                         4 0 sl_00000004 -0.007309942
#> 5             5    0                         5 0 sl_00000005 -0.007309942
#> 6             6    0                         6 0 sl_00000006 -0.007309942
#>       lagged_z            I has_neighbors classification
#> 1 -0.007309942 5.343525e-05          TRUE             LL
#> 2 -0.007309942 5.343525e-05          TRUE             LL
#> 3 -0.007309942 5.343525e-05          TRUE             LL
#> 4 -0.007309942 5.343525e-05          TRUE             LL
#> 5 -0.007309942 5.343525e-05          TRUE             LL
#> 6 -0.007309942 5.343525e-05          TRUE             LL

# Plot the results
plot_local_moran(moran_result, snapshot_time = 12)

plot_local_moran(moran_result, plot_3d = TRUE)