Skip to content

Commit 534d69b

Browse files
committed
Fix issue with predictor NA cells for each engine due to predictor integration loop
1 parent b4cf9bd commit 534d69b

File tree

9 files changed

+25
-8
lines changed

9 files changed

+25
-8
lines changed

R/engine_bart.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ engine_bart <- function(x,
9595
# Burn in the background
9696
template <- terra::rasterize(x$background, template, field = 0)
9797

98+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
99+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
100+
98101
# Set up dbarts control with some parameters, rest default
99102
dc <- dbarts::dbartsControl(keepTrees = TRUE, # Keep trees
100103
n.burn = nburn,

R/engine_breg.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ engine_breg <- function(x,
8181
# Burn in the background
8282
template <- terra::rasterize(x$background, template, field = 0)
8383

84+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
85+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
86+
8487
# Set up the parameter list
8588
params <- list(
8689
iter = iter,

R/engine_gdb.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ engine_gdb <- function(x,
103103
# Burn in the background
104104
template <- terra::rasterize(background, template, field = 0)
105105

106+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
107+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
108+
106109
# Set up boosting control
107110
bc <- mboost::boost_control(mstop = iter,
108111
nu = learning_rate,

R/engine_glm.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ engine_glm <- function(x,
9090
# Burn in the background
9191
template <- terra::rasterize(x$background, template, field = 0)
9292

93+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
94+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
95+
9396
# Specify default control
9497
if(is.null(control)){
9598
control <- stats::glm.control()

R/engine_glmnet.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ engine_glmnet <- function(x,
114114
# Burn in the background
115115
template <- terra::rasterize(x$background, template, field = 0)
116116

117+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
118+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
119+
117120
# Set up the parameter list
118121
params <- list(
119122
alpha = alpha,

R/engine_scampr.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ engine_scampr <- function(x,
9898
# Burn in the background
9999
template <- terra::rasterize(x$background, template, field = 0)
100100

101+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
102+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
103+
101104
# Set up the parameter list
102105
params <- list(
103106
type = type,

R/engine_stan.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ engine_stan <- function(x,
113113
# If predictor existing, use them
114114
template <- emptyraster(x$predictors$get_data() )
115115
}
116+
116117
# Burn in the background
117118
template <- terra::rasterize(x$background, template, field = 0)
118119

120+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
121+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
122+
119123
# Define new engine object of class
120124
eg <- Engine
121125

R/engine_xgboost.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ engine_xgboost <- function(x,
115115
# Burn in the background
116116
template <- terra::rasterize(x$background, template, field = 0)
117117

118+
# mask template where all predictor layers are NA; change na.rm = FALSE for comeplete.cases
119+
if (!is.Waiver(x$predictors)) template <- terra::mask(template, sum(x$predictors$get_data(), na.rm = TRUE))
120+
118121
# Set up the parameter list
119122
params <- list(
120123
booster = booster,

R/train.R

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,14 +1820,6 @@ methods::setMethod(
18201820
}
18211821
out$settings$set("has_limits", TRUE)
18221822
} else {
1823-
if(settings$get('inference_only')==FALSE){
1824-
# set all NAs in prediction to NAs according in predictors
1825-
o <- tryCatch(expr = {terra::mask(out$get_data("prediction"), out$model$predictors_object$get_data()[[1]],
1826-
updatevalue = NA)}, error = function(e) NA)
1827-
if (inherits(x = o, what = "SpatRaster")) {
1828-
if(terra::hasValues(o)) {out <- out$set_data("prediction", o)} else {warning("Issue while masking prediction")}
1829-
} else {warning("Issue while fetching predictors object")}
1830-
}
18311823
out$settings$set("has_limits", FALSE)
18321824
}
18331825

0 commit comments

Comments
 (0)