## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>", fig.bg = "white",
  dev.args = list(bg = "white"), fig.width = 8, fig.height = 6,
  dpi = 144, fig.align = "center"
)

## ----models-------------------------------------------------------------------
library(potentiomap)
data("synthetic_wells")
points <- ps_make_points(
  synthetic_wells, "x", "y", "gw_elevation", "well_id", "EPSG:26916"
)

result <- suppressWarnings(ps_interpolate(
  points, methods = c("IDW", "TPS", "OK", "UK"), grid_res = 400,
  return = "result", support = TRUE, support_max_distance = 1000
))
summary(result)
result$conditions

## ----selected-diagnostics-----------------------------------------------------
ps_diagnostics(result, "TPS")[c(
  "selection_mode", "selected_lambda", "effective_degrees_of_freedom",
  "selected_at_search_boundary", "return_status"
)]

ps_diagnostics(result, "UK")[c(
  "coordinate_scaling", "model_matrix_rank", "condition_number",
  "observed_range", "predicted_range",
  "predicted_range_to_observed_range_ratio", "return_status"
)]

## ----support------------------------------------------------------------------
knitr::kable(result$support$summary)
head(result$support$records)

## ----support-distance-map, fig.alt="Nearest-observation-distance raster for a synthetic monitoring network, with the training convex hull outlined and observation wells shown as points."----
distance <- result$support$rasters[["nearest_observation_distance"]]

par(bg = "white")
terra::plot(
  distance, col = hcl.colors(64, "YlGnBu"),
  main = "Local observation support: nearest-well distance"
)
terra::plot(
  terra::convHull(points), add = TRUE, border = "#9b3d2f",
  lwd = 2, lty = 2
)
terra::plot(points, add = TRUE, pch = 21, bg = "white", cex = 0.75)
legend(
  "topleft", legend = "Training convex hull", lty = 2, lwd = 2,
  col = "#9b3d2f", bty = "n"
)

## ----contour-support----------------------------------------------------------
contours <- ps_contours(result$surfaces$IDW, interval = 1)
classified <- suppressWarnings(ps_contour_support(
  contours, support = result$support,
  supported_distance = 500, approximate_distance = 1200
))
knitr::kable(classified$summary)

