Skip to content

Commit 7213fa0

Browse files
committed
Small 🐛 fixes to ensemble learner and predictors
1 parent e41e471 commit 7213fa0

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Small bug fixed related to manual provision of scenario thresholds.
1212
* `predictor_filter()` now also accepts [`SpatRaster`] objects as inputs.
1313
* Small fix so that bounding box extent is correctly printed #138
14-
* Small bug fix to `engine_gdb` in dev branch.
14+
* Small :bug: fixes to `engine_gdb` in dev branch.
1515

1616
# ibis.iSDM 0.1.5
1717

R/ensemble.R

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,27 @@ methods::setMethod(
252252
# Ensure that predictions have unique names
253253
names(ras) <- paste0('model', 1:terra::nlyr(ras))
254254
ex <- terra::extract(ras, point, ID = FALSE)
255-
ex <- cbind(point[,field_occurrence], ex)
256-
fit <- stats::glm(
257-
formula = paste(field_occurrence, "~", paste0(names(ras), collapse = ' + ')) |> stats::as.formula(),
258-
family = stats::binomial(),data = ex
259-
)
255+
ex <- cbind(point[,field_occurrence] |> sf::st_drop_geometry(), ex)
256+
vars <- names(ras)
257+
258+
# If any variable is fully NAs, remove
259+
check <- apply(ex, 2, function(z) all(is.na(z)))
260+
if(any(check)){
261+
cli::cli_alert_warning(paste("Removing ", names(which(check)), "because there are only NA values..."))
262+
ex <- ex[,which(!check)]
263+
vars <- vars[vars %in% names(which(!check))]
264+
}
265+
fit <- try({
266+
stats::glm(
267+
formula = paste(field_occurrence, "~", paste0(vars, collapse = ' + ')) |> stats::as.formula(),
268+
family = stats::binomial(),data = ex)
269+
},silent = TRUE)
270+
if(inherits(fit, 'try-error')) cli::cli_abort('SuperlearModel did not fit...', fit)
260271
# Now predict output with the meta-learner
261-
new <- emptyraster(ras)
262-
new[which(!is.na(ras[[1]])[])] <- terra::predict(
263-
fit, ras, na.rm = FALSE, type = "response",
272+
new <- terra::predict(
273+
object = ras, model = fit, na.rm = FALSE, type = "response",
264274
cores = getOption('ibis.nthread'))
275+
names(new) <- paste0('superlearner_lyr')
265276
attr(new, "superlearner.coefficients") <- stats::coef(fit)
266277
try({ rm(ex,fit) },silent = TRUE)
267278
}

R/utils-predictors.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ predictor_check <- function(env){
11981198

11991199
# Check NaN
12001200
check_nan <- apply(env, 2, function(z) all(is.nan(z)))
1201+
if(any(is.na(check_nan))) check_nan[is.na(check_nan)] <- FALSE
12011202
if(any(check_nan)){
12021203
if(getOption('ibis.setupmessages', default = TRUE)) {
12031204
myLog('[Setup]','yellow', 'Excluded ', paste0(names(which(check_nan)),collapse = "; "),
@@ -1218,6 +1219,7 @@ predictor_check <- function(env){
12181219

12191220
# Check variance
12201221
check_var <- apply(env, 2, function(z) stats::var(z, na.rm = TRUE)) == 0
1222+
if(any(is.na(check_var))) check_var[is.na(check_var)] <- FALSE
12211223
if(any(check_var)){
12221224
if(getOption('ibis.setupmessages', default = TRUE)) {
12231225
myLog('[Setup]','yellow', 'Excluded ', paste0(names(which(check_var)),collapse = "; "),

R/utils.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ thresholdval <- function(x, knot, sense = "gte") {
322322
#'
323323
#' @noRd
324324
scale_weight <- function(v, method = "scale"){
325-
if(is.null(v)) return(1) # Return dummy 1 for equal weight
325+
if(is.null(v)) return(NULL) # Return dummy 1 for equal weight
326+
if(length(v)==0) return(NULL)
326327
assertthat::assert_that(
327328
length(v)>1,
328329
is.character(method)

tests/testthat/test_modelFits.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ test_that('Add further tests for model fits', {
118118
expect_s4_class(o, "SpatRaster")
119119
expect_length(names(o), 2) # Should be at maximum 2 layers
120120

121+
122+
121123
# ----------- #
122124
# Partial stuff
123125
skip_if_not_installed("pdp")

0 commit comments

Comments
 (0)