Skip to contents

Builds a machine-learning emulator that approximates the routing model's depth response to its parameters, so that many what-if scenarios can be explored far faster than re-running the full pipeline. Training data are generated by evaluating the Manning depth relation across sampled parameters; the surrogate then predicts depth for new parameter sets.

Usage

flood_surrogate(
  x,
  n_train = 500,
  n_range = c(0.02, 0.08),
  width_range = c(10, 40),
  seed = NULL
)

Arguments

x

A flood_project whose route slot supplies the fixed slope and discharge context, or a flood_route object directly.

n_train

Number of training samples. Default 500.

n_range, width_range

Length-2 priors for Manning's \(n\) and channel width used to generate training data.

seed

Optional integer seed for reproducibility.

Value

If x is a flood_project, the same object with its surrogate entry stored in meta (the project schema has no dedicated slot) and the stage logged. Otherwise a list of class flood_surrogate with elements engine ("ranger" or "loglinear"), predict (a function taking a data frame with columns Q, n, width and returning predicted depth), performance (in-sample rmse and r2), and settings.

Details

When ranger is installed, a random forest is used; otherwise a log-linear model provides a dependency-free fallback (exact for the power-law Manning relation, and a reasonable approximation more generally). The surrogate is a convenience for rapid exploration and interpolation, not a replacement for the physical model.

See also

flood_route for the model being emulated.

Examples

disc <- data.frame(
  date = seq(as.Date("2020-06-01"), by = "day", length.out = 12),
  Q_mm = c(0, 1, 3, 8, 18, 30, 22, 14, 8, 4, 2, 1)
)
r <- flood_route(disc, area_km2 = 300)
s <- flood_surrogate(r, n_train = 300, seed = 1)
s$performance
#>    rmse      r2 
#> 0.15363 0.98469 
# Predict depth for new parameter sets, fast
s$predict(data.frame(Q = 150, n = 0.04, width = 25))
#> [1] 3.180562