ErrorCrit_KGE2.R 6.83 KB
Newer Older
1
ErrorCrit_KGE2 <- function(InputsCrit, OutputsModel, warnings = TRUE, verbose = TRUE) {
2
3
  
  
4
5
  ##Arguments_check________________________________
  if (!inherits(InputsCrit, "InputsCrit")) {
6
    stop("InputsCrit must be of class 'InputsCrit'")
7
8
9
10
11
  }
  if (inherits(InputsCrit, "Multi") | inherits(InputsCrit, "Compo")) {
    stop("InputsCrit must be of class 'Single'. Use the ErrorCrit function on objects of class 'Multi' or 'Compo' with KGE'")
  }
  if (!inherits(OutputsModel, "OutputsModel")) {
12
    stop("OutputsModel must be of class 'OutputsModel'")
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  }
  
  
  ##Initialisation_________________________________
  CritName <- NA
  CritVar  <- InputsCrit$varObs
  if (InputsCrit$transfo == "") {
    CritName <- "KGE'[CritVar]"
  }
  if (InputsCrit$transfo == "sqrt") {
    CritName <- "KGE'[sqrt(CritVar)]"
  }
  if (InputsCrit$transfo == "log") {
    CritName <- "KGE'[log(CritVar)]"
  }
  if (InputsCrit$transfo == "inv") {
    CritName <- "KGE'[1/CritVar]"
  }
  if (InputsCrit$transfo == "sort") {
    CritName <- "KGE'[sort(CritVar)]"
  }
  CritName      <- gsub(pattern = "CritVar", replacement = CritVar, x = CritName)
  CritValue     <- NA
  CritBestValue <- +1
  Multiplier    <- -1
  ### must be equal to -1 or +1 only
  
  
  ##Data_preparation_______________________________
  VarObs <- InputsCrit$obs
  VarObs[!InputsCrit$BoolCrit] <- NA
44
45
46
47
48
49
50
51
52
  if (InputsCrit$varObs == "Q") {
    VarSim <- OutputsModel$Qsim
  }
  if (InputsCrit$varObs == "SCA") {
    VarSim <- rowMeans(sapply(OutputsModel$CemaNeigeLayers[InputsCrit$groupLayer], FUN = "[[", "Gratio"))
  }
  if (InputsCrit$varObs == "SWE") {
    VarSim <- rowMeans(sapply(OutputsModel$CemaNeigeLayers[InputsCrit$groupLayer], FUN = "[[", "SnowPack"))
  }
53
54
55
56
57
58
59
60
61
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  VarSim[!InputsCrit$BoolCrit] <- NA
  
  ##Data_transformation
  if (InputsCrit$transfo %in% c("log", "inv") & is.null(InputsCrit$epsilon) & warnings) {
    if (any(VarObs %in% 0)) {
      warning("zeroes detected in Qobs: the corresponding time-steps will be excluded from the criteria computation if the epsilon argument of 'CreateInputsCrit' = NULL")
    }
    if (any(VarSim %in% 0)) {
      warning("zeroes detected in Qsim: the corresponding time-steps will be excluded from the criteria computation if the epsilon argument of 'CreateInputsCrit' = NULL")
    }  
  }
  if ("epsilon" %in% names(InputsCrit) & !is.null(InputsCrit$epsilon)) {
    VarObs <- VarObs + InputsCrit$epsilon
    VarSim <- VarSim + InputsCrit$epsilon
  }
  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 (sum(!TS_ignore) == 1) {
    OutputsCrit <- list(NA)
    names(OutputsCrit) <- c("CritValue")
    return(OutputsCrit)
  } ### to avoid a problem in standard deviation computation
  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")
  }
  
  ##Other_variables_preparation
  meanVarObs <- mean(VarObs[!TS_ignore])
  meanVarSim <- mean(VarSim[!TS_ignore])
  
  iCrit           <- 0
  SubCritPrint    <- NULL
  SubCritNames    <- NULL
  SubCritValues   <- NULL
  
  
  ##SubErrorCrit_____KGE_rPearson__________________
  iCrit <- iCrit + 1
  SubCritPrint[iCrit]  <- paste(CritName, " cor(sim, obs, \"pearson\") =", sep = "")
  SubCritValues[iCrit] <- NA
  SubCritNames[iCrit]  <- "r"
  
  Numer <- sum((VarObs[!TS_ignore] - meanVarObs) * (VarSim[!TS_ignore] - meanVarSim))
  Deno1 <- sqrt(sum((VarObs[!TS_ignore] - meanVarObs)^2))
  Deno2 <- sqrt(sum((VarSim[!TS_ignore] - meanVarSim)^2))
  
  if (Numer == 0) {
    if (Deno1 == 0 & Deno2 == 0) {
143
144
      Crit <- 1
    } else {
145
      Crit <- 0
146
    }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  } else {
    Crit  <- Numer / (Deno1 * Deno2)
  }
  if (is.numeric(Crit) & is.finite(Crit)) {
    SubCritValues[iCrit] <- Crit
  }
  
  
  ##SubErrorCrit_____KGE_gamma______________________
  iCrit <- iCrit + 1
  SubCritPrint[iCrit]  <- paste(CritName, " cv(sim)/cv(obs)          =", sep = "")
  SubCritValues[iCrit] <- NA
  SubCritNames[iCrit]  <- "gamma"
  
  if (meanVarSim == 0) {
    if (sd(VarSim[!TS_ignore]) == 0) {
      CVsim <- 1
164
    } else {
165
      CVsim <- 99999
166
    }
167
168
  } else {
    CVsim <- sd(VarSim[!TS_ignore]) / meanVarSim
169
    
170
171
172
173
174
175
  }
  if (meanVarObs == 0) {
    if (sd(VarObs[!TS_ignore]) == 0) {
      CVobs <- 1
    } else {
      CVobs <- 99999
176
    }
177
178
  } else {
    CVobs <- sd(VarObs[!TS_ignore]) / meanVarObs
179
  }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  if (CVsim == 0 &
      CVobs == 0) {
    Crit <- 1
  } else {
    Crit <- CVsim / CVobs
  }
  if (is.numeric(Crit) & is.finite(Crit)) {
    SubCritValues[iCrit] <- Crit
  }
  
  
  ##SubErrorCrit_____KGE_beta______________________
  iCrit <- iCrit + 1
  SubCritPrint[iCrit]  <- paste(CritName, " mean(sim)/mean(obs)      =", sep = "")
  SubCritValues[iCrit] <- NA
  SubCritNames[iCrit]  <- "beta"
  
  if (meanVarSim == 0 & meanVarObs == 0) {
    Crit <- 1
  } else {
    Crit <- meanVarSim / meanVarObs
  }
  if (is.numeric(Crit) & is.finite(Crit)) {
    SubCritValues[iCrit] <- Crit
  }
  
  
  ##ErrorCrit______________________________________
  if (sum(is.na(SubCritValues)) == 0) {
    CritValue <- (1 - sqrt((SubCritValues[1] - 1)^2 + (SubCritValues[2] - 1)^2 + (SubCritValues[3] - 1)^2))
  }
  
  
  ##Verbose______________________________________
  if (verbose) {
    message("Crit. ", CritName, " = ", sprintf("%.4f", CritValue))
    message(paste("\tSubCrit.", SubCritPrint, sprintf("%.4f", SubCritValues), "\n", sep = " "))
  }
  
  
  ##Output_________________________________________
  OutputsCrit <- list(CritValue       = CritValue,
                      CritName        = CritName,
                      SubCritValues   = SubCritValues,
                      SubCritNames    = SubCritNames,
                      CritBestValue   = CritBestValue,
                      Multiplier      = Multiplier,
                      Ind_notcomputed = Ind_TS_ignore
  )
  
  class(OutputsCrit) <- c("KGE2", "ErrorCrit")
  return(OutputsCrit)
  
}