5555# ' @param confidence Numeric. The confidence value for LDC methods. See details below..
5656# ' @param lambda Numeric. The lambda param for LDC Cauchy weighting...
5757# ' @param exclude_slef Logical. To exclude a benchmark point from assessing itself.
58+ # ' @param drop_features Integer vector. Completely remove the RS variable from the benchmarking process. For
59+ # ' consistency, it is recommended to exclude the same variables used in the histogram step; unless
60+ # ' you have a specific reason not to.
5861# ' @param make_su Logical. To make the uncertainty map or not.
5962# ' @param num_threads Integer. Specifies the number of CPU threads to be used for processing. A value
6063# ' below 1 indicates that all available threads will be utilized. Refer to the details section for
@@ -90,68 +93,67 @@ benchmark <- function(
9093 confidence = 0.5 ,
9194 lambda = 2.0 ,
9295 exclude_slef = TRUE ,
96+ drop_features = NULL ,
9397 make_su = FALSE ,
9498 num_threads = - 1 ,
9599 ... ) {
96100
101+ # check k_pred and k_obs
102+ if (k_pred < k_obs ) stop(" 'k_obs' must be smaller or equal to 'k_pred'." )
97103 # check samples and histograms
98- if (.is_mat(samples )) {
99- samples <- .check_mat(samples )
100- } else {
101- stop(" 'samples' must be a matrix or an object convertibe to matrix" )
102- }
103- if (.is_mat(histogram )) {
104- histogram <- .check_mat(histogram )
105- } else {
106- stop(" 'histogram' must be a matrix or an object convertibe to matrix" )
107- }
108-
109- if (nrow(histogram ) != ncol(histogram )) {
110- warning(" Historgram dimensions are not the same!\n " )
111- }
112- # cat("Histogram dimension:", dim(histogram), "\n")
104+ samples <- if (.is_mat(samples )) .check_mat(samples ) else stop(" 'samples' must be a matrix or convertible to one." )
105+ histogram <- if (.is_mat(histogram )) .check_mat(histogram ) else stop(" 'histogram' must be a matrix or convertible to one." )
106+ if (nrow(histogram ) != ncol(histogram )) warning(" Historgram dimensions are not the same!\n " )
113107
114108 if (methods :: is(histogram , " histo" )) {
115109 # check for histo bin_width consistency
116110 if (is.null(bin_width )) {
117111 bin_width <- attributes(histogram )$ bin.width
118112 } else {
119113 if (bin_width != attributes(histogram )$ bin.width ) {
120- warning(" The supplied 'bin_width` is different from the arrtibute( histogram)$bin.width from the input ." )
114+ warning(" Provided 'bin_width' differs from histogram attribute ." )
121115 }
122116 }
123117 # check for histo offset consistency
124118 if (is.null(offset )) {
125119 offset <- attributes(histogram )$ offset
126120 } else {
127121 if (offset != attributes(histogram )$ offset ) {
128- warning(" The supplied 'offset` is different from the arrtibute( histogram)$offset from the input ." )
122+ warning(" Provided 'offset' differs from histogram attribute ." )
129123 }
130124 }
131125 }
132126
133127 # interpolate histogram
134128 if (interpolate ) {
135129 histogram <- terra :: as.matrix(
136- x = terra :: disagg(
137- x = terra :: rast(histogram ),
138- fact = 2 ,
139- method = " bilinear"
140- ),
130+ terra :: disagg(terra :: rast(histogram ), fact = 2 , method = " bilinear" ),
141131 wide = TRUE
142132 )
143133 }
144134 # get the bin number after interpolation
145135 bin_num <- min(dim(histogram ))
146136
147-
137+ exclude_var <- NULL
138+ # drop features from calculation if requested
139+ if (length(drop_features )) {
140+ n_vars <- (ncol(samples ) - 2 ) / 2
141+ exclude_var <- c(drop_features + 2 , drop_features + 2 + n_vars )
142+ }
148143
149144 if (.is_mat(data )) {
150145 # check and convert to matrix
151146 data <- .check_mat(data )
152147 # correction scale for long-lat CRS
153148 correction <- ifelse(.is_lonlat(data ), scale_factor , 1 )
154149
150+ if (ncol(samples ) != ncol(data )) {
151+ stop(" Samples must include all raster values (matching column count with 'data')." )
152+ }
153+
154+ # remove the features from the reference samples as well
155+ if (! is.null(exclude_var )) samples [, exclude_var ] <- 0
156+
155157 tryCatch(
156158 {
157159 output <- benchmarking(
@@ -172,6 +174,7 @@ benchmark <- function(
172174 confidence = confidence ,
173175 lambda = lambda ,
174176 exclude_slef = exclude_slef ,
177+ drop = exclude_var ,
175178 make_su = make_su ,
176179 num_threads = num_threads
177180 )
@@ -192,16 +195,14 @@ benchmark <- function(
192195 # sample extraction if needed
193196 if (ncol(samples ) == 2 ) {
194197 cat(" Extracting sample values...\n " )
195- # add xy to the stack and extract
196- samples <- cbind(
197- samples ,
198- as.matrix(terra :: extract(x = data , y = samples , ID = FALSE ))
199- )
200- } else if ((ncol(samples ) - 2 ) == terra :: nlyr(data )) {
201- cat(" Samples with raster values are provided!\n " )
202- } else {
203- # this should include rows/cols columns as well
204- stop(" Samples must either be coordinate values exclusively or include all raster layer values." )
198+ samples <- cbind(samples , as.matrix(terra :: extract(data , samples , ID = FALSE )))
199+ } else if ((ncol(samples ) - 2 ) != terra :: nlyr(data )) {
200+ stop(" Sample feature count does not match number of raster layers." )
201+ }
202+
203+ # remove the features from the reference samples as well
204+ if (length(drop_features )) {
205+ samples [, exclude_var ] <- 0
205206 }
206207
207208 tryCatch(
@@ -225,6 +226,7 @@ benchmark <- function(
225226 confidence = confidence ,
226227 lambda = lambda ,
227228 exclude_slef = exclude_slef ,
229+ drop = exclude_var ,
228230 make_su = make_su ,
229231 num_threads = num_threads ,
230232 ...
@@ -248,7 +250,7 @@ benchmark <- function(
248250# the generic HCAS prediction function for C++ integration with terra
249251# NOTE: the na.rm arg in terra::predict doesn't provide the correct mask
250252# and keep model an empty list
251- benchmarking <- function (model , newdata , make_su , ... ){
253+ benchmarking <- function (model , newdata , make_su , drop = NULL , ... ){
252254 # check for NAs
253255 has_na <- anyNA(newdata )
254256 # number of output columns; TRUE/FALSE + 1
@@ -268,6 +270,9 @@ benchmarking <- function(model, newdata, make_su, ...){
268270 dat <- as.matrix(newdata )
269271 }
270272
273+ # if drop_features are provided exclude them from features
274+ if (length(drop )) dat [, drop ] <- 0
275+
271276 tryCatch(
272277 {
273278 # the HCAS C++ function
@@ -278,7 +283,7 @@ benchmarking <- function(model, newdata, make_su, ...){
278283 )
279284 },
280285 error = function (cond ) {
281- message(" Error: the benchmarking C++ function faild, returning -0.02! " )
286+ message(" Benchmarking C++ function failed. Returning -0.02 for all cells. " )
282287 # return error values -0.02
283288 return (
284289 matrix (- 0.02 , nrow = nr , ncol = nc )
0 commit comments