ErrorCrit_NSE.R 1.36 KB
Newer Older
1
ErrorCrit_NSE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = TRUE) {
Dorchies David's avatar
Dorchies David committed
2

3
  ## Arguments check
4
  if (!inherits(OutputsModel, "OutputsModel")) {
5
    stop("'OutputsModel' must be of class 'OutputsModel'")
6
  }
Dorchies David's avatar
Dorchies David committed
7

8
  EC <- .ErrorCrit(InputsCrit = InputsCrit, Crit = "NSE",  OutputsModel = OutputsModel, warnings = warnings)
Dorchies David's avatar
Dorchies David committed
9

10
  CritValue <- NA
Dorchies David's avatar
Dorchies David committed
11

12
  if (EC$CritCompute) {
13
    ## Other variables preparation
14
15
    meanVarObs <- mean(EC$VarObs[!EC$TS_ignore])
    meanVarSim <- mean(EC$VarSim[!EC$TS_ignore])
Dorchies David's avatar
Dorchies David committed
16

17
    ## ErrorCrit
18
19
    Emod <- sum((EC$VarSim[!EC$TS_ignore] - EC$VarObs[!EC$TS_ignore])^2)
    Eref <- sum((EC$VarObs[!EC$TS_ignore] - mean(EC$VarObs[!EC$TS_ignore]))^2)
Dorchies David's avatar
Dorchies David committed
20

21
22
23
24
25
26
27
    if (Emod == 0 & Eref == 0) {
      Crit <- 0
    } else {
      Crit <- (1 - Emod / Eref)
    }
    if (is.numeric(Crit) & is.finite(Crit)) {
      CritValue <- Crit
28
    }
Dorchies David's avatar
Dorchies David committed
29

30
31
    ## Verbose
    if (verbose) {
32
      message(sprintf("Crit. %s = %.4f", EC$CritName, CritValue))
33
    }
34
  }
Dorchies David's avatar
Dorchies David committed
35
36


37
  ## Output
38
  OutputsCrit <- list(CritValue       = CritValue,
39
40
41
                      CritName        = EC$CritName,
                      CritBestValue   = EC$CritBestValue,
                      Multiplier      = EC$Multiplier,
Dorchies David's avatar
Dorchies David committed
42
43
                      Ind_notcomputed = EC$Ind_TS_ignore)

44
45
  class(OutputsCrit) <- c("NSE", "ErrorCrit")
  return(OutputsCrit)
Dorchies David's avatar
Dorchies David committed
46

Delaigue Olivier's avatar
Delaigue Olivier committed
47
}
Dorchies David's avatar
Dorchies David committed
48
49

class(ErrorCrit_NSE) <- c("FUN_CRIT", class(ErrorCrit_NSE))