-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiomod3_class2_SM.R
380 lines (348 loc) · 17.2 KB
/
biomod3_class2_SM.R
1
2
3
4
5
6
7
8
9
10
11
12
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
44
45
46
47
48
49
50
51
52
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
## -------------------------------------------------------------------------- #
## 1. MS.models.out -----------------------------------------------------
## -------------------------------------------------------------------------- #
##' @name MS.models.out
##' @aliases MS.models.out-class
##' @author Hélène Blancheteau
##'
##' @title \code{MS_Modeling()} output object class
##'
##' @slot ms.project a \code{character} corresponding to the name of
##' the multispecies project
##' @slot modeling.id a \code{character} corresponding to the name (ID) of the
##' simulation set
##' @slot dir.name a \code{character} corresponding to the modeling folder
##' @slot sp.name a \code{character vector} corresponding to the species name
##' @slot data.type a \code{character} corresponding to the data type
##' @slot expl.var.names a \code{vector} containing names of explanatory
##' variables
##' @slot models.computed a \code{vector} containing names of computed models
##' @slot models.failed a \code{vector} containing names of failed models
##' @slot has.evaluation.data a \code{logical} value defining whether evaluation
##' data is given
##' @slot scale.models a \code{logical} value defining whether models have been
##' rescaled or not
##' @slot link a \code{character} containing the file name of the saved object
##'
##' @param object a \code{\link{MS.models.out}} object
##'
##' @rdname MS.models.out
##' @export
setClass("MS.models.out",
representation(ms.project = 'character',
modeling.id = 'character',
dir.name = 'character',
sp.name = 'character',
data.type = 'character',
expl.var.names = 'character',
models.computed = 'list',
models.failed = 'list',
has.evaluation.data = 'logical',
scale.models = 'logical',
link = 'character'),
prototype(modeling.id = as.character(format(Sys.time(), "%s")),
dir.name = '.',
sp.name = '',
expl.var.names = '',
has.evaluation.data = FALSE,
scale.models = TRUE),
validity = function(object){ return(TRUE) } )
setClass("BIOMOD.stored.ms.models.out",
representation(val = 'MS.models.out'),
prototype(val = NULL),
validity = function(object) { return(TRUE) })
##' @name getters.out
##' @aliases get_species_data
##' @aliases get_eval_data
##' @aliases get_options
##' @aliases get_calib_lines
##' @aliases get_formal_data
##' @aliases get_projected_models
##' @aliases free
##' @aliases get_predictions
##' @aliases get_kept_models
##' @aliases get_built_models
##' @aliases get_evaluations
##' @aliases get_variables_importance
##'
##' @title Functions to extract informations from \code{\link{MS.models.out}},
##' \code{\link{MS.projection.out}} or \code{\link{MS.ensemble.models.out}} objects
##'
##' @param obj a \code{\link{MS.formated.data}}, \code{\link{MS.models.out}}, \code{\link{MS.projection.out}} or
##' \code{\link{MS.ensemble.models.out}} object
##' @param sp a \code{character} name of a specific species
##' @param as.data.frame a \code{logical} defining whether output should be returned as
##' \code{data.frame} or \code{array} object
##' @param subinfo a \code{character} corresponding to the information to be extracted, must be
##' among \code{NULL}, \code{expl.var.names}, \code{resp.var}, \code{expl.var}, \code{MinMax},
##' \code{eval.resp.var}, \code{eval.expl.var} (see Details)
##' @param evaluation a \code{logical} defining whether evaluation data should be used or not
##' @param full.name (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing model names to be kept, must be either \code{all} or a
##' sub-selection of model names that can be obtained with the \code{\link{get_built_models}}
##' function
##'
##' @param PA (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing pseudo-absence set to be loaded, must be among \code{PA1},
##' \code{PA2}, \code{...}, \code{allData}
##' @param run (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing repetition set to be loaded, must be among \code{RUN1},
##' \code{RUN2}, \code{...}, \code{allRun}
##' @param algo (\emph{optional, default} \code{NULL}) \cr
##' A \code{character} containing algorithm to be loaded, must be either
##' \code{ANN}, \code{CTA}, \code{FDA}, \code{GAM}, \code{GBM}, \code{GLM}, \code{MARS},
##' \code{MAXENT}, \code{MAXNET}, \code{RF}, \code{SRE}, \code{XGBOOST}
##'
##' @param merged.by.PA (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing merged pseudo-absence set to be loaded, must be among \code{PA1},
##' \code{PA2}, \code{...}, \code{mergedData}
##' @param merged.by.run (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing merged repetition set to be loaded, must be among \code{RUN1},
##' \code{RUN2}, \code{...}, \code{mergedRun}
##' @param merged.by.algo (\emph{optional, default} \code{NULL}) \cr
##' A \code{character} containing merged algorithm to be loaded, must be among
##' \code{ANN}, \code{CTA}, \code{FDA}, \code{GAM}, \code{GBM}, \code{GLM}, \code{MARS},
##' \code{MAXENT}, \code{MAXNET}, \code{RF}, \code{SRE}, \code{XGBOOST}, \code{mergedAlgo}
##' @param filtered.by (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing evaluation metric selected to filter single models to build the
##' ensemble models, must be among \code{POD}, \code{FAR}, \code{POFD}, \code{SR},
##' \code{ACCURACY}, \code{BIAS}, \code{ROC}, \code{TSS}, \code{KAPPA}, \code{OR}, \code{ORSS},
##' \code{CSI}, \code{ETS}, \code{BOYCE}, \code{MPA}
##'
##' @param metric.eval (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing evaluation metric to be kept, must be among \code{POD},
##' \code{FAR}, \code{POFD}, \code{SR}, \code{ACCURACY}, \code{BIAS}, \code{ROC}, \code{TSS},
##' \code{KAPPA}, \code{OR}, \code{ORSS}, \code{CSI}, \code{ETS}, \code{BOYCE}, \code{MPA}
##' @param expl.var (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing explanatory variables to be kept, that can be obtained with the
##' \code{\link{get_formal_data}(obj, subinfo = 'expl.var.names')} function
##'
##' @param metric.binary (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing evaluation metric selected to transform predictions into binary
##' values, must be among \code{POD}, \code{FAR}, \code{POFD}, \code{SR}, \code{ACCURACY},
##' \code{BIAS}, \code{ROC}, \code{TSS}, \code{KAPPA}, \code{OR}, \code{ORSS}, \code{CSI},
##' \code{ETS}, \code{BOYCE}, \code{MPA}
##' @param metric.filter (\emph{optional, default} \code{NULL}) \cr
##' A \code{vector} containing evaluation metric to filter predictions, must be among \code{POD},
##' \code{FAR}, \code{POFD}, \code{SR}, \code{ACCURACY}, \code{BIAS}, \code{ROC}, \code{TSS},
##' \code{KAPPA}, \code{OR}, \code{ORSS}, \code{CSI}, \code{ETS}, \code{BOYCE}, \code{MPA}
##'
##' @param model.as.col (\emph{optional, default} \code{FALSE}) \cr
##' A \code{boolean} given to \code{\link{get_predictions}}. If \code{TRUE}
##' prediction are returned as a wide \code{data.frame} with each column containing
##' predictions for a single model and corresponding to the old output given by
##' \pkg{biomod2} in version < 4.2-2. If \code{FALSE} predictions are returned
##' as a long \code{data.frame} with many additional informations readily
##' available.
##' @param ... additional parameters
##'
##' @importFrom biomod2 load_stored_object get_options
##' @importMethodsFrom biomod2 get_species_data get_eval_data get_options get_calib_lines get_projected_models free get_predictions get_kept_models get_formal_data get_built_models get_evaluations get_variables_importance
##'
NULL
# setGeneric("get_species_data", function(obj, ...) { standardGeneric("get_species_data") }) ## 012
# setGeneric("get_eval_data", function(obj, ...) { standardGeneric("get_eval_data") }) ## 012
#
# setGeneric("get_options", function(obj, ...) { standardGeneric("get_options") }) ## A
# setGeneric("get_calib_lines", function(obj, ...) { standardGeneric("get_calib_lines") }) ## A
#
# setGeneric("get_projected_models", function(obj, ...) { standardGeneric("get_projected_models") }) ## B
# setGeneric("free", function(obj, ...) { standardGeneric("free") }) ## B
#
# setGeneric("get_predictions", function(obj, ...) { standardGeneric("get_predictions") }) ## ABC
#
# setGeneric("get_kept_models", function(obj, ...) { standardGeneric("get_kept_models") }) ## C
#
# setGeneric("get_formal_data", function(obj, ...) { standardGeneric("get_formal_data") }) ## AC
# setGeneric("get_built_models", function(obj, ...) { standardGeneric("get_built_models") }) ## AC
# setGeneric("get_evaluations", function(obj, ...) { standardGeneric("get_evaluations") }) ## AC
# setGeneric("get_variables_importance", function(obj, ...) { standardGeneric("get_variables_importance") }) ## AC
## show.MS.models.out ---------------------------------------------------
##'
##' @rdname MS.models.out
##' @importMethodsFrom methods show
##' @export
##'
setMethod('show', signature('MS.models.out'), function(object) {
.bm_cat("MS.models.out")
cat("\nModeling folder :", [email protected], fill = .Options$width)
cat("\nSpecies modeled :", [email protected], fill = .Options$width)
cat("\nModeling id :", [email protected], fill = .Options$width)
cat("\nConsidered variables :", [email protected], fill = .Options$width)
cat("\n\nComputed Models : ", fill = .Options$width)
show([email protected])
.bm_cat()
})
## get_options.MS.models.out ---------------------------------------------------
##'
##' @rdname getters.out
##' @importFrom biomod2 get_options
##' @importMethodsFrom biomod2 get_options
##' @export
##'
setMethod("get_options", signature("MS.models.out"), function(obj, sp) {
nameFolder <- file.path([email protected], sp)
model <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".models.out"))))
model_options <- load_stored_object([email protected])
return(model_options)
})
## get_calib_lines.MS.models.out ---------------------------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_calib_lines", "MS.models.out",
function(obj, as.data.frame = FALSE, PA = NULL, run = NULL)
{
out_total <- foreach (sp = [email protected], .combine = rbind) %do% {
nameFolder <- file.path([email protected], sp)
model <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".models.out"))))
out <- load_stored_object([email protected])
if (!is.null(out) && as.data.frame == TRUE) {
tmp <- melt(out, varnames = c("points", "PA_run"))
tmp$PA = strsplit(sub("^_", "", tmp$PA_run), "_")[[1]][1]
tmp$run = strsplit(sub("^_", "", tmp$PA_run), "_")[[1]][2]
out <- tmp[, c("PA", "run", "points", "value")]
colnames(out)[4] = "calib.lines"
keep_lines <- .filter_outputs.df(out, subset.list = list(PA = PA, run = run))
out <- out[keep_lines, ]
}
names(out) <- paste(names(out), sp, sep = "_")
return(out)
}
return(out_total)
}
)
## get_formal_data.MS.models.out ---------------------------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_formal_data", "MS.models.out",
function(obj, sp, subinfo = NULL)
{
nameFolder <- file.path([email protected], sp)
model <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".models.out"))))
if (is.null(subinfo)) {
return(load_stored_object([email protected]))
} else if (subinfo == 'MinMax') {
env = as.data.frame(get_formal_data(model)@data.env.var)
MinMax = foreach(i = 1:ncol(env)) %do%
{
x = env[, i]
if (is.numeric(x)) {
return(list(min = min(x, na.rm = TRUE)
, max = max(x, na.rm = TRUE)))
} else if (is.factor(x)) {
return(list(levels = levels(x)))
}
}
names(MinMax) = colnames(env)
return(MinMax)
} else if (subinfo == 'expl.var') {
return(as.data.frame(get_formal_data(model)@data.env.var))
} else if (subinfo == 'expl.var.names') {
return([email protected])
} else if (subinfo == 'resp.var') {
return(get_formal_data(model)@data.species)
} else if (subinfo == 'eval.resp.var') {
return(as.numeric(get_formal_data(model)@eval.data.species))
} else if (subinfo == 'eval.expl.var') {
return(as.data.frame(get_formal_data(model)@eval.data.env.var))
} else { stop("Unknown subinfo tag")}
}
)
## get_predictions.MS.models.out ---------------------------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_predictions", "MS.models.out",
function(obj, sp, evaluation = FALSE
, full.name = NULL, PA = NULL, run = NULL, algo = NULL
, model.as.col = FALSE)
{
nameFolder <- file.path([email protected], sp)
model <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".models.out"))))
if (evaluation && ([email protected])) {
warning("! Calibration data returned because no evaluation data available")
evaluation = FALSE
}
# select calibration or eval data
if (evaluation) {
out <- load_stored_object([email protected])
} else {
out <- load_stored_object([email protected])
}
# subselection of models_selected
keep_lines <- .filter_outputs.df(out, subset.list = list(full.name = full.name, PA = PA
, run = run, algo = algo))
out <- out[keep_lines, ]
if (model.as.col) {
out <- .transform_model.as.col(out)
}
return(out)
}
)
## get_built_models.MS.models.out ---------------------------------------------------
##' @rdname getters.out
##' @export
##'
setMethod("get_built_models", "MS.models.out",
function(obj, full.name = NULL, PA = NULL, run = NULL, algo = NULL)
{
out <- [email protected]
for (sp in [email protected]){
vec <- out[[sp]]
keep_ind <- .filter_outputs.vec(vec, obj.type = "mod", subset.list = list(full.name = full.name, PA = PA
, run = run, algo = algo))
out[[sp]] <- vec[keep_ind]
}
return(out)
}
)
## get_evaluations.MS.models.out ---------------------------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_evaluations", "MS.models.out",
function(obj, sp, full.name = NULL, PA = NULL, run = NULL, algo = NULL, metric.eval = NULL)
{
nameFolder <- file.path([email protected], sp)
model <- get(load(file.path(nameFolder, paste0(sp, ".", [email protected], ".models.out"))))
out <- load_stored_object([email protected])
if (nrow(out) == 0) {
cat("\n! models have no evaluations\n")
return(invisible(NULL))
} else {
keep_lines <- .filter_outputs.df(out, subset.list = list(full.name = full.name, PA = PA
, run = run, algo = algo
, metric.eval = metric.eval))
out <- out[keep_lines, ]
return(out)
}
}
)
## get_variables_importance.MS.models.out ---------------------------------------------------
##' @rdname getters.out
##' @export
##'
setMethod("get_variables_importance", "MS.models.out",
function(obj, sp, full.name = NULL, PA = NULL, run = NULL, algo = NULL, expl.var = NULL)
{
nameFolder <- file.path([email protected], sp)
model <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".models.out"))))
out <- load_stored_object([email protected])
if ([email protected]@link == '') {
cat("\n! models have no variables importance\n")
return(invisible(NULL))
} else {
keep_lines <- .filter_outputs.df(out, subset.list = list(full.name = full.name, PA = PA
, run = run, algo = algo
, expl.var = expl.var))
out <- out[keep_lines, ]
return(out)
}
}
)