Commit 3565d8b5 authored by Delaigue Olivier's avatar Delaigue Olivier
Browse files

v1.2.14.12 CLEAN: simplified code of the ErrorCrit_* funs

parent 4f8c1383
Package: airGR
Type: Package
Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling
Version: 1.2.14.11
Version: 1.2.14.12
Date: 2019-04-17
Authors@R: c(
person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")),
......
......@@ -14,7 +14,7 @@ output:
### 1.2.14.11 Release Notes (2019-04-17)
### 1.2.14.12 Release Notes (2019-04-17)
#### New features
......
......@@ -7,25 +7,18 @@ ErrorCrit_KGE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = T
EC <- .ErrorCrit(InputsCrit = InputsCrit, Crit = "KGE", OutputsModel = OutputsModel, warnings = warnings)
if (!EC$CritCompute) {
CritValue <- NA
SubCritValues <- NA
SubCritNames <- NA
} else {
CritValue <- NA
SubCritValues <- rep(NA, 3)
SubCritNames <- c("r", "alpha", "beta")
SubCritPrint <- rep(NA, 3)
if (EC$CritCompute) {
## Other variables preparation
meanVarObs <- mean(EC$VarObs[!EC$TS_ignore])
meanVarSim <- mean(EC$VarSim[!EC$TS_ignore])
iCrit <- 0
SubCritPrint <- NULL
SubCritNames <- NULL
SubCritValues <- NULL
## SubErrorCrit KGE rPearson
iCrit <- iCrit + 1
SubCritPrint[iCrit] <- paste0(EC$CritName, " cor(sim, obs, \"pearson\") =")
SubCritValues[iCrit] <- NA
SubCritNames[iCrit] <- "r"
SubCritPrint[1L] <- paste0(EC$CritName, " cor(sim, obs, \"pearson\") =")
Numer <- sum((EC$VarObs[!EC$TS_ignore] - meanVarObs) * (EC$VarSim[!EC$TS_ignore] - meanVarSim))
Deno1 <- sqrt(sum((EC$VarObs[!EC$TS_ignore] - meanVarObs) ^ 2))
......@@ -41,14 +34,11 @@ ErrorCrit_KGE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = T
Crit <- Numer / (Deno1 * Deno2)
}
if (is.numeric(Crit) & is.finite(Crit)) {
SubCritValues[iCrit] <- Crit
SubCritValues[1L] <- Crit
}
## SubErrorCrit KGE alpha
iCrit <- iCrit + 1
SubCritPrint[iCrit] <- paste0(EC$CritName, " sd(sim)/sd(obs) =")
SubCritValues[iCrit] <- NA
SubCritNames[iCrit] <- "alpha"
SubCritPrint[2L] <- paste0(EC$CritName, " sd(sim)/sd(obs) =")
Numer <- sd(EC$VarSim[!EC$TS_ignore])
Denom <- sd(EC$VarObs[!EC$TS_ignore])
......@@ -59,14 +49,11 @@ ErrorCrit_KGE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = T
Crit <- Numer / Denom
}
if (is.numeric(Crit) & is.finite(Crit)) {
SubCritValues[iCrit] <- Crit
SubCritValues[2L] <- Crit
}
## SubErrorCrit KGE beta
iCrit <- iCrit + 1
SubCritPrint[iCrit] <- paste0(EC$CritName, " mean(sim)/mean(obs) =")
SubCritValues[iCrit] <- NA
SubCritNames[iCrit] <- "beta"
SubCritPrint[3L] <- paste0(EC$CritName, " mean(sim)/mean(obs) =")
if (meanVarSim == 0 & meanVarObs == 0) {
Crit <- 1
......@@ -74,12 +61,12 @@ ErrorCrit_KGE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = T
Crit <- meanVarSim / meanVarObs
}
if (is.numeric(Crit) & is.finite(Crit)) {
SubCritValues[iCrit] <- Crit
SubCritValues[3L] <- Crit
}
## ErrorCrit
if (sum(is.na(SubCritValues)) == 0) {
CritValue <- (1 - sqrt((SubCritValues[1] - 1)^2 + (SubCritValues[2] - 1)^2 + (SubCritValues[3] - 1)^2))
CritValue <- (1 - sqrt((SubCritValues[1L] - 1)^2 + (SubCritValues[2L] - 1)^2 + (SubCritValues[3L] - 1)^2))
}
## Verbose
......
......@@ -7,23 +7,18 @@ ErrorCrit_KGE2 <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose =
EC <- .ErrorCrit(InputsCrit = InputsCrit, Crit = "KGE2", OutputsModel = OutputsModel, warnings = warnings)
if (!EC$CritCompute) {
CritValue <- NA
} else {
CritValue <- NA
SubCritValues <- rep(NA, 3)
SubCritNames <- c("r", "gamma", "beta")
SubCritPrint <- rep(NA, 3)
if (EC$CritCompute) {
## Other variables preparation
meanVarObs <- mean(EC$VarObs[!EC$TS_ignore])
meanVarSim <- mean(EC$VarSim[!EC$TS_ignore])
iCrit <- 0
SubCritPrint <- NULL
SubCritNames <- NULL
SubCritValues <- NULL
## SubErrorCrit KGE rPearson
iCrit <- iCrit + 1
SubCritPrint[iCrit] <- paste0(EC$CritName, " cor(sim, obs, \"pearson\") =")
SubCritValues[iCrit] <- NA
SubCritNames[iCrit] <- "r"
SubCritPrint[1L] <- paste0(EC$CritName, " cor(sim, obs, \"pearson\") =")
Numer <- sum((EC$VarObs[!EC$TS_ignore] - meanVarObs) * (EC$VarSim[!EC$TS_ignore] - meanVarSim))
Deno1 <- sqrt(sum((EC$VarObs[!EC$TS_ignore] - meanVarObs)^2))
......@@ -39,14 +34,11 @@ ErrorCrit_KGE2 <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose =
Crit <- Numer / (Deno1 * Deno2)
}
if (is.numeric(Crit) & is.finite(Crit)) {
SubCritValues[iCrit] <- Crit
SubCritValues[1L] <- Crit
}
## SubErrorCrit KGE gamma
iCrit <- iCrit + 1
SubCritPrint[iCrit] <- paste0(EC$CritName, " cv(sim)/cv(obs) =")
SubCritValues[iCrit] <- NA
SubCritNames[iCrit] <- "gamma"
SubCritPrint[2L] <- paste0(EC$CritName, " cv(sim)/cv(obs) =")
if (meanVarSim == 0) {
if (sd(EC$VarSim[!EC$TS_ignore]) == 0) {
......@@ -74,14 +66,11 @@ ErrorCrit_KGE2 <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose =
Crit <- CVsim / CVobs
}
if (is.numeric(Crit) & is.finite(Crit)) {
SubCritValues[iCrit] <- Crit
SubCritValues[2L] <- Crit
}
## SubErrorCrit KGE beta
iCrit <- iCrit + 1
SubCritPrint[iCrit] <- paste0(EC$CritName, " mean(sim)/mean(obs) =")
SubCritValues[iCrit] <- NA
SubCritNames[iCrit] <- "beta"
SubCritPrint[3L] <- paste0(EC$CritName, " mean(sim)/mean(obs) =")
if (meanVarSim == 0 & meanVarObs == 0) {
Crit <- 1
......@@ -89,12 +78,12 @@ ErrorCrit_KGE2 <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose =
Crit <- meanVarSim / meanVarObs
}
if (is.numeric(Crit) & is.finite(Crit)) {
SubCritValues[iCrit] <- Crit
SubCritValues[3L] <- Crit
}
## ErrorCrit
if (sum(is.na(SubCritValues)) == 0) {
CritValue <- (1 - sqrt((SubCritValues[1] - 1)^2 + (SubCritValues[2] - 1)^2 + (SubCritValues[3] - 1)^2))
CritValue <- (1 - sqrt((SubCritValues[1L] - 1)^2 + (SubCritValues[2L] - 1)^2 + (SubCritValues[3L] - 1)^2))
}
## Verbose
......
......@@ -7,9 +7,9 @@ ErrorCrit_NSE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = T
EC <- .ErrorCrit(InputsCrit = InputsCrit, Crit = "NSE", OutputsModel = OutputsModel, warnings = warnings)
if (!EC$CritCompute) {
CritValue <- NA
} else {
CritValue <- NA
if (EC$CritCompute) {
## Other variables preparation
meanVarObs <- mean(EC$VarObs[!EC$TS_ignore])
meanVarSim <- mean(EC$VarSim[!EC$TS_ignore])
......
......@@ -7,9 +7,9 @@ ErrorCrit_RMSE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose =
EC <- .ErrorCrit(InputsCrit = InputsCrit, Crit = "RMSE", OutputsModel = OutputsModel, warnings = warnings)
if (!EC$CritCompute) {
CritValue <- NA
} else {
CritValue <- NA
if (EC$CritCompute) {
## ErrorCrit
Numer <- sum((EC$VarSim - EC$VarObs)^2, na.rm = TRUE)
Denom <- sum(!is.na(EC$VarObs))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment