cv_to_waywiser: Convert 'sdmTMB_cv()' objects to 'sf' format for spatial...

View source: R/cross-val.R

cv_to_waywiserR Documentation

Convert sdmTMB_cv() objects to sf format for spatial assessment with waywiser

Description

[Experimental] Converts cross-validation results to an sf::sf() object for use with spatial model assessment tools such as those in the waywiser package. This enables multi-scale spatial assessment of model predictions.

Usage

cv_to_waywiser(
  object,
  ll_names = c("longitude", "latitude"),
  ll_crs = 4326,
  utm_crs = get_crs(object$data, ll_names)
)

Arguments

object

An object of class sdmTMB_cv from sdmTMB_cv().

ll_names

Column names for longitude and latitude in the original data. Note the order: longitude first, then latitude.

ll_crs

The coordinate reference system (CRS) for the ll_names columns. Defaults to 4326 (WGS84 lon/lat).

utm_crs

The projected coordinate reference system (CRS) for the output sf object. By default (if you're feeling lucky!) automatically detected using get_crs() based on ll_names. Can be manually specified as an EPSG code (e.g., 32609) or any format accepted by sf::st_crs().

Details

This function is particularly useful for assessing spatial models at multiple scales using the waywiser package. After converting to sf format, you can use functions like waywiser::ww_multi_scale() to evaluate how model performance changes when predictions are aggregated to different spatial scales.

For delta/hurdle models, the combined predictions are returned (e.g., the product of the encounter probability and positive catch rate).

Value

An sf::sf() object with POINT geometry containing:

truth

The observed response values

estimate

The cross-validated predictions

geometry

Spatial point locations

See Also

sdmTMB_cv(), get_crs(), https://sdmTMB.github.io/sdmTMB/articles/cross-validation.html

Examples


mesh <- make_mesh(pcod_2011, c("X", "Y"), cutoff = 12)

# Run cross-validation
set.seed(123)
m_cv <- sdmTMB_cv(
  density ~ depth_scaled,
  data = pcod_2011,
  mesh = mesh,
  family = tweedie(),
  k_folds = 2
)

# Convert with default auto-detected CRS based on lon/lat columns:
cv_sf <- cv_to_waywiser(m_cv, ll_names = c("lon", "lat"))

# Or manually specify the desired UTM CRS:
cv_sf <- cv_to_waywiser(m_cv, ll_names = c("lon", "lat"), utm_crs = 32609)

# Use with waywiser for multi-scale assessment
waywiser::ww_multi_scale(
  cv_sf,
  truth,    # column name (unquoted)
  estimate, # column name (unquoted)
  n = list(c(5, 5), c(2, 2)) # 5x5 and 2x2 grid blocks
)


sdmTMB documentation built on Nov. 26, 2025, 9:06 a.m.