From d585f712375edd95272e278648a2e562881aefbf Mon Sep 17 00:00:00 2001 From: Dorchies David <david.dorchies@inrae.fr> Date: Mon, 21 Jun 2021 12:39:09 +0200 Subject: [PATCH] refactor: move fatal error of multi|compo for RMSE to ErrorCrit_RMSE --- R/ErrorCrit_RMSE.R | 23 +++++++++++++---------- R/UtilsErrorCrit.R | 6 +----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/R/ErrorCrit_RMSE.R b/R/ErrorCrit_RMSE.R index 6cab33bc..ce1b78fe 100644 --- a/R/ErrorCrit_RMSE.R +++ b/R/ErrorCrit_RMSE.R @@ -1,19 +1,22 @@ ErrorCrit_RMSE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = TRUE) { - + ## Arguments check if (!inherits(OutputsModel, "OutputsModel")) { stop("'OutputsModel' must be of class 'OutputsModel'") } - + if (!inherits(InputsCrit, "Single")) { + stop("'ErrorCrit_RMSE' can only be used with 'InputsCrit' of class 'Single'") + } + EC <- .ErrorCrit(InputsCrit = InputsCrit, Crit = "RMSE", OutputsModel = OutputsModel, warnings = warnings) - + CritValue <- NA - + if (EC$CritCompute) { ## ErrorCrit Numer <- sum((EC$VarSim - EC$VarObs)^2, na.rm = TRUE) Denom <- sum(!is.na(EC$VarObs)) - + if (Numer == 0) { Crit <- 0 } else { @@ -22,22 +25,22 @@ ErrorCrit_RMSE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = if (is.numeric(Crit) & is.finite(Crit)) { CritValue <- Crit } - + ## Verbose if (verbose) { message(sprintf("Crit. %s = %.4f", EC$CritName, CritValue)) } } - - + + ## Output OutputsCrit <- list(CritValue = CritValue, CritName = EC$CritName, CritBestValue = EC$CritBestValue, Multiplier = EC$Multiplier, Ind_notcomputed = EC$Ind_TS_ignore) - + class(OutputsCrit) <- c("RMSE", "ErrorCrit") return(OutputsCrit) - + } diff --git a/R/UtilsErrorCrit.R b/R/UtilsErrorCrit.R index 4e200bb2..89b4e25f 100644 --- a/R/UtilsErrorCrit.R +++ b/R/UtilsErrorCrit.R @@ -9,11 +9,7 @@ stop("'InputsCrit' must be of class 'InputsCrit'", call. = FALSE) } if (inherits(InputsCrit, "Multi") | inherits(InputsCrit, "Compo")) { - if (Crit == "RMSE") { - stop("'InputsCrit' must be of class 'Single'. Use the 'ErrorCrit' function on objects of class 'Multi' with RMSE", call. = FALSE) - } else { - stop(paste0("'InputsCrit' must be of class 'Single'. Use the 'ErrorCrit' function on objects of class 'Multi' or 'Compo' with ", Crit), call. = FALSE) - } + stop(paste0("'InputsCrit' must be of class 'Single'. Use the 'ErrorCrit' function on objects of class 'Multi' or 'Compo' with ", Crit), call. = FALSE) } -- GitLab