ErrorCrit_RMSE.R 4.51 KB
Newer Older
1
2
3
4
ErrorCrit_RMSE <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = TRUE) {
    
    
    ##Arguments_check________________________________
5
    if (!inherits(InputsCrit, "InputsCrit")) {
6
7
8
      stop("InputsCrit must be of class 'InputsCrit' \n")
      return(NULL)
    }
9
10
11
12
    if (inherits(InputsCrit, "Multi") | inherits(InputsCrit, "Compo")) {
      stop("InputsCrit must be of class 'Single'. Use the ErrorCrit function on objects of class 'Multi' with RMSE")
      return(NULL)
    }
13
    if (!inherits(OutputsModel, "OutputsModel")) {
14
15
16
17
18
19
20
      stop("OutputsModel must be of class 'OutputsModel' \n")
      return(NULL)
    }
    
    
    ##Initialisation_________________________________
    CritName <- NA
21
    CritVar  <- InputsCrit$varObs
22
    if (InputsCrit$transfo == "") {
23
      CritName <- "RMSE[CritVar]"
24
25
    }
    if (InputsCrit$transfo == "sqrt") {
26
      CritName <- "RMSE[sqrt(CritVar)]"
27
28
    }
    if (InputsCrit$transfo == "log") {
29
      CritName <- "RMSE[log(CritVar)]"
30
31
    }
    if (InputsCrit$transfo == "inv") {
32
      CritName <- "RMSE[1/CritVar]"
33
34
    }
    if (InputsCrit$transfo == "sort") {
35
      CritName <- "RMSE[sort(CritVar)]"
36
    }
37
38
39
40
    CritName      <- gsub(pattern = "CritVar", replacement = CritVar, x = CritName)
    CritValue     <- NA
    CritBestValue <- +1
    Multiplier    <- +1
41
42
43
44
    ### must be equal to -1 or +1 only
    
    
    ##Data_preparation_______________________________
45
    VarObs <- InputsCrit$obs
46
47
48
49
50
    VarObs[!InputsCrit$BoolCrit] <- NA
    VarSim <- OutputsModel$Qsim
    VarSim[!InputsCrit$BoolCrit] <- NA
    
    ##Data_transformation
51
    if (InputsCrit$transfo %in% c("log", "inv") & is.null(InputsCrit$epsilon) & warnings) {
52
      if (any(VarObs %in% 0)) {
53
        warning("zeroes detected in Qobs: the corresponding time-steps will be excluded from the criteria computation if the epsilon argument of 'CreateInputsCrit' = NULL")
54
55
      }
      if (any(VarSim %in% 0)) {
56
        warning("zeroes detected in Qsim: the corresponding time-steps will be excluded from the criteria computation if the epsilon argument of 'CreateInputsCrit' = NULL")
57
58
      }  
    }
59
60
61
    if ("epsilon" %in% names(InputsCrit) & !is.null(InputsCrit$epsilon)) {
      VarObs <- VarObs + InputsCrit$epsilon
      VarSim <- VarSim + InputsCrit$epsilon
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    }
    if (InputsCrit$transfo == "sqrt") {
      VarObs <- sqrt(VarObs)
      VarSim <- sqrt(VarSim)
    }
    if (InputsCrit$transfo == "log") {
      VarObs <- log(VarObs)
      VarSim <- log(VarSim)
      VarSim[VarSim      < -1e100] <- NA
    }
    if (InputsCrit$transfo == "inv") {
      VarObs <- 1 / VarObs
      VarSim <- 1 / VarSim
      VarSim[abs(VarSim) > 1e+100] <- NA
    }
    if (InputsCrit$transfo == "sort") {
      VarSim[is.na(VarObs)] <- NA
      VarSim <- sort(VarSim, na.last = TRUE)
      VarObs <- sort(VarObs, na.last = TRUE)
      InputsCrit$BoolCrit <-
        sort(InputsCrit$BoolCrit, decreasing = TRUE)
    }
    
    ##TS_ignore
    TS_ignore <- !is.finite(VarObs) | !is.finite(VarSim) | !InputsCrit$BoolCrit
    Ind_TS_ignore <- which(TS_ignore)
    if (length(Ind_TS_ignore) == 0) {
      Ind_TS_ignore <- NULL
    }
    if (sum(!TS_ignore) == 0) {
      OutputsCrit <- list(NA)
      names(OutputsCrit) <- c("CritValue")
      return(OutputsCrit)
    }
    if (inherits(OutputsModel, "hourly")) {
      WarningTS <- 365
    }
    if (inherits(OutputsModel, "daily")) {
      WarningTS <- 365
    }
    if (inherits(OutputsModel, "monthly")) {
      WarningTS <-  12
    }
    if (inherits(OutputsModel, "yearly")) {
      WarningTS <-   3
    }
    if (sum(!TS_ignore) < WarningTS & warnings) {
      warning("\t criterion computed on less than ", WarningTS, " time-steps")
    }
    
    
    ##ErrorCrit______________________________________
    Numer <- sum((VarSim - VarObs)^2, na.rm = TRUE)
    Denom <- sum(!is.na(VarObs))
    
    if (Numer == 0) {
      Crit <- 0
    } else {
      Crit <- sqrt(Numer / Denom)
    }
    if (is.numeric(Crit) & is.finite(Crit)) {
      CritValue <- Crit
    }
    
    
    ##Verbose______________________________________
    if (verbose) {
129
      message("Crit. ", CritName, " = ", sprintf("%.4f", CritValue), "\n")
130
131
132
133
134
135
136
137
138
139
    }
    
    
    ##Output_________________________________________
    OutputsCrit <- list(CritValue       = CritValue,
                        CritName        = CritName,
                        CritBestValue   = CritBestValue,
                        Multiplier      = Multiplier,
                        Ind_notcomputed = Ind_TS_ignore
    )
140
141
    
    class(OutputsCrit) <- c("RMSE", "ErrorCrit")
142
143
    return(OutputsCrit)
    
144
  }