forked from marlonecobos/kuenm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindependent_evaluation.R
More file actions
398 lines (353 loc) · 15.7 KB
/
independent_evaluation.R
File metadata and controls
398 lines (353 loc) · 15.7 KB
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
#' Evaluate models with independent data
#'
#' @description
#' This function evaluates the selected models using independent data (i.e.,
#' data not used during model calibration). The function computes omission rate
#' and pROC, and optionally assesses whether the environmental conditions in the
#' independent data are analogous (i.e., within the range) to those in the
#' calibration data.
#'
#' @usage independent_evaluation(fitted_models, new_data,
#' consensus = c("mean", "median"),
#' type = "cloglog", extrapolation_type = "E",
#' var_to_restrict = NULL, perform_mop = TRUE,
#' mop_type = "detailed",
#' calculate_distance = TRUE,
#' where_distance = "all",
#' return_predictions = TRUE,
#' return_binary = TRUE,
#' progress_bar = FALSE, ...)
#'
#' @param fitted_models an object of class `fitted_models` returned by the
#' \code{\link{fit_selected}}() function.
#' @param new_data a `data.frame` containing environmental variables for
#' independent test records. The column names must correspond exactly to the
#' environmental variables used to fit the selected models, and each row to an
#' individual test record.
#' @param consensus (character) vector specifying the types of consensus to
#' use. Available options are `"median"` and `"mean"`. Default is
#' `c("median", "mean")`.
#' @param type (character) the format of prediction values. For `maxnet` models,
#' valid options are `"raw"`, `"cumulative"`, `"logistic"`, and `"cloglog"`. For
#' `glm` models, valid options are `"response"` and `"cloglog"`. Default is
#' `"cloglog"`.
#' @param extrapolation_type (character) extrapolation type of model. Models can
#' be transferred with three options: free extrapolation ('E'), extrapolation
#' with clamping ('EC'), and no extrapolation ('NE'). Default = 'E'. See details.
#' @param var_to_restrict (character) vector specifying which variables to clamp or
#' not extrapolate. Only applicable if extrapolation_type is "EC" or "NE".
#' Default is `NULL`, meaning all variables will be clamped or not extrapolated.
#' @param perform_mop (logical) whether to execute a Mobility-Oriented Parity
#' (MOP) analysis. This analysis assesses if the environmental conditions in the
#' `new_data` are analogous (within ranges) to those in the calibration data.
#' Defaults to `TRUE`.
#' @param mop_type (character) type of MOP analysis to be performed. Options
#' available are "basic", "simple" and "detailed". Default is 'simples'. See
#' \code{\link{projection_mop}}() for more details.
#' @param calculate_distance (logical) whether to calculate distances
#' (dissimilarities) between new_data and calibration data. Default is TRUE.
#' @param where_distance (character) specifies which values in `new_data` should
#' be used to calculate distances. Options are: "in_range" (only conditions
#' within the calibration range), "out_range" (only conditions outside the
#' calibration range), and "all" (all conditions). Default is "all".
#' @param return_predictions (logical) whether to return continuous predictions
#' at the locations of independent records in `new_data`. Default is TRUE.
#' @param return_binary (logical) whether to return binary predictions
#' at the locations of independent records in `new_data`. The predictions are
#' binarized using the respective thresholds stores in `fitted_models`. Default
#' is TRUE.
#' @param progress_bar (logical) whether to display a progress bar during
#' mop processing. Default is FALSE.
#' @param ... additional arguments passed to \code{\link[mop]{mop}()}.
#'
#' @importFrom mop mop
#' @importFrom fpROC auc_metrics
#' @importFrom stats setNames
#' @return
#' A list containing the following elements:
#'
#' - **evaluation**: A `data.frame` with omission rate and pROC values for each
#' selected model and for the consensus.
#' - **mop_results**: (Only if `perform_mop = TRUE`) An object of class
#' `mop_results`, with metrics of environmental similarity between calibration
#' and independent data.
#' - **predictions**: (Only if `return_predictions = TRUE`) A `list` of
#' `data.frames` containing continuous and binary predictions at the independent
#' record locations, along with MOP distances, an indicator of whether
#' environmental conditions at each location fall within the calibration range,
#' and the identity of the variables that have lower and higher values than the
#' calibration range. If the `fitted_models` object includes categorical
#' variables, the returned `data.frame` will also contain columns indicating
#' which values in `new_data` were not present in the calibration data.
#' @examples
#' # Example with maxnet
#' # Import example of fitted_models (output of fit_selected())
#' data("fitted_model_maxnet", package = "kuenm2")
#'
#' # Import independent records to evaluate the models
#' data("new_occ", package = "kuenm2")
#'
#' # Import raster layers
#' var <- terra::rast(system.file("extdata", "Current_variables.tif",
#' package = "kuenm2"))
#'
#' #Extract variables to occurrences
#' new_data <- extract_occurrence_variables(occ = new_occ, x = "x", y = "y",
#' raster_variables = var)
#'
#' #Add some fake data beyond the limits of calibration ranges
#' fake_data <- data.frame("pr_bg" = c(1, 1, 1),
#' "x" = c(NA, NA, NA),
#' "y" = c(NA, NA, NA),
#' "bio_1" = c(10, 15, 23),
#' "bio_7" = c(12, 16, 20),
#' "bio_12" = c(2300, 2000, 1000),
#' "bio_15" = c(30, 40, 50),
#' "SoilType" = c(1, 1, 1))
#' new_data <- rbind(new_data, fake_data)
#'
#'
#' # Evaluate models with independent data
#' res_ind <- independent_evaluation(fitted_models = fitted_model_maxnet,
#' new_data = new_data)
#'
#' @export
independent_evaluation <- function(fitted_models, new_data,
consensus = c("mean", "median"),
type = "cloglog",
extrapolation_type = "E",
var_to_restrict = NULL,
perform_mop = TRUE,
mop_type = "detailed",
calculate_distance = TRUE,
where_distance = "all",
return_predictions = TRUE,
return_binary = TRUE,
progress_bar = FALSE,
...){
#### Check data ####
if (missing(fitted_models)) {
stop("Argument 'fitted_models' must be defined.")
}
if (!inherits(fitted_models, "fitted_models")) {
stop("Argument 'fitted_models' must be a 'fitted_models' object.")
}
if (missing(new_data)) {
stop("Argument 'new_data' must be defined.")
}
if (!inherits(new_data, "data.frame")) {
stop("Argument 'new_data' must be a 'data.frame' object.")
}
if (!inherits(consensus, "character")) {
stop("Argument 'consensus' must be a 'character'.")
}
consensus_out <- setdiff(consensus, c("median", "mean"))
if (length(consensus_out) > 0) {
stop("Invalid 'consensus' provided.",
"\nAvailable options are 'median' and 'mean'.")
}
if(fitted_models$algorithm == "maxnet"){
if (!any(c("raw", "cumulative", "logistic", "cloglog") %in% type)) {
stop("Invalid 'type' provided.",
"\nAvailable options for maxnet fitted_models are: 'raw', 'cumulative',
'logistic', or 'cloglog'.")
}
if(type == "raw")
type <- "exponential"
}
if(fitted_models$algorithm == "glm"){
if (!any(c("response", "cloglog") %in% type)) {
stop("Invalid 'type' provided.",
"\nAvailable options for glm fitted_models are 'response' or 'cloglog'.")
}
if(type == "cloglog")
type = "link"
}
if(length(extrapolation_type) > 1){
stop("Extrapolation type accepts only one of these values: 'E', 'EC', or
'NE'")
}
extrapolation_out <- setdiff(extrapolation_type, c("E", "EC", "NE"))
if (length(extrapolation_out) > 0) {
stop("Invalid 'extrapolation type' provided.",
"\nAvailable options are: 'E', 'EC', and 'NE'.")
}
if (extrapolation_type %in% c("E", "EC") & !is.null(var_to_restrict) &
!inherits(var_to_restrict, "character")) {
stop("Argument 'var_to_restrict' must be NULL or 'character'.")
}
if(!inherits(perform_mop, "logical")){
stop("Argument 'perform_mop' must be 'logical'")
}
if (!inherits(mop_type, "character")) {
stop("Argument 'mop_type' must be a 'character'.")
}
mop_type_out <- setdiff(mop_type, c("basic", "simple", "detailed"))
if (length(mop_type_out) > 0) {
stop("Invalid 'mop_type' provided.",
"\nAvailable options are: 'basic', 'simple', or 'detailed'.")
}
if(!inherits(calculate_distance, "logical")){
stop("Argument 'calculate_distance' must be 'logical'.")
}
distance_out <- setdiff(where_distance, c("in_range", "out_range", "all"))
if (length(distance_out) > 0) {
stop("Invalid 'where_distance' provided.",
"\nAvailable options are: 'in_range', 'out_range', and 'all'.")
}
if(!inherits(return_predictions, "logical")){
stop("Argument 'return_predictions' must be 'logical'.")
}
if(!inherits(return_binary, "logical")){
stop("Argument 'return_binary' must be 'logical'.")
}
if(!inherits(progress_bar, "logical")){
stop("Argument 'progress_bar' must be 'logical'.")
}
#Check variables
v <- unique(unlist(sapply(fitted_models$Models, function(x)
names(x$Full_model$varmax)[-1],
simplify = F)))
v <- gsub("I\\((.*?)\\^2\\)", "\\1", v) #Remove quadratic pattern
v <- v[!grepl("categorical", v)] #Remove categorical pattern
v <- unique(unlist(strsplit(v, ":"))) #Remove product pattern
#Check variables absent from new_data
v_out <- setdiff(v, colnames(new_data))
if(length(v_out) > 0){
stop("The following variables used to fit the models are absent from the 'new_data:\n'", paste(v_out, collapse = "; "))
}
#Predict to independent records
pred_test <- predict_selected(models = fitted_models,
new_variables = new_data[,v, drop = FALSE],
consensus = consensus,
extrapolation_type = extrapolation_type,
var_to_restrict = var_to_restrict,
type = type,
progress_bar = FALSE)
#Save names
nm <- names(pred_test)
#Predict to background
bg_data <- fitted_models$calibration_data
pred_bg <- predict_selected(models = fitted_models,
new_variables = bg_data[,v, drop = FALSE],
consensus = consensus,
extrapolation_type = extrapolation_type,
var_to_restrict = var_to_restrict,
type = type,
progress_bar = FALSE)
#Get only consensus predictions
pred_test <- lapply(names(pred_test), function(i){
if(i == "General_consensus"){
return(pred_test[[i]])
} else {
return(pred_test[[i]]$Model_consensus)
}
})
names(pred_test) <- nm
#Get thresholds
thr <- lapply(names(fitted_models$thresholds), function(i){
if(i != "General_consensus"){
return(fitted_models$thresholds[[i]])
} else {
return(fitted_models$thresholds$consensus)
}
})
names(thr) <- names(fitted_models$thresholds)
names(thr)[names(thr) == "consensus"] <- "General_consensus"
res <- lapply(names(pred_test), function(i){
# print(i)
#Get pred test i
p_i <- pred_test[[i]]
#Get consensus predictions
if(i != "General_consensus"){
#p_i <- p_i$Model_consensus
bg_i <- pred_bg[[i]]$Model_consensus
} else {
bg_i <- pred_bg[[i]]
}
#Calculate omission rate
omr_i <- sapply(names(p_i), function(x){
sum(p_i[[x]] < thr[[i]][[x]])/length(p_i[[x]])
})
#Calculate proc
proc_i <- lapply(names(p_i), function(x){
res_x <- fpROC::auc_metrics(test_prediction = p_i[[x]],
prediction = bg_i[[x]],
threshold = fitted_models$omission_rate)$summary[, 4:5]
if(is.null(res_x)){
res_x <- c(Mean_AUC_ratio = NA, pval_pROC = NA)
}
return(res_x)
})
names(proc_i) <- names(p_i)
proc_i <- as.data.frame(do.call(rbind, proc_i))
#Save results
df_i <- data.frame(Model = i,
consensus = names(p_i),
omr = omr_i)
df_i <- cbind(df_i, proc_i)
colnames(df_i)[3] <- paste0("Omission_rate_at_", fitted_models$omission_rate)
row.names(df_i) <- NULL
return(df_i)
})
res <- do.call(rbind, res)
if(perform_mop){
mop_res <- mop_with_records(train_data = bg_data,
test_data = new_data,
variables = v,
categorical_variables = fitted_models$categorical_variables,
mop_type = mop_type,
calculate_distance = calculate_distance,
where_distance = where_distance,
progress_bar = progress_bar, ...)
} else {
mop_res <- NULL}
if(return_predictions){
predictions <- list()
predictions[["continuous"]] <- do.call(cbind, pred_test)
if(return_binary){
# Initializing a new list to store the binarized data
pred_test_binarized <- list()
# Iterating over each model/consensus and applying binarization
for (model_name in names(pred_test)) {
# Initialize a sublist for the current model within pred_test_binarized
pred_test_binarized[[model_name]] <- list()
# Get the pred_test dataframe for the current model
current_pred_data <- pred_test[[model_name]]
# Get the threshold values for the current model
current_thr_data <- thr[[model_name]]
# Iterate over the 'mean' and 'median' metrics
for (metric in consensus) {
binarized_values <- as.numeric(current_pred_data[[metric]] >= current_thr_data[[metric]])
# Add the binarized values to the model's sublist
pred_test_binarized[[model_name]][[metric]] <- binarized_values
}
# Convert the model's sublist to a dataframe to maintain the original structure
pred_test_binarized[[model_name]] <- as.data.frame(pred_test_binarized[[model_name]])
}
predictions$binary <- do.call(cbind, pred_test_binarized)
}
} else { #End of return_predictions
predictions <- NULL
}
#Append mop results to predictions
if(perform_mop && return_predictions){
for(pred_type in names(predictions)){
predictions[[pred_type]] <- cbind(predictions[[pred_type]], mop_res$mop_records)
}
} else if (perform_mop && !return_predictions){
predictions <- mop_res$mop_records
}
#Final results
final_res <- list("evaluation" = res,
"mop_results" = mop_res$mop_results,
"predictions" = predictions)
return(final_res)
}
#Consensus = mean
#Model 192: Higher omission rate (0.4, > 0.1) and significant pROC value
#Model 189: Higher omission rate (0.4, > 0.1) and significant pROC value
#General consensus: Higher omission rate (0.4, > 0.1) and significant pROC value
#Mop summary
#All independent data are within the ranges of calibration data.
#The mean distance was, varying between and