-
Notifications
You must be signed in to change notification settings - Fork 21
/
biomod2_classes_5.R
713 lines (630 loc) · 27.5 KB
/
biomod2_classes_5.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
##' @name predict.em
##' @author Damien Georges
##'
##' @title Functions to get predictions from \code{\link{biomod2_ensemble_model}} objects
##'
##' @description This function allows the user to predict single models from
##' \code{\link{biomod2_ensemble_model}} on (new) explanatory variables.
##'
##'
##' @param object a \code{\link{biomod2_ensemble_model}} object
##' @param newdata a \code{data.frame} or \code{\link[terra:rast]{SpatRaster}} object
##' containing data for new predictions
##' @param \ldots (\emph{optional})
##'
##'
##' @seealso \code{\link{biomod2_ensemble_model}}
##' @family Toolbox functions
##'
##'
##' @importFrom terra app classify nlyr
##' @importFrom stats qt sd
##'
NULL
#setGeneric("predict", def = function(object, ...) { standardGeneric("predict") })
# ---------------------------------------------------------------------------- #
# 9. biomod2_ensemble_model --------------------------------------------------
# ---------------------------------------------------------------------------- #
##' @name biomod2_ensemble_model
##' @aliases biomod2_ensemble_model-class
##' @aliases EMmean_biomod2_model-class
##' @aliases EMmedian_biomod2_model-class
##' @aliases EMcv_biomod2_model-class
##' @aliases EMci_biomod2_model-class
##' @aliases EMca_biomod2_model-class
##' @aliases EMwmean_biomod2_model-class
##' @author Damien Georges
##'
##' @title Ensemble model output object class (when running \code{BIOMOD_EnsembleModeling()})
##'
##' @description Class created by \code{\link{BIOMOD_EnsembleModeling}}
##'
##'
##' @slot modeling.id a \code{character} corresponding to the name (ID) of the simulation set
##' @slot model_name a \code{character} corresponding to the model name
##' @slot model_class a \code{character} corresponding to the model class
##' @slot model_options a \code{list} containing the model options
##' @slot model the corresponding model object
##' @slot scaling_model the corresponding scaled model object
##' @slot dir_name a \code{character} corresponding to the modeling folder
##' @slot resp_name a \code{character} corresponding to the species name
##' @slot expl_var_names a \code{vector} containing names of explanatory variables
##' @slot expl_var_type a \code{vector} containing classes of explanatory variables
##' @slot expl_var_range a \code{list} containing ranges of explanatory variables
##' @slot model_evaluation a \code{data.frame} containing the model evaluations
##' @slot model_variables_importance a \code{data.frame} containing the model variables importance
##'
##' @param object a \code{\link{biomod2_ensemble_model}} object
##'
##' @details
##'
##' \code{biomod2_model} is the basic object for \pkg{biomod2} ensemble species distribution models.
##' \cr All listed classes below are derived from \code{biomod2_model}, and have a
##' \code{model_class} slot specific value :
##'
##' \itemize{
##' \item \code{biomod2_ensemble_model} : \code{model_class} is \code{EM}
##' \item \code{EMmean_biomod2_model} : \code{model_class} is \code{EMmean}
##' \item \code{EMmedian_biomod2_model} : \code{model_class} is \code{EMmedian}
##' \item \code{EMcv_biomod2_model} : \code{model_class} is \code{EMcv}
##' \item \code{EMci_biomod2_model} : \code{model_class} is \code{EMci}
##' \item \code{EMca_biomod2_model} : \code{model_class} is \code{EMca}
##' \item \code{EMwmean_biomod2_model} : \code{model_class} is \code{EMwmean}
##' }
##'
##'
##' @seealso \code{\link{biomod2_model}}, \code{\link{BIOMOD_EnsembleModeling}}
##' @family Toolbox objects
##'
##'
##' @examples
##'
##' showClass("biomod2_ensemble_model")
##' showClass("EMmean_biomod2_model")
##' showClass("EMmedian_biomod2_model")
##' showClass("EMcv_biomod2_model")
##' showClass("EMci_biomod2_model")
##' showClass("EMca_biomod2_model")
##' showClass("EMwmean_biomod2_model")
##'
##'
NULL
##' @name biomod2_ensemble_model-class
##' @rdname biomod2_ensemble_model
##' @export
##'
## 9.1 Class Definition ---------------------------------------------------------
setClass('biomod2_ensemble_model',
representation(modeling.id = 'character'), ## maybe some additional args should be added here
contains = 'biomod2_model',
prototype = list(model_class = 'EM'),
validity = function(object) { return(TRUE) })
## 9.2 Show method -------------------------------------------------------------
##' @rdname biomod2_ensemble_model
##' @importMethodsFrom methods show
##' @importFrom methods callNextMethod
##' @export
##'
setMethod('show', signature('biomod2_ensemble_model'),
function(object) {
callNextMethod(object)
})
## 9.3 predict2 method -------------------------------------------------------------
### biomod2_ensemble_model predict2.em doc -------------------------------------
##' @name predict2.em
##' @aliases predict2.biomod2_ensemble_model.SpatRaster
##' @aliases predict2.biomod2_ensemble_model.data.frame
##' @aliases predict2.EMmean_biomod2_model.SpatRaster
##' @aliases predict2.EMmean_biomod2_model.data.frame
##' @aliases predict2.EMmedian_biomod2_model.SpatRaster
##' @aliases predict2.EMmedian_biomod2_model.data.frame
##' @aliases predict2.EMcv_biomod2_model.SpatRaster
##' @aliases predict2.EMcv_biomod2_model.data.frame
##' @aliases predict2.EMci_biomod2_model.SpatRaster
##' @aliases predict2.EMci_biomod2_model.data.frame
##' @aliases predict2.EMca_biomod2_model.SpatRaster
##' @aliases predict2.EMca_biomod2_model.data.frame
##' @aliases predict2.EMwmean_biomod2_model.SpatRaster
##' @aliases predict2.EMwmean_biomod2_model.data.frame
##' @author Remi Patin
##'
##' @title Functions to get predictions from \code{\link{biomod2_ensemble_model}} objects
##'
##' @description This function allows the user to predict single models from
##' \code{\link{biomod2_ensemble_model}} on (new) explanatory variables.
##'
##'
##' @param object a \code{\link{biomod2_ensemble_model}} object
##' @param newdata a \code{data.frame} or \code{\link[terra:rast]{SpatRaster}} object
##' containing data for new predictions
##' @param data_as_formal_predictions (\emph{optional, default} \code{FALSE}). A
##' \code{boolean} describing whether \code{newdata} is given as raw environmental
##' data (\code{FALSE}) or as formal predictions of the individual models
##' used to build the ensemble model (\code{TRUE}).
##'
##' @param \ldots (\emph{optional})
##' @inheritParams predict2.bm
##'
##' @seealso \code{\link{biomod2_ensemble_model}}
##' @family Toolbox functions
##'
##'
##' @importFrom terra rast app classify writeRaster
##' @keywords internal
NULL
### biomod2_ensemble_model + SpatRaster -------------------------------------------------
##' @rdname predict2.em
setMethod('predict2', signature(object = 'biomod2_ensemble_model', newdata = "SpatRaster"),
function(object, newdata, predfun, seedval = NULL, ...) {
args <- list(...)
data_as_formal_predictions <- args$data_as_formal_predictions
if (is.null(data_as_formal_predictions)) {
data_as_formal_predictions <- FALSE
}
filename <- args$filename
overwrite <- args$overwrite
on_0_1000 <- args$on_0_1000
na.rm <- args$na.rm
if (is.null(na.rm)) {
na.rm <- TRUE
}
if (is.null(overwrite)) {
overwrite <- TRUE
}
if (is.null(on_0_1000)) {
on_0_1000 <- FALSE
}
# additional arg retrieved for EMci
sd_prediction <- args$sd_prediction
mean_prediction <- args$mean_prediction
side <- args$side
# additional arg retrived for EMca
thresh <- args$thresh
# additional arg retrived for EMwmean
penalization_scores <- args$penalization_scores
mod.name <- args$mod.name
if (data_as_formal_predictions) {
newdata <- subset(newdata, object@model)
} else {
newdata <- .get_formal_predictions(object, newdata, on_0_1000 = on_0_1000, seedval = seedval)
}
out <- predfun(newdata,
on_0_1000 = on_0_1000,
mean_prediction = mean_prediction,
sd_prediction = sd_prediction,
side = side,
thresh = thresh,
penalization_scores = penalization_scores,
mod.name = mod.name,
na.rm = na.rm)
if (!is.null(out) & !is.null(filename)) {
cat("\n\t\tWriting projection on hard drive...")
if (on_0_1000 & !inherits(object, "EMcv_biomod2_model")) { ## projections are stored as positive integer
writeRaster(out, filename = filename, overwrite = overwrite,
datatype = "INT2S", NAflag = -9999)
} else { ## keep default data format for saved raster
writeRaster(out, filename = filename, overwrite = overwrite)
}
out <- rast(filename)
}
return(out)
})
### biomod2_ensemble_model + data.frame -------------------------------------
##' @rdname predict2.em
setMethod('predict2', signature(object = 'biomod2_ensemble_model', newdata = "data.frame"),
function(object, newdata, predfun, seedval = NULL, ...) {
args <- list(...)
data_as_formal_predictions <- args$data_as_formal_predictions
if (is.null(data_as_formal_predictions)) {
data_as_formal_predictions <- FALSE
}
on_0_1000 <- args$on_0_1000
if (is.null(on_0_1000)) {
on_0_1000 <- FALSE
}
na.rm <- args$na.rm
if (is.null(na.rm)) {
na.rm <- TRUE
}
# additional arg retrieved for EMci
sd_prediction <- args$sd_prediction
mean_prediction <- args$mean_prediction
side <- args$side
# additional arg retrived for EMca
thresh <- args$thresh
# additional arg retrived for EMwmean
penalization_scores <- args$penalization_scores
if (data_as_formal_predictions) {
newdata <- newdata[ , object@model, drop = FALSE]
} else {
newdata <- .get_formal_predictions(object, newdata, on_0_1000 = on_0_1000, seedval = seedval)
}
out <- predfun(newdata,
on_0_1000 = on_0_1000,
mean_prediction = mean_prediction,
sd_prediction = sd_prediction,
side = side,
thresh = thresh,
penalization_scores = penalization_scores,
na.rm = na.rm)
return(out)
})
# --------------------------------------------------------------------------- #
# 10.1 biomod2_ensemble_model subclass ---------------------------------------
# ---------------------------------------------------------------------------- #
### -------------------------------------------------------------------------- #
### 10.1 EMmean_biomod2_model ------------------------------------------------
### -------------------------------------------------------------------------- #
##' @name EMmean_biomod2_model-class
##' @rdname biomod2_ensemble_model
##' @export
setClass('EMmean_biomod2_model',
representation(),
contains = 'biomod2_ensemble_model',
prototype = list(model_class = 'EMmean'),
validity = function(object) { return(TRUE) })
##'
##' @rdname predict2.em
##'
setMethod('predict2', signature(object = 'EMmean_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, ...) {
predfun <- function(newdata, on_0_1000, mod.name, na.rm, ...){
if (nlyr(newdata) == 1) {
return(newdata)
} else {
return(
app(newdata,function(x){
m <- mean(x, na.rm = na.rm)
if (on_0_1000) {
m <- round(m)
}
return(m)
}, wopt = list(names = mod.name))
)
}
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, ...)
}
)
##' @rdname predict2.em
setMethod('predict2', signature(object = 'EMmean_biomod2_model', newdata = "data.frame"),
function(object, newdata, ...) {
predfun <- function(newdata, on_0_1000, na.rm, ...){
out <- rowMeans(newdata, na.rm = na.rm)
if (on_0_1000) {
out <- round(out)
}
out
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, ...)
}
)
### -------------------------------------------------------------------------- #
### 10.2 EMmedian_biomod2_model ----------------------------------------------
### -------------------------------------------------------------------------- #
##' @name EMmedian_biomod2_model-class
##' @rdname biomod2_ensemble_model
##' @export
setClass('EMmedian_biomod2_model',
representation(),
contains = 'biomod2_ensemble_model',
prototype = list(model_class = 'EMmedian'),
validity = function(object) { return(TRUE) })
##'
##' @rdname predict2.em
##'
setMethod('predict2', signature(object = 'EMmedian_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, ...) {
predfun <- function(newdata, on_0_1000, mod.name, na.rm, ...){
if (nlyr(newdata) == 1) {
return(newdata)
} else {
return(
app(newdata,function(x){
m <- median(x, na.rm = na.rm)
if (on_0_1000) {
m <- round(m)
}
return(m)
}, wopt = list(names = mod.name))
)
}
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, ...)
}
)
##' @rdname predict2.em
setMethod('predict2', signature(object = 'EMmedian_biomod2_model', newdata = "data.frame"),
function(object, newdata, ...) {
predfun <- function(newdata, on_0_1000, na.rm, ...){
out <- apply(newdata, 1, median, na.rm = na.rm)
if (on_0_1000) {
out <- round(out)
}
out
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, ...)
}
)
### -------------------------------------------------------------------------- #
### 10.3 EMcv_biomod2_model --------------------------------------------------
### -------------------------------------------------------------------------- #
##' @name EMcv_biomod2_model-class
##' @rdname biomod2_ensemble_model
##' @export
setClass('EMcv_biomod2_model',
representation(),
contains = 'biomod2_ensemble_model',
prototype = list(model_class = 'EMcv'),
validity = function(object) { return(TRUE) })
##'
##' @rdname predict2.em
##'
setMethod('predict2', signature(object = 'EMcv_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, ...) {
predfun <- function(newdata, on_0_1000, mod.name, na.rm, ...){
if (nlyr(newdata) <= 1) {
stop(paste0("\n Model EMcv was not computed because only one single model was kept in ensemble modeling (", names(newdata), ")"))
}
out <- app(newdata, function(x){
sd(x, na.rm = na.rm)/mean(x, na.rm = na.rm) * 100
}, wopt = list(names = mod.name))
return(out)
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, ...)
}
)
##' @rdname predict2.em
setMethod('predict2', signature(object = 'EMcv_biomod2_model', newdata = "data.frame"),
function(object, newdata, ...) {
predfun <- function(newdata, na.rm, ...){
if (ncol(newdata) <= 1) {
stop(paste0("\n Model EMcv was not computed because only one single model was kept in ensemble modeling ("
, colnames(newdata), ")"))
}
out <- apply(newdata, 1,
function(x) {
ifelse(length(x) == 1, 0,
sd(x, na.rm = na.rm)/mean(x, na.rm = na.rm)*100)
})
return(out)
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, ...)
}
)
### -------------------------------------------------------------------------- #
### 10.4 EMci_biomod2_model --------------------------------------------------
### -------------------------------------------------------------------------- #
##' @name EMci_biomod2_model-class
##' @rdname biomod2_ensemble_model
##' @export
setClass('EMci_biomod2_model',
representation(alpha = 'numeric', side = 'character'),
contains = 'biomod2_ensemble_model',
prototype = list(model_class = 'EMci', alpha = 0.05, side = 'superior'),
validity = function(object) {
if (!(object@side %in% c('inferior','superior'))) {
stop("side arg should be 'inferior' or 'superior")
} else {
return(TRUE)
}
})
##'
##' @rdname predict2.em
##'
setMethod('predict2', signature(object = 'EMci_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, ...) {
predfun <- function(newdata, on_0_1000, mod.name, na.rm, ...){
args <- list(...)
mean_prediction <- args$mean_prediction
sd_prediction <- args$sd_prediction
side <- args$side
if (is.null(mean_prediction)) {
mean_prediction <- app(newdata, mean, wopt = list(names = mod.name),
na.rm = na.rm)
}
if (is.null(sd_prediction)) {
sd_prediction <- app(newdata, sd, wopt = list(names = mod.name),
na.rm = na.rm)
}
ci_prediction <- switch(
side,
inferior = mean_prediction - (sd_prediction * (qt((1-object@alpha/2), df = length(object@model) + 1 ) / sqrt(length(object@model))) ),
superior = mean_prediction + (sd_prediction * (qt((1-object@alpha/2), df = length(object@model) + 1 ) / sqrt(length(object@model))) )
)
if (on_0_1000) {
ci_prediction <- classify(round(ci_prediction),
matrix(c(-Inf, 0, 0,
1000, Inf, 1000),
nrow = 2, byrow = TRUE))
} else {
ci_prediction <- classify(round(ci_prediction),
matrix(c(-Inf, 0, 0,
1, Inf, 1),
nrow = 2, byrow = TRUE))
}
ci_prediction
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, side = object@side, ...)
}
)
##' @rdname predict2.em
setMethod('predict2', signature(object = 'EMci_biomod2_model', newdata = "data.frame"),
function(object, newdata, ...) {
predfun <- function(newdata, na.rm, ...){
args <- list(...)
mean_prediction <- args$mean_prediction
sd_prediction <- args$sd_prediction
side <- args$side
on_0_1000 <- args$on_0_1000
if (is.null(mean_prediction)) {
mean_prediction <- rowMeans(newdata, na.rm = na.rm)
}
if (is.null(sd_prediction)) {
sd_prediction <- apply(newdata, 1, sd, na.rm = na.rm)
}
ci_prediction <- switch(side,
inferior = mean_prediction - (sd_prediction * (qt((1-object@alpha/2), df = length(object@model) + 1 ) / sqrt(length(object@model))) ),
superior = mean_prediction + (sd_prediction * (qt((1-object@alpha/2), df = length(object@model) + 1 ) / sqrt(length(object@model))) ))
# reclassify prediction to prevent from out of bounds prediction
if (on_0_1000) {
ci_prediction <- round(ci_prediction * 1000)
ci_prediction[ci_prediction > 1000] <- 1000
ci_prediction[ci_prediction < 0] <- 0
} else {
ci_prediction[ci_prediction > 1] <- 1
ci_prediction[ci_prediction < 0] <- 0
}
ci_prediction
}
# redirect to predict2.biomod2_ensemble_model.data.frame
callNextMethod(object, newdata, predfun = predfun, side = object@side, ...)
}
)
### -------------------------------------------------------------------------- #
### 10.5 EMca_biomod2_model --------------------------------------------------
### -------------------------------------------------------------------------- #
##' @name EMca_biomod2_model-class
##' @rdname biomod2_ensemble_model
##' @export
setClass('EMca_biomod2_model',
representation(thresholds = 'numeric'),
contains = 'biomod2_ensemble_model',
prototype = list(model_class = 'EMca'),
validity = function(object) { return(TRUE) })
##'
##' @rdname predict2.em
##'
setMethod('predict2', signature(object = 'EMca_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, data_as_formal_predictions = FALSE, ...) {
args <- list(...)
on_0_1000 <- args$on_0_1000
if (is.null(on_0_1000)) {
on_0_1000 <- FALSE
}
predfun <- function(newdata, on_0_1000, thresh, mod.name, na.rm, ...){
if (nlyr(newdata) == 1) {
return(bm_BinaryTransformation(newdata, thresh))
} else {
return(
app(bm_BinaryTransformation(newdata, thresh),
function(x){
m <- mean(x, na.rm = na.rm)
if (on_0_1000) {
m <- round(m * 1000)
}
return(m)
}, wopt = list(names = mod.name))
)
}
}
if (on_0_1000) {
thresh <- object@thresholds
} else {
thresh <- object@thresholds / 1000
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata, predfun = predfun, thresh = thresh,
data_as_formal_predictions = data_as_formal_predictions,
...)
}
)
##' @rdname predict2.em
setMethod('predict2', signature(object = 'EMca_biomod2_model', newdata = "data.frame"),
function(object, newdata, data_as_formal_predictions = FALSE, ...) {
args <- list(...)
on_0_1000 <- args$on_0_1000
if (is.null(on_0_1000)) {
on_0_1000 <- FALSE
}
predfun <- function(newdata, na.rm, ...){
out <- rowMeans(bm_BinaryTransformation(newdata, thresh),
na.rm = na.rm)
if (on_0_1000) {
out <- round(out * 1000)
}
out
}
if (on_0_1000) {
thresh <- object@thresholds
} else {
thresh <- object@thresholds / 1000
}
# redirect to predict2.biomod2_ensemble_model.data.frame
callNextMethod(object, newdata, predfun = predfun,
data_as_formal_predictions = data_as_formal_predictions,
...)
}
)
### -------------------------------------------------------------------------- #
### 10.6 EMwmean_biomod2_model -----------------------------------------------
### -------------------------------------------------------------------------- #
##' @name EMwmean_biomod2_model-class
##' @rdname biomod2_ensemble_model
##' @export
setClass('EMwmean_biomod2_model',
representation(penalization_scores='numeric'),
contains = 'biomod2_ensemble_model',
prototype = list(model_class = 'EMwmean'),
validity = function(object) { return(TRUE) })
##'
##' @rdname predict2.em
##'
setMethod('predict2', signature(object = 'EMwmean_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, data_as_formal_predictions = FALSE, ...) {
if (ncol(newdata) < 1) {
stop("Model EMwmean was not computed because no single model was kept in ensemble modeling")
}
predfun <- function(newdata, on_0_1000, penalization_scores,
mod.name, ...){
if (nlyr(newdata) == 1) {
return(newdata)
} else {
return(
app(newdata, function(x) {
wm <- sum(x * penalization_scores)
if (on_0_1000) {
wm <- round(wm)
}
return(wm)
}, wopt = list(names = mod.name))
)
}
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata,
predfun = predfun,
data_as_formal_predictions = data_as_formal_predictions,
penalization_scores = object@penalization_scores, ...)
}
)
##' @rdname predict2.em
setMethod('predict2', signature(object = 'EMwmean_biomod2_model', newdata = "data.frame"),
function(object, newdata, data_as_formal_predictions = FALSE, ...) {
if (ncol(newdata) < 1) {
stop("Model EMwmean was not computed because no single model was kept in ensemble modeling")
}
predfun <- function(newdata, on_0_1000, penalization_scores, ...){
out <- as.vector(
as.matrix(newdata) %*% penalization_scores
)
if (on_0_1000) {
out <- round(out)
}
out
}
# redirect to predict2.biomod2_ensemble_model.SpatRaster
callNextMethod(object, newdata,
predfun = predfun,
penalization_scores = object@penalization_scores,
data_as_formal_predictions = data_as_formal_predictions,
...)
}
)