Skip to content

Commit 28f0783

Browse files
authored
Merge pull request #71 from wevertonbio/main
New functions and vignettes
2 parents ec27e01 + 1435b31 commit 28f0783

File tree

84 files changed

+3860
-547
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3860
-547
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Imports:
3535
doSNOW,
3636
enmpa (>= 0.2.1),
3737
foreach (>= 1.5),
38+
fpROC (>= 0.1.0),
3839
glmnet (>= 4.1),
3940
grDevices,
4041
graphics,

NAMESPACE

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ S3method(print,projection_data)
88
export(advanced_cleaning)
99
export(bivariate_response)
1010
export(calibration)
11+
export(colors_for_changes)
1112
export(detect_concave)
1213
export(explore_calibration_geo)
1314
export(explore_calibration_hist)
@@ -19,6 +20,7 @@ export(glmnet_mx)
1920
export(import_projections)
2021
export(initial_cleaning)
2122
export(move_2closest_cell)
23+
export(organize_for_projection)
2224
export(organize_future_worldclim)
2325
export(partial_roc)
2426
export(perform_pca)
@@ -43,15 +45,16 @@ export(sort_columns)
4345
export(variable_importance)
4446
importFrom(doSNOW,registerDoSNOW)
4547
importFrom(enmpa,aux_var_comb)
46-
importFrom(enmpa,kfold_partition)
4748
importFrom(enmpa,predict_glm)
48-
importFrom(enmpa,proc_enm)
4949
importFrom(foreach,`%dopar%`)
5050
importFrom(foreach,foreach)
51+
importFrom(fpROC,auc_metrics)
5152
importFrom(glmnet,glmnet)
5253
importFrom(glmnet,glmnet.control)
5354
importFrom(grDevices,adjustcolor)
55+
importFrom(grDevices,col2rgb)
5456
importFrom(grDevices,hcl.colors)
57+
importFrom(grDevices,rgb)
5558
importFrom(graphics,abline)
5659
importFrom(graphics,arrows)
5760
importFrom(graphics,axis)
@@ -101,12 +104,14 @@ importFrom(terra,as.data.frame)
101104
importFrom(terra,buffer)
102105
importFrom(terra,cells)
103106
importFrom(terra,clamp)
107+
importFrom(terra,coltab)
104108
importFrom(terra,crop)
105109
importFrom(terra,diff)
106110
importFrom(terra,distance)
107111
importFrom(terra,ext)
108112
importFrom(terra,extract)
109113
importFrom(terra,levels)
114+
importFrom(terra,mask)
110115
importFrom(terra,mean)
111116
importFrom(terra,median)
112117
importFrom(terra,minmax)

R/Classes.R

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ new_explore_list <- function(summary, exploration_stats,
2828

2929
# prepared_data Class Constructor
3030
new_prepared_data <- function(species, calibration_data, formula_grid,
31-
kfolds, data_xy, continuous_variables,
31+
part_data, partition_method, n_replicates,
32+
train_proportion, data_xy,
33+
continuous_variables,
3234
categorical_variables, weights, pca, algorithm) {
3335
data <- list(
3436
species = species,
3537
calibration_data = calibration_data,
3638
formula_grid = formula_grid,
37-
kfolds = kfolds,
39+
part_data = part_data,
40+
partition_method = partition_method,
41+
n_replicates = n_replicates,
42+
train_proportion = train_proportion,
3843
data_xy = data_xy,
3944
continuous_variables = continuous_variables,
4045
categorical_variables = categorical_variables,
@@ -77,7 +82,10 @@ new_fitted_models <- function(species,
7782
addsamplestobackground,
7883
omission_rate,
7984
thresholds,
80-
algorithm){
85+
algorithm,
86+
partition_method,
87+
n_replicates,
88+
train_proportion){
8189
data <- list(
8290
species = species,
8391
Models = Models,
@@ -90,7 +98,10 @@ new_fitted_models <- function(species,
9098
addsamplestobackground = addsamplestobackground,
9199
omission_rate = omission_rate,
92100
thresholds = thresholds,
93-
algorithm = algorithm
101+
algorithm = algorithm,
102+
partition_method = partition_method,
103+
n_replicates = n_replicates,
104+
train_proportion = train_proportion
94105
)
95106
class(data) <- "fitted_models"
96107
return(data)

R/Methods.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ print.prepared_data <- function(x, ...) {
9999
cat(" - Presence:", table(x$calibration_data$pr_bg)[2], "\n")
100100
cat(" - Background:", table(x$calibration_data$pr_bg)[1], "\n")
101101

102-
cat("Training-Testing Method:\n")
103-
cat(" - k-fold Cross-validation:", length(x$kfolds), "folds\n")
102+
cat("Partition Method:", x$partition_method, "\n")
103+
if(x$partition_method %in% c("kfolds", "leave-one-out"))
104+
cat(" - Number of kfolds:", length(x$part_data), "\n")
105+
if(x$partition_method%in% c("subsample", "bootstrap")){
106+
cat(" - Number of replicates:", x$n_replicates, "\n")
107+
cat(" - Train proportion:", x$train_proportion, "\n")
108+
}
104109

105110
cat("Continuous Variables:\n")
106111
cat(" -", paste(x$continuous_variables, collapse = ", "), "\n")

R/bivariate_response.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@
5252
#' data(fitted_model_maxnet, package = "kuenm2")
5353
#'
5454
#' # Response curve (notice response affected by covariance)
55-
#' bivariate_response(models = fitted_model_maxnet, modelID = "Model_189",
55+
#' bivariate_response(models = fitted_model_maxnet, modelID = "Model_219",
5656
#' variable1 = "bio_1", variable2 = "bio_12")
5757
#'
5858
#' # Example with glm
5959
#' # Import example of fitted_models (output of fit_selected())
6060
#' data(fitted_model_glm, package = "kuenm2")
6161
#'
6262
#' # Response curve
63-
#' bivariate_response(models = fitted_model_glm, modelID = "Model_86",
63+
#' bivariate_response(models = fitted_model_glm, modelID = "Model_85",
6464
#' variable1 = "bio_1", variable2 = "bio_7")
6565

6666
bivariate_response <- function(models,

R/calibration.R

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
#' @importFrom utils txtProgressBar setTxtProgressBar read.csv head write.csv
7575
#' @importFrom stats aggregate glm as.formula
7676
#' @importFrom glmnet glmnet.control glmnet
77-
#' @importFrom enmpa proc_enm
77+
#' @importFrom enmpa predict_glm
78+
#' @importFrom fpROC auc_metrics
7879
#'
7980
#' @export
8081
#'
@@ -129,23 +130,39 @@
129130
#' Warren and Seifert (2011).
130131
#'
131132
#' @examples
132-
#' # Import prepared data for maxnet models
133-
#' data(sp_swd, package = "kuenm2")
133+
#' # Import occurrences
134+
#' data(occ_data, package = "kuenm2")
134135
#'
136+
#' # Import raster layers
137+
#' var <- terra::rast(system.file("extdata", "Current_variables.tif",
138+
#' package = "kuenm2"))
139+
#' # Use only continuous variables
140+
#' var <- var[[c("bio_1", "bio_7", "bio_12", "bio_15")]]
141+
#'
142+
#' # Prepare data for maxnet model
143+
#' sp_swd <- prepare_data(algorithm = "maxnet", occ = occ_data,
144+
#' x = "x", y = "y",
145+
#' raster_variables = var,
146+
#' species = occ_data[1, 1],
147+
#' n_background = 100, ,
148+
#' features = c("l", "lq"),
149+
#' r_multiplier = 1,
150+
#' partition_method = "kfolds")
135151
#' # Model calibration (maxnet)
136152
#' m <- calibration(data = sp_swd, error_considered = 10)
137-
#'
138153
#' m
139154
#'
140-
#' # Import prepared data for GLM models
141-
#' data(sp_swd_glm, package = "kuenm2")
142-
#'
143-
#' ## Model calibration (GLM)
155+
#' # Prepare data for glm model
156+
#' sp_swd_glm <- prepare_data(algorithm = "glm", occ = occ_data,
157+
#' x = "x", y = "y",
158+
#' raster_variables = var,
159+
#' species = occ_data[1, 1],
160+
#' n_background = 100,
161+
#' features = c("l", "lq"),
162+
#' partition_method = "kfolds")
144163
#' m_glm <- calibration(data = sp_swd_glm, error_considered = 10)
145-
#'
146164
#' m_glm
147165

148-
149166
calibration <- function(data,
150167
error_considered,
151168
remove_concave = FALSE,
@@ -260,7 +277,15 @@ calibration <- function(data,
260277
message("Task 1/2: checking for concave responses in models:")
261278
}
262279

263-
q_grids <- formula_grid[grepl("q", formula_grid$Features), ]
280+
#Get maximum reg multipliers (if algorithm is maxnet)
281+
if(algorithm == "maxnet"){
282+
max_reg <- max(formula_grid$R_multiplier)
283+
q_grids <- formula_grid[grepl("q", formula_grid$Features) &
284+
formula_grid$R_multiplier == max_reg, ]
285+
} else {
286+
q_grids <- formula_grid[grepl("q", formula_grid$Features), ]
287+
}
288+
#Get total number of models to test
264289
n_tot <- nrow(q_grids)
265290

266291
if (n_tot == 0) {

0 commit comments

Comments
 (0)