-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiomod3_class3_EM.R
241 lines (215 loc) · 10.4 KB
/
biomod3_class3_EM.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
## --------------------------------------------------------------------------- #
## MS.ensemble.models.out ---------------------------------------------
## --------------------------------------------------------------------------- #
##' @name MS.ensemble.models.out
##' @aliases MS.ensemble.models.out-class
##' @author Damien Georges
##'
##' @title \code{BIOMOD_EnsembleModeling()} output object class
##'
##' @description Class returned by \code{\link{BIOMOD_EnsembleModeling}}, and used by
##' \code{\link{BIOMOD_LoadModels}}, \code{\link{BIOMOD_PresenceOnly}} and
##' \code{\link{BIOMOD_EnsembleForecasting}}
##'
##'
##' @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} corresponding to the species name
##' @slot expl.var.names a \code{vector} containing names of explanatory
##' variables
##' @slot data.type a \code{character} corresponding to the data type
##' @slot models.out a \code{\link{BIOMOD.stored.models.out-class}} object
##' containing informations from \code{\link{BIOMOD_Modeling}} object
##' @slot em.by a \code{character} corresponding to the way kept models have
##' been combined to build the ensemble models, must be among
##' \code{PA+run}, \code{PA+algo}, \code{PA},
##' \code{algo}, \code{all}
##' @slot em.computed a \code{vector} containing names of ensemble models
##' @slot em.failed a \code{vector} containing names of failed ensemble models
# ##' @slot em.models_needed a \code{list} containing single models for each ensemble model
##' @slot em.models_kept a \code{list} containing single models for each ensemble model
##' @slot link a \code{character} containing the file name of the saved object
##'
##' @param object a \code{\link{MS.ensemble.models.out}} object
##'
##'
##' @rdname MS.ensemble.models.out
##' @export
##'
# Class Definition ---------------------------------------------------------
setClass("MS.ensemble.models.out",
representation(ms.project = 'character',
modeling.id = 'character',
dir.name = 'character',
sp.name = 'character',
expl.var.names = 'character',
data.type = 'character',
models.out = 'BIOMOD.stored.ms.models.out',
em.by = 'character',
em.computed = 'list',
em.failed = 'list',
em.models_kept = 'list',
link = 'character'),
prototype(modeling.id = '.',
dir.name = '.',
sp.name = '',
expl.var.names = '',
data.type = '',
models.out = new('BIOMOD.stored.ms.models.out'),
em.by = character(),
em.computed = NULL,
em.failed = NULL,
em.models_kept = NULL),
validity = function(object){ return(TRUE) })
# Other functions ----------------------------------------------------------
## show.MS.ensemble.models.out ---------------------------------------------
##'
##' @rdname MS.ensemble.models.out
##' @importMethodsFrom methods show
##' @export
##'
setMethod('show', signature('MS.ensemble.models.out'), function(object) {
.bm_cat("MS.ensemble.models.out")
cat("\nsp.name :", [email protected], fill = .Options$width)
cat("\nexpl.var.names :", [email protected], fill = .Options$width)
cat("\n")
cat("\nmodels computed:", fill = .Options$width)
show([email protected])
cat("\nmodels failed:", fill = .Options$width)
show([email protected])
.bm_cat()
})
## get_formal_data.MS.ensemble.models.out ----------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_formal_data", "MS.ensemble.models.out",
function(obj, sp, subinfo = NULL)
{
nameFolder <- file.path([email protected], sp)
em <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".ensemble.models.out"))))
if (is.null(subinfo)) {
return(load_stored_object([email protected]))
} else {
bm_form = get_formal_data(em)
return(get_formal_data(bm_form, subinfo = subinfo))
}
}
)
## get_built_models.MS.ensemble.models.out ---------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_built_models", "MS.ensemble.models.out",
function(obj, full.name = NULL, merged.by.algo = NULL, merged.by.run = NULL
, merged.by.PA = NULL, filtered.by = NULL, algo = NULL)
{
out <- [email protected]
keep_ind <- .filter_outputs.vec(out, obj.type = "em", subset.list = list(full.name = full.name
, merged.by.PA = merged.by.PA
, merged.by.run = merged.by.run
, merged.by.algo = merged.by.algo
, filtered.by = filtered.by
, algo = algo))
out <- out[keep_ind]
return(out)
}
)
## get_kept_models.MS.ensemble.models.out ----------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_kept_models", "MS.ensemble.models.out", function(obj) { return([email protected]_kept) })
## get_predictions.MS.ensemble.models.out ----------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_predictions", "MS.ensemble.models.out",
function(obj, sp, evaluation = FALSE, full.name = NULL, merged.by.algo = NULL, merged.by.run = NULL
, merged.by.PA = NULL, filtered.by = NULL, algo = NULL,
model.as.col = FALSE)
{
nameFolder <- file.path([email protected], sp)
em <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".ensemble.models.out"))))
# check evaluation data availability
if (evaluation && (!get_formal_data(em)@has.evaluation.data)) {
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
, merged.by.algo = merged.by.algo
, merged.by.run = merged.by.run
, merged.by.PA = merged.by.PA
, filtered.by = filtered.by
, algo = algo))
out <- out[keep_lines, ]
if (model.as.col) {
out <- .transform_model.as.col(out)
}
return(out)
}
)
## get_evaluations.MS.ensemble.models.out ----------------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_evaluations", "MS.ensemble.models.out",
function(obj, sp, full.name = NULL, merged.by.algo = NULL, merged.by.run = NULL
, merged.by.PA = NULL, filtered.by = NULL, algo = NULL, metric.eval = NULL)
{
nameFolder <- file.path([email protected], sp)
em <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".ensemble.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
, merged.by.algo = merged.by.algo
, merged.by.run = merged.by.run
, merged.by.PA = merged.by.PA
, filtered.by = filtered.by
, algo = algo
, metric.eval = metric.eval))
out <- out[keep_lines, ]
return(out)
}
}
)
## get_variables_importance.MS.ensemble.models.out -------------------------
##'
##' @rdname getters.out
##' @export
##'
setMethod("get_variables_importance", "MS.ensemble.models.out",
function(obj, sp, full.name = NULL, merged.by.algo = NULL, merged.by.run = NULL
, merged.by.PA = NULL, filtered.by = NULL, algo = NULL, expl.var = NULL)
{
nameFolder <- file.path([email protected], sp)
em <- get(load(file.path(nameFolder, paste0(sp,".", [email protected], ".ensemble.models.out"))))
out <- load_stored_object([email protected])
keep_lines <- .filter_outputs.df(out, subset.list = list(full.name = full.name
, merged.by.algo = merged.by.algo
, merged.by.run = merged.by.run
, merged.by.PA = merged.by.PA
, filtered.by = filtered.by
, algo = algo
, expl.var = expl.var))
out <- out[keep_lines, ]
return(out)
}
)