Learn R Programming

trafficCAR

Overview

trafficCAR is an R package for constructing conditional autoregressive (CAR) precision matrices on graph and road network data. It is built for users seeking principled spatial dependence structures for linear networks (streets, paths, and segmented roadways) and end-to-end utilities for turning raw road geometries into model-ready adjacency/weight matrices.

The package is designed to support methodological, simulation-based, and applied modeling workflows with intrinsic and proper CAR models defined on network structures. It includes helpers for turning LINESTRING/MULTILINESTRING features into stable segments, for building graph objects and sparse matrices, and for scaling or constraining CAR precision matrices to align with common spatial statistics conventions.

Core functionality includes:

  • Building graph representations from spatial road geometries

  • Constructing adjacency and weight matrices

  • Generating ICAR and proper CAR precision matrices

In addition, trafficCAR provides convenient wrappers and plotting utilities aimed at traffic and speed modeling, so you can fit CAR/ICAR models and immediately map latent effects back to a road network.

What the package offers

  • Road and graph preparation: Convert LINESTRING/MULTILINESTRING road data to stable segment IDs with lengths (roads_to_segments()), construct segment adjacency (build_adjacency()), and build a full igraph-based road network (build_network()).

  • Spatial weights and precision matrices: Create binary or row-standardized weight matrices (weights_from_adjacency()), build intrinsic and proper CAR precision matrices (car_precision()), apply Besag scaling (intrinsic_car_precision()), and impose sum-to-zero constraints for ICAR components (icar_sum_to_zero()).

  • Simulation utilities: Sample from proper CAR latent Gaussian models with Gibbs updates (sample_proper_car()) or draw from multivariate normals with sparse precision matrices for custom workflows (rmvnorm_prec()).

  • Model fitting for traffic outcomes: Fit Gaussian CAR/ICAR regression models with optional covariates (fit_car()), or use traffic-oriented wrappers that prepare speed and travel-time outcomes, apply transformations, and return augmented fit objects (fit_traffic()).

  • Augmentation and visualization: Attach fitted latent effects back to road geometries for mapping (augment_fit() and augment_traffic_fit()), create static plots (plot_traffic_static()), and generate interactive leaflet maps (plot_traffic_interactive()).

  • Helper utilities: Simplify road geometries while preserving topology (simplify_roads()), compute connected components for ICAR centering (components_from_adjacency()), and derive degree or row-standardized matrices used across the CAR constructors.

Typical workflow

  1. Prepare road geometries: Clean and optionally simplify input road data. Convert LINESTRING/MULTILINESTRING features into stable, length-aware segments with roads_to_segments().

  2. Build network structure: Use build_adjacency() or build_network() to produce adjacency/graph objects that encode which road segments touch or intersect.

  3. Create weights/precision matrices: Convert adjacency into binary or row-standardized weights (weights_from_adjacency()), then build ICAR or proper CAR precision matrices with car_precision() or intrinsic_car_precision().

  4. Fit models or simulate: Fit Gaussian CAR/ICAR regression models with fit_car() or fit_traffic(), or run simulations with sample_proper_car() and rmvnorm_prec() for benchmarking and model checking.

  5. Augment and visualize: Attach fitted spatial effects back to the road geometries (augment_fit() or augment_traffic_fit()) and visualize results via static or interactive plotting helpers.

Data expectations

  • Road geometries should be LINESTRING or MULTILINESTRING features, typically stored in sf objects.
  • Segment-level outcomes or covariates should align with the segments produced by roads_to_segments() or with the graph indices used to build adjacency/weight matrices.
  • For ICAR models, connected components are handled via components_from_adjacency() and sum-to-zero constraints can be applied with icar_sum_to_zero().

Installation

You can install the released version of trafficCAR from CRAN:

install.packages("trafficCAR")

To install the development version from GitHub:

# install.packages("devtools")
devtools::install_github("mell00/trafficCAR")

Copy Link

Version

Install

install.packages('trafficCAR')

Version

0.1.0

License

MIT + file LICENSE

Maintainer

Madison Ell

Last Published

January 27th, 2026

Functions in trafficCAR (0.1.0)

fit_car

Fit Gaussian CAR / ICAR regression via Gibbs sampling
degree_matrix

Degree matrix from adjacency
fit_traffic

Fit a Gaussian CAR traffic model (speed or travel time)
plot_predicted

Plot predicted traffic outcome on road network
roads_datasets

Example road network datasets
rmvnorm_prec

Draw from a multivariate normal with sparse precision
plot_traffic_map

Quick map helper for augmented roads
roads_to_segments

Convert road geometries to modeling segments
plot_roads_static

Static map of road-segment traffic measures
row_standardize_weights

Row-standardize adjacency matrix
sample_proper_car

Gibbs sampler for a proper CAR latent Gaussian model
plot_relative_congestion

Plot relative congestion on road network
sample_icar

Sample ICAR random effects with component-wise sum-to-zero constraints
simplify_network

Simplify a built network object by removing parallel edges (and loops)
trafficCAR-package

trafficCAR: Bayesian CAR Models for Road-Segment Traffic
residuals.traffic_fit

Residuals for trafficCAR fits
prep_travel_time

Prepare travel time outcome for Gaussian modeling
plot_observed_fitted

Plot observed vs predicted traffic values
plot_mcmc_diagnostics

MCMC diagnostic plots
weights_from_adjacency

Construct spatial weights matrix
ppc_summary

Posterior predictive checks for trafficCAR fits
prep_speed

Prepare speed outcome for Gaussian modeling
update_sigma2_ig

Optional Gibbs update for sigma2 with Inv-Gamma prior
update_beta_gaussian

Gibbs update for beta in y = X beta + x + eps
fetch_osm_roads

Fetch road geometries from OpenStreetMap
.summarize_draws

Summarize draws into mean and equal-tail interval
car_precision

CAR precision matrix from an adjacency matrix
build_adjacency

Build segment adjacency from segment geometries
build_network

Build a road network graph from sf LINESTRING data
augment_roads

Augment roads with predicted traffic quantities
map_roads_interactive

Interactive map of road-segment traffic measures
map_roads_interactive_layers

Interactive map with multiple standard traffic layers
moran_residuals

Moran's I for trafficCAR residuals
intrinsic_car_precision

Intrinsic CAR (ICAR) precision matrix
icar_sum_to_zero

Apply sum-to-zero constraint to ICAR precision
load_roads

Load road geometries from sf object or file
as_sparse_adjacency

Coerce/validate adjacency matrix as sparse with zero diagonal
components_from_adjacency

Connected components and isolates from an adjacency matrix
.extract_gaussian_draws

Extract draws from a base fit object (adapter) EDIT FIELDS LATER