From 6c86591a153d293458ad9a6cadb43a2f71dbb3ce Mon Sep 17 00:00:00 2001 From: Dan McGraw Date: Wed, 4 Nov 2026 12:25:14 -0700 Subject: [PATCH 1/6] Update plot theme and rejection sampling documentation --- R/plotting_utils.R | 86 +++++++++++++++++++++++++++++++++++++++--- R/rejection_sampling.R | 2 +- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/R/plotting_utils.R b/R/plotting_utils.R index bcf610ad..f42a398c 100644 --- a/R/plotting_utils.R +++ b/R/plotting_utils.R @@ -21,11 +21,85 @@ #' subtitle = "Demonstrating theme_rfar_conceptual()", #' x = "AEP", y = "Stage (ft)") + #' theme_rfar_conceptual() -theme_rfar_conceptual <- function(){ +theme_rfar_conceptual <- function() { ggplot2::theme_bw() + - ggplot2::theme(legend.position = "none", - plot.title = ggplot2::element_text(size = 11, face = "bold.italic"), - plot.subtitle = ggplot2::element_text(size = 8, face = "italic"), - axis.title = ggplot2::element_text(size = 9, face = "bold"), - axis.text = ggplot2::element_text(size = 8)) + ggplot2::theme( + legend.position = "none", + plot.title = ggplot2::element_text(size = 11, face = "bold.italic"), + plot.subtitle = ggplot2::element_text(size = 8, face = "italic"), + axis.title = ggplot2::element_text(size = 9, face = "bold"), + axis.text = ggplot2::element_text(size = 8) + ) +} + +#' AEP-Labelled Normal-Variate X Scale +#' +#' Creates a continuous \pkg{ggplot2} x-axis positioned on the standard-normal +#' variate scale while displaying labels as annual exceedance probabilities +#' (AEPs). This is useful for stage-frequency, flow-frequency, and similar +#' conceptual plots where values are modeled or transformed in normal-variate +#' space but should be interpreted as probabilities. +#' +#' Major breaks are specified as AEP values and transformed using +#' \code{qnorm(1 - AEP)}. Minor breaks are generated from the supplied +#' multipliers across AEP decades from \eqn{10^{-1}} through \eqn{10^{-10}}. +#' +#' @param aep_breaks Numeric vector of annual exceedance probabilities to use +#' as major axis breaks. Values must be greater than 0 and less than 1. +#' Defaults range from 0.99 through \eqn{10^{-10}}. +#' @param minor_aep Numeric vector of multipliers used to generate minor AEP +#' breaks within each decade. The default, \code{2:9}, produces minor breaks +#' such as 0.2 through 0.9, 0.02 through 0.09, and so on. +#' +#' @return A \pkg{ggplot2} continuous x-scale object that can be added to a +#' \code{ggplot} via \code{+}. +#' +#' @export +#' +#' @examples +#' library(ggplot2) +#' +#' example_data <- data.frame( +#' AEP = c(0.99, 0.5, 0.1, 0.01, 0.001), +#' Stage = c(10.2, 11.5, 14.0, 17.3, 20.1) +#' ) +#' +#' ggplot(example_data, aes(x = qnorm(1 - AEP), y = Stage)) + +#' geom_line() + +#' scale_x_aep() + +#' labs( +#' title = "Example Stage-Frequency Curve", +#' x = "Annual Exceedance Probability", +#' y = "Stage (ft)" +#' ) +scale_x_aep <- function( + aep_breaks = c( + 9.9e-1, + 9e-1, + 5e-1, + 1e-1, + 1e-2, + 1e-3, + 1e-4, + 1e-5, + 1e-6, + 1e-7, + 1e-8, + 1e-9, + 1e-10 + ), + minor_aep = 2:9 +) { + minor_aep_breaks <- unlist( + lapply(minor_aep, function(i) i * 10^-(1:10)) + ) + + z_breaks <- stats::qnorm(1 - aep_breaks) + z_breaks_minor <- stats::qnorm(1 - minor_aep_breaks) + + ggplot2::scale_x_continuous( + breaks = z_breaks, + minor_breaks = z_breaks_minor, + labels = aep_breaks + ) } diff --git a/R/rejection_sampling.R b/R/rejection_sampling.R index c53103e7..0a6138b0 100644 --- a/R/rejection_sampling.R +++ b/R/rejection_sampling.R @@ -160,7 +160,7 @@ pmf_stage_lognormal <- function( #' #' @examples #' -#' pmf_ln <- pmf_stage_lognormal(pmf_shift = 239, pmf_mean = 241.9, pmf_sigma = 0.5) +#' pmf_ln <- pmf_stage_lognormal(pmf_shift = 1651, pmf_mean = 1663, pmf_sigma = 0.5) #' result <- rejection_sampling_stage( #' pmf_stage_LN = pmf_ln, #' stage_freq_df = ex_rs_stage_loading, From af46117f7e05da755ac73e73b1a3a45995956b5b Mon Sep 17 00:00:00 2001 From: Dan McGraw Date: Wed, 4 Nov 2026 13:18:11 -0700 Subject: [PATCH 2/6] devtools:doc --- NAMESPACE | 1 + man/rejection_sampling_stage.Rd | 2 +- man/scale_x_aep.Rd | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 man/scale_x_aep.Rd diff --git a/NAMESPACE b/NAMESPACE index 2267f619..011f5f33 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(qp3) export(rejection_sampling_stage) export(rfa_simulate) export(scale_hydrograph) +export(scale_x_aep) export(stage2aep) export(stratified_sampler) export(theme_rfar_conceptual) diff --git a/man/rejection_sampling_stage.Rd b/man/rejection_sampling_stage.Rd index 86599601..3768841e 100644 --- a/man/rejection_sampling_stage.Rd +++ b/man/rejection_sampling_stage.Rd @@ -40,7 +40,7 @@ assigned Weibull plotting positions for use in frequency analysis. } \examples{ -pmf_ln <- pmf_stage_lognormal(pmf_shift = 239, pmf_mean = 241.9, pmf_sigma = 0.5) +pmf_ln <- pmf_stage_lognormal(pmf_shift = 1651, pmf_mean = 1663, pmf_sigma = 0.5) result <- rejection_sampling_stage( pmf_stage_LN = pmf_ln, stage_freq_df = ex_rs_stage_loading, diff --git a/man/scale_x_aep.Rd b/man/scale_x_aep.Rd new file mode 100644 index 00000000..c14fa976 --- /dev/null +++ b/man/scale_x_aep.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotting_utils.R +\name{scale_x_aep} +\alias{scale_x_aep} +\title{AEP-Labelled Normal-Variate X Scale} +\usage{ +scale_x_aep( + aep_breaks = c(0.99, 0.9, 0.5, 0.1, 0.01, 0.001, 1e-04, 1e-05, 1e-06, 1e-07, 1e-08, + 1e-09, 1e-10), + minor_aep = 2:9 +) +} +\arguments{ +\item{aep_breaks}{Numeric vector of annual exceedance probabilities to use +as major axis breaks. Values must be greater than 0 and less than 1. +Defaults range from 0.99 through \eqn{10^{-10}}.} + +\item{minor_aep}{Numeric vector of multipliers used to generate minor AEP +breaks within each decade. The default, \code{2:9}, produces minor breaks +such as 0.2 through 0.9, 0.02 through 0.09, and so on.} +} +\value{ +A \pkg{ggplot2} continuous x-scale object that can be added to a +\code{ggplot} via \code{+}. +} +\description{ +Creates a continuous \pkg{ggplot2} x-axis positioned on the standard-normal +variate scale while displaying labels as annual exceedance probabilities +(AEPs). This is useful for stage-frequency, flow-frequency, and similar +conceptual plots where values are modeled or transformed in normal-variate +space but should be interpreted as probabilities. +} +\details{ +Major breaks are specified as AEP values and transformed using +\code{qnorm(1 - AEP)}. Minor breaks are generated from the supplied +multipliers across AEP decades from \eqn{10^{-1}} through \eqn{10^{-10}}. +} +\examples{ +library(ggplot2) + +example_data <- data.frame( + AEP = c(0.99, 0.5, 0.1, 0.01, 0.001), + Stage = c(10.2, 11.5, 14.0, 17.3, 20.1) +) + +ggplot(example_data, aes(x = qnorm(1 - AEP), y = Stage)) + + geom_line() + + scale_x_aep() + + labs( + title = "Example Stage-Frequency Curve", + x = "Annual Exceedance Probability", + y = "Stage (ft)" + ) +} From 632ab0ce31c0b5bdfba0cc86906b6b6c1a1fd37b Mon Sep 17 00:00:00 2001 From: Dan McGraw Date: Wed, 4 Nov 2026 14:33:26 -0700 Subject: [PATCH 3/6] updated yaml --- _pkgdown.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 9f46270a..20dd302c 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -46,6 +46,7 @@ reference: - interpolate_stage_matrix - power_function - theme_rfar_conceptual + - scale_x_aep - pmf_stage_lognormal - rejection_sampling_stage - title: "Datasets" From b055c0aa64a79444398f7f51eec0a1623ded2f75 Mon Sep 17 00:00:00 2001 From: Dan McGraw Date: Tue, 10 Nov 2026 14:16:29 -0700 Subject: [PATCH 4/6] function to create unregulated to regulated transform function --- R/unreg_reg_transform.R | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 R/unreg_reg_transform.R diff --git a/R/unreg_reg_transform.R b/R/unreg_reg_transform.R new file mode 100644 index 00000000..433832fb --- /dev/null +++ b/R/unreg_reg_transform.R @@ -0,0 +1,69 @@ +#' Create an Unregulated-to-Regulated Inflow Transformation Function +#' +#' Constructs a function that transforms unregulated inflow values to corresponding +#' regulated inflow values using linear interpolation. By default, input values +#' outside the range of the supplied unregulated inflows are returned unchanged. +#' +#' @param unreg_inflow Numeric vector of unregulated inflow values. Values should +#' represent the independent variable for the interpolation. +#' @param reg_inflow Numeric vector of regulated inflow values corresponding +#' element-wise to `unreg_inflow`. +#' @param extend Logical; if `TRUE` (default), values passed to the returned +#' function that fall outside the observed range of `unreg_inflow` are returned +#' unchanged. If `FALSE`, out-of-range values return `NA`. +#' +#' @return A function accepting a numeric vector `x` and returning a numeric vector +#' of interpolated regulated inflow values. +#' +#' @details +#' The transformation uses [stats::approxfun()] with `rule = 1`, which produces +#' `NA` for values outside the interpolation range. When `extend = TRUE`, those +#' out-of-range values are replaced with their original input values rather than +#' extrapolated. +#' +#' Missing values in `x` are preserved as `NA`. The minimum and maximum +#' interpolation bounds are calculated with missing values removed from +#' `unreg_inflow`. +#' +#' @examples +#' unreg_inflow <- c(0, 100, 200, 300) +#' reg_inflow <- c(0, 90, 170, 240) +#' +#' transform_inflow <- unreg_reg_transform(unreg_inflow, reg_inflow) +#' +#' # Interpolated value +#' transform_inflow(150) +#' +#' # Out-of-range values are unchanged when extend = TRUE +#' transform_inflow(c(-10, 50, 350)) +#' +#' # Out-of-range values return NA when extend = FALSE +#' strict_transform <- unreg_reg_transform( +#' unreg_inflow, +#' reg_inflow, +#' extend = FALSE +#' ) +#' strict_transform(c(-10, 50, 350)) +#' +#' @export +unreg_reg_transform <- function( + unreg_inflow, + reg_inflow, + extend = TRUE +) { + f <- approxfun(unreg_inflow, reg_inflow, rule = 1) + + xmin <- min(unreg_inflow, na.rm = TRUE) + xmax <- max(unreg_inflow, na.rm = TRUE) + + function(x) { + result <- f(x) + + if (extend) { + outside_range <- !is.na(x) & (x < xmin | x > xmax) + result[outside_range] <- x[outside_range] + } + + result + } +} From 4350e0854ea149646efc517441c1fd8322b42c4b Mon Sep 17 00:00:00 2001 From: Dan McGraw Date: Tue, 10 Nov 2026 15:37:56 -0700 Subject: [PATCH 5/6] added unreg-reg transform input to rfa_simulate --- R/rfa_simulate.R | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/R/rfa_simulate.R b/R/rfa_simulate.R index a22f0a56..c7db267a 100644 --- a/R/rfa_simulate.R +++ b/R/rfa_simulate.R @@ -57,6 +57,10 @@ #' sampling probabilities. #' @param resmodel Data frame with three columns: elevation (ft), storage (acre-ft), #' and discharge (cfs). Defines the reservoir model for Modified Puls routing. +#' @param unreg_reg_transform Optional function that transforms sampled +#' unregulated inflow volume to corresponding regulated inflow volumes before +#' hydrograph scaling. Typically created with [unreg_reg_transform()]. +#' If `NULL` (default), sampled volumes are used without transformation. #' @param Nbins Integer. Number of stratified sampling bins in EV1 space. #' Default is 50. #' @param events_per_bin Integer. Number of events sampled per bin. Default is 200. @@ -194,6 +198,7 @@ rfa_simulate <- function( seasonality, hydrographs, resmodel, + unreg_reg_transform = NULL, Nbins = 50, events_per_bin = 200, routing_dt = 1, @@ -236,6 +241,14 @@ rfa_simulate <- function( # Remove rows with missing stages in stage timeseries stage_ts <- stage_ts[!is.na(stage_ts$stage), ] + # If Unregulated-Regulated Transform Function is supplied, then transform volume samples + regulated_transform <- !is.null(unreg_reg_transform) + if (regulated_transform) { + cli::cli_alert_info( + "Applying unregulated-to-regulated transformation to sampled inflow volumes." + ) + } + # ============================================================================ # MEDIAN ONLY ==== # ============================================================================ @@ -348,11 +361,17 @@ rfa_simulate <- function( hydrograph_shape <- hydrographs[[hydroSamps[realiz]]][, 2:3] # Hydrograph observed volume obs_hydrograph_vol <- attr(hydrographs[[hydroSamps[realiz]]], "obs_vol") + # Select the target sample volume for hydrograph scaling + sample_volume <- if (regulated_transform) { + unreg_reg_transform(Q_matrix[i, j]) + } else { + Q_matrix[i, j] + } # Scale hydrograph to sample volume scaled_hydrograph <- scale_hydrograph( hydrograph_shape, obs_hydrograph_vol, - Q_matrix[i, j], + sample_volume, routing_dt = routing_dt ) # Route scaled hydrograph @@ -371,7 +390,7 @@ rfa_simulate <- function( realiz_numeric[realiz, ] <- c( InitMonths[realiz], InitStages[realiz], - Q_matrix[i, j], + sample_volume, tmpResults[1], tmpResults[2] ) @@ -512,11 +531,17 @@ rfa_simulate <- function( hydrograph_shape <- hydrographs[[hydroSamps[realiz]]][, 2:3] # Hydrograph observed volume obs_hydrograph_vol <- attr(hydrographs[[hydroSamps[realiz]]], "obs_vol") + # Select the target sample volume for hydrograph scaling + sample_volume <- if (regulated_transform) { + unreg_reg_transform(Q_matrix[i, j]) + } else { + Q_matrix[i, j] + } # Scale hydrograph to sample volume scaled_hydrograph <- scale_hydrograph( hydrograph_shape, obs_hydrograph_vol, - Q_matrix[i, j], + sample_volume, routing_dt = routing_dt ) # Route scaled hydrograph @@ -535,7 +560,7 @@ rfa_simulate <- function( realiz_numeric[realiz, ] <- c( InitMonths[realiz], InitStages[realiz], - Q_matrix[i, j], + sample_volume, tmpResults[1], tmpResults[2] ) @@ -721,11 +746,17 @@ rfa_simulate <- function( hydrographs[[hydroSamps[realiz]]], "obs_vol" ) + # Set Target for unreg or reg + sample_volume <- if (regulated_transform) { + unreg_reg_transform(Q_matrix[i, j]) + } else { + Q_matrix[i, j] + } scaled_hydrograph <- scale_hydrograph( hydrograph_shape, obs_hydrograph_vol, - Q_matrix[i, j], + sample_volume, routing_dt = routing_dt ) From 8b201599d5271923c6d9e1deec5d26eb8b6b0339 Mon Sep 17 00:00:00 2001 From: Dan McGraw Date: Tue, 10 Nov 2026 16:31:47 -0700 Subject: [PATCH 6/6] tests, document, and check for v0.9 --- DESCRIPTION | 32 ++++++------ NAMESPACE | 1 + NEWS.md | 14 ++++++ README.md | 4 ++ _pkgdown.yml | 1 + man/rfa_simulate.Rd | 6 +++ man/unreg_reg_transform.Rd | 59 +++++++++++++++++++++++ tests/testthat/test-unreg-reg-transform.R | 26 ++++++++++ 8 files changed, 127 insertions(+), 16 deletions(-) create mode 100644 man/unreg_reg_transform.Rd create mode 100644 tests/testthat/test-unreg-reg-transform.R diff --git a/DESCRIPTION b/DESCRIPTION index 702e3ead..af1a0707 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rfaR Title: RMC Reservoir Frequency Analysis in R -Version: 0.5.1 +Version: 0.9.0 Authors@R: c( person("Daniel", "McGraw", , "daniel.e.mcgraw@usace.army.mil", role = c("aut", "cre"), @@ -26,29 +26,29 @@ Authors@R: c( role = "rev", comment = "P.E., USACE RMC"), person("Bryan", "Robinson", , "bryan.j.robinson@usace.army.mil", role = "rev", comment = "P.E., USACE RMC")) -Description: An R implementation of the U.S. Army Corps of Engineers (USACE) - Risk Management Center-Reservoir Frequency Analysis (RMC-RFA) methodology/software. - This package produces reservoir stage-frequency curves with uncertainty bounds - by combining deterministic flood routing (Modified Puls method) with a nested - Monte Carlo framework. The outer loop quantifies knowledge uncertainty in inflow - volume frequency distributions, while the inner loop simulates natural variability - across thousands of flood events. Uncertain variables include inflow volume, - flood hydrograph shape, seasonal occurrence, and antecedent reservoir stage. - The resulting stage-frequency curves provide critical hydrologic hazard - assessment data for dam safety risk analyses, enabling evaluation of loading - parameters (stage, discharge, duration) and their associated annual exceedance +Description: An R implementation of the U.S. Army Corps of Engineers (USACE) + Risk Management Center-Reservoir Frequency Analysis (RMC-RFA) methodology/software. + This package produces reservoir stage-frequency curves with uncertainty bounds + by combining deterministic flood routing (Modified Puls method) with a nested + Monte Carlo framework. The outer loop quantifies knowledge uncertainty in inflow + volume frequency distributions, while the inner loop simulates natural variability + across thousands of flood events. Uncertain variables include inflow volume, + flood hydrograph shape, seasonal occurrence, and antecedent reservoir stage. + The resulting stage-frequency curves provide critical hydrologic hazard + assessment data for dam safety risk analyses, enabling evaluation of loading + parameters (stage, discharge, duration) and their associated annual exceedance probabilities. -Nickname: Friday +Nickname: Friday After Next License: file LICENSE URL: https://github.com/USACE-RMC/rfaR, https://usace-rmc.github.io/rfaR/ BugReports: https://github.com/USACE-RMC/rfaR/issues Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.3 -Depends: +Depends: R (>= 4.1) LazyData: true -Suggests: +Suggests: gt, knitr, rmarkdown, @@ -60,7 +60,7 @@ Suggests: patchwork, ggsci VignetteBuilder: knitr -Imports: +Imports: cli, future, future.apply, diff --git a/NAMESPACE b/NAMESPACE index 011f5f33..388ba3dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ export(scale_x_aep) export(stage2aep) export(stratified_sampler) export(theme_rfar_conceptual) +export(unreg_reg_transform) importFrom(stats,approx) importFrom(stats,approxfun) importFrom(stats,pnorm) diff --git a/NEWS.md b/NEWS.md index 92a2a31f..056a8880 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,17 @@ +# rfaR 0.9.0 + +Documentation will statisfy quality assurance for v1.0. No major functional changes expected between 0.9.0 and 1.0. + +I want to personally thank any users for their on-going patience for documetation. We're close. -Dan + +## New features + +* Added `unreg_reg_transform()` to create an unregulated-to-regulated + inflow transformation function using linear interpolation. + +* `rfa_simulate()` now accepts an optional `unreg_reg_transform` function + to transform sampled volumes before hydrograph scaling. + # rfaR 0.5.1 ## Summary diff --git a/README.md b/README.md index bd073a6d..806064a3 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ An R implementation of the U.S. Army Corps of Engineers (USACE) Risk Management See [`NEWS.md`](NEWS.md) for a history of user-facing changes across versions. +## Acknowledgments + +The authors thank Allen Avance, Samantha Hartke, Reuben Sasaki, and Bryan Robinson of the U.S. Army Corps of Engineers Risk Management Center (USACE - RMC) for their technical review, feedback, and contributions to package development. + ## Installation ### For users diff --git a/_pkgdown.yml b/_pkgdown.yml index 20dd302c..9f4044c9 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -49,6 +49,7 @@ reference: - scale_x_aep - pmf_stage_lognormal - rejection_sampling_stage + - unreg_reg_transform - title: "Datasets" desc: "Example datasets included with rfaR" contents: diff --git a/man/rfa_simulate.Rd b/man/rfa_simulate.Rd index cd479527..5ed8bb71 100644 --- a/man/rfa_simulate.Rd +++ b/man/rfa_simulate.Rd @@ -12,6 +12,7 @@ rfa_simulate( seasonality, hydrographs, resmodel, + unreg_reg_transform = NULL, Nbins = 50, events_per_bin = 200, routing_dt = 1, @@ -57,6 +58,11 @@ sampling probabilities.} \item{resmodel}{Data frame with three columns: elevation (ft), storage (acre-ft), and discharge (cfs). Defines the reservoir model for Modified Puls routing.} +\item{unreg_reg_transform}{Optional function that transforms sampled +unregulated inflow volume to corresponding regulated inflow volumes before +hydrograph scaling. Typically created with \code{\link[=unreg_reg_transform]{unreg_reg_transform()}}. +If \code{NULL} (default), sampled volumes are used without transformation.} + \item{Nbins}{Integer. Number of stratified sampling bins in EV1 space. Default is 50.} diff --git a/man/unreg_reg_transform.Rd b/man/unreg_reg_transform.Rd new file mode 100644 index 00000000..0c9dcf9f --- /dev/null +++ b/man/unreg_reg_transform.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/unreg_reg_transform.R +\name{unreg_reg_transform} +\alias{unreg_reg_transform} +\title{Create an Unregulated-to-Regulated Inflow Transformation Function} +\usage{ +unreg_reg_transform(unreg_inflow, reg_inflow, extend = TRUE) +} +\arguments{ +\item{unreg_inflow}{Numeric vector of unregulated inflow values. Values should +represent the independent variable for the interpolation.} + +\item{reg_inflow}{Numeric vector of regulated inflow values corresponding +element-wise to \code{unreg_inflow}.} + +\item{extend}{Logical; if \code{TRUE} (default), values passed to the returned +function that fall outside the observed range of \code{unreg_inflow} are returned +unchanged. If \code{FALSE}, out-of-range values return \code{NA}.} +} +\value{ +A function accepting a numeric vector \code{x} and returning a numeric vector +of interpolated regulated inflow values. +} +\description{ +Constructs a function that transforms unregulated inflow values to corresponding +regulated inflow values using linear interpolation. By default, input values +outside the range of the supplied unregulated inflows are returned unchanged. +} +\details{ +The transformation uses \code{\link[stats:approxfun]{stats::approxfun()}} with \code{rule = 1}, which produces +\code{NA} for values outside the interpolation range. When \code{extend = TRUE}, those +out-of-range values are replaced with their original input values rather than +extrapolated. + +Missing values in \code{x} are preserved as \code{NA}. The minimum and maximum +interpolation bounds are calculated with missing values removed from +\code{unreg_inflow}. +} +\examples{ +unreg_inflow <- c(0, 100, 200, 300) +reg_inflow <- c(0, 90, 170, 240) + +transform_inflow <- unreg_reg_transform(unreg_inflow, reg_inflow) + +# Interpolated value +transform_inflow(150) + +# Out-of-range values are unchanged when extend = TRUE +transform_inflow(c(-10, 50, 350)) + +# Out-of-range values return NA when extend = FALSE +strict_transform <- unreg_reg_transform( + unreg_inflow, + reg_inflow, + extend = FALSE +) +strict_transform(c(-10, 50, 350)) + +} diff --git a/tests/testthat/test-unreg-reg-transform.R b/tests/testthat/test-unreg-reg-transform.R new file mode 100644 index 00000000..16852c2d --- /dev/null +++ b/tests/testthat/test-unreg-reg-transform.R @@ -0,0 +1,26 @@ +test_that("unreg_reg_transform interpolates and handles out-of-range values", { + transform <- unreg_reg_transform( + unreg_inflow = c(0, 100, 200), + reg_inflow = c(0, 80, 150) + ) + + # Interpolates within the provided inflow range + expect_equal(transform(50), 40) + expect_equal(transform(150), 115) + + # Returns original values outside the range when extend = TRUE + expect_equal(transform(-10), -10) + expect_equal(transform(250), 250) +}) + +test_that("unreg_reg_transform returns NA outside range when extend is FALSE", { + transform <- unreg_reg_transform( + unreg_inflow = c(0, 100, 200), + reg_inflow = c(0, 80, 150), + extend = FALSE + ) + + expect_equal(transform(50), 40) + expect_true(is.na(transform(-10))) + expect_true(is.na(transform(250))) +})