trafficCAR-intro

Introduction

trafficCAR provides tools for constructing conditional autoregressive (CAR) precision matrices on graphs derived from road networks. The package supports network creation from spatial linework, basic spatial weight construction, and proper/ICAR precision matrices that can be used in Gaussian hierarchical models.

This vignette walks through a minimal workflow that uses the bundled example roads data to build a network, create spatial weights, and construct CAR/ICAR precision matrices suitable for simulation or modeling.

Load the package and example data

library(trafficCAR)
library(Matrix)

data("roads_small", package = "trafficCAR")
roads_sf <- roads_small

Build a road network graph

net <- build_network(
  roads_sf,
  crs_out = 3857,
  node_intersections = TRUE,
  snap_tol = 0,
  simplify = TRUE
)

c(
  nodes = nrow(net$nodes),
  edges = nrow(net$edges),
  adjacency_nnz = Matrix::nnzero(net$A)
)
#>         nodes         edges adjacency_nnz 
#>          1754          2287          4574

Spatial weights from the adjacency matrix

W_row <- weights_from_adjacency(net$A, style = "row-standardized")
Matrix::rowSums(W_row)[1:6]
#> [1] 1 1 1 1 1 1

CAR and ICAR precision matrices

Q_car <- car_precision(net$A, type = "proper", rho = 0.25, tau = 1)

Q_icar <- intrinsic_car_precision(net$A, tau = 1, scale = FALSE)

Q_car[1:6, 1:6]
#> 6 x 6 sparse Matrix of class "dsCMatrix"
#>                             
#> [1,]  1.00 .  .    -0.25 . .
#> [2,]  .    1  .     .    . .
#> [3,]  .    .  1.00 -0.25 . .
#> [4,] -0.25 . -0.25  3.00 . .
#> [5,]  .    .  .     .    2 .
#> [6,]  .    .  .     .    . 1
dim(Q_icar)
#> [1] 1754 1754

Sampling from a CAR precision matrix

set.seed(123)
theta <- trafficCAR:::rmvnorm_prec(Q_car)
c(mean = mean(theta), sd = sd(theta))
#>       mean         sd 
#> 0.01508324 0.70033817

Interactive road visualizations

trafficCAR includes lightweight interactive mapping helpers built on leaflet. They visualize segment-level traffic quantities such as predicted traffic levels or relative congestion on the bundled example road network.

The example below fabricates simple segment-level quantities so the map can be rendered without fitting a full model. In applied workflows, these values are produced automatically by augment_roads().

roads_sf <- roads_small

# mock traffic quantities (stand-in for augment_roads() output)
set.seed(123)

# many mapping helpers expect these standard columns
roads_sf$predicted_mean <- runif(nrow(roads_sf), min = 20, max = 60)
roads_sf$relative_congestion <- as.numeric(scale(runif(nrow(roads_sf))))

has_leaflet <- requireNamespace("leaflet", quietly = TRUE) &&
  requireNamespace("viridisLite", quietly = TRUE)

if (has_leaflet) {
  map_roads_interactive(roads_sf, value = "predicted_speed")
} else {
  message("Install 'leaflet' and 'viridisLite' to view the interactive map.")
}

You can also expose multiple standard traffic layers using map_roads_interactive_layers().

if (has_leaflet) {
  map_roads_interactive_layers(
    roads_sf,
    values = c("predicted_speed", "relative_congestion")
  )
}

Takeaways

Further directions

Planned extensions include: