### Example Usage of checkMaxentInstallation Source: https://consbiol-unibern.github.io/SDMtune/reference/checkMaxentInstallation.html This example demonstrates calling the function and shows the expected output when Maxent is correctly installed, including checks for Java, rJava, and maxent.jar. ```R checkMaxentInstallation() #> ✔ Java is installed. #> ✔ The packege rJava is installed. #> ✔ The file maxent.jar is present in the correct folder. #> [1] TRUE ``` -------------------------------- ### Get Tunable Arguments for an SDM Model Source: https://consbiol-unibern.github.io/SDMtune/reference/getTunableArgs.html This example demonstrates how to train an SDM model and then retrieve its tunable hyperparameters using `getTunableArgs`. Ensure the necessary packages are installed and data is prepared. ```r files <- list.files(path = file.path(system.file(package = "dismo")), "ex", full.names = TRUE) predictors <- terra::rast(files) p_coords <- virtualSp$presence bg_coords <- virtualSp$background data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") model <- train(method = "Maxnet", data = data, fc = "l") getTunableArgs(model) ``` -------------------------------- ### Install SDMtune from GitHub Source: https://consbiol-unibern.github.io/SDMtune/index.html Installs the development version of the SDMtune package directly from its GitHub repository using the devtools package. ```R devtools::install_github("ConsBiol-unibern/SDMtune") ``` -------------------------------- ### Example: Prepare Data and Train Model Source: https://consbiol-unibern.github.io/SDMtune/reference/combineCV.html This example demonstrates the process of acquiring environmental variables, preparing presence and background locations, creating an SWD object, and training a model with cross-validation. It requires the 'dismo' and 'terra' packages. ```R # Acquire environmental variables files <- list.files(path = file.files(package = "dismo"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") #> ℹ Extracting predictor information for presence locations #> ✔ Extracting predictor information for presence locations [20ms] #> #> ℹ Extracting predictor information for absence/background locations #> ✔ Extracting predictor information for absence/background locations [46ms] #> # Create 4 random folds splitting only the presence data များ <- randomFolds(data, k = 4, only_presence = TRUE) model <- train(method = "Maxnet", data = data, folds = folds) #> Cross Validation ■■■■■■■■■ 25% | ETA: 5s - 00:00:1.6 #> Cross Validation ■■■■■■■■■■■■■■■■■■■■■■■ 75% | ETA: 1s - 00:00:4.3 #> Cross Validation ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:5.6 #> ``` -------------------------------- ### Example: Train Maxent Model and Get Variable Importance Source: https://consbiol-unibern.github.io/SDMtune/reference/maxentVarImp.html This example demonstrates the full workflow: acquiring environmental data, preparing species occurrence data, training a Maxent model, and then calculating variable importance using `maxentVarImp`. It requires the 'dismo' and 'terra' packages. ```R # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") #> ℹ Extracting predictor information for presence locations #> ✔ Extracting predictor information for presence locations [20ms] #> #> ℹ Extracting predictor information for absence/background locations #> ✔ Extracting predictor information for absence/background locations [46ms] #> # Train a Maxent model # The next line checks if Maxent is correctly configured but you don't need # to run it in your script model <- train(method = "Maxent", data = data, fc = "l") maxentVarImp(model) #> Variable Percent_contribution Permutation_importance #> 1 bio1 48.4267 0.0000 #> 2 biome 36.7933 7.0174 #> 3 bio8 7.3643 21.7752 #> 4 bio6 2.6993 56.4936 #> 5 bio7 2.5507 0.0000 #> 6 bio16 1.2838 0.0000 #> 7 bio17 0.5727 0.0000 #> 8 bio12 0.2764 14.2192 #> 9 bio5 0.0328 0.4946 ``` -------------------------------- ### Check Maxent Installation Function Source: https://consbiol-unibern.github.io/SDMtune/reference/checkMaxentInstallation.html Use this function to verify if Maxent is correctly installed. Set verbose to TRUE for detailed messages about missing components. ```R checkMaxentInstallation(verbose = TRUE) ``` -------------------------------- ### Example: Hyperparameter Tuning with Grid Search Source: https://consbiol-unibern.github.io/SDMtune/reference/combineCV.html This example demonstrates how to perform hyperparameter tuning using the `gridSearch` function. It defines a list of hyperparameters to test and specifies 'auc' as the evaluation metric. The output includes results and models from the grid search. ```R # Define the hyperparameters to test h <- list(reg = 1:2, fc = c("lqp", "lqph")) # Run the function using the AUC as metric output <- gridSearch(model, hypers = h, metric = "auc") #> Grid Search ■■■■■■■ 20% | ETA: 8s - 00:00:02 #> Grid Search ■■■■■■■■■■■■■ 40% | ETA: 13s - 00:00:8.8 #> Grid Search ■■■■■■■■■■■■■■■■■■■ 60% | ETA: 7s - 00:00:10.3 #> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■ 80% | ETA: 4s - 00:00:16 #> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:16 #> output@results #> fc reg train_AUC test_AUC diff_AUC #> 1 lqp 1 0.8644779 0.8527638 0.01171417 #> 2 lqph 1 0.8729872 0.8542633 0.01872400 #> 3 lqp 2 0.8613124 0.8503793 0.01093317 #> 4 lqph 2 0.8670053 0.8504928 0.01651250 output@models #> [[1]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqp" #> • reg: 1 #> #> ── Info #> • Species: Virtual species #> • Replicates: 4 #> • Total presence locations: 400 #> • Total absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> #> [[2]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqph" #> • reg: 1 #> #> ── Info #> • Species: Virtual species #> • Replicates: 4 #> • Total presence locations: 400 #> • Total absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> #> [[3]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqp" #> • reg: 2 #> #> ── Info #> • Species: Virtual species #> • Replicates: 4 #> • Total presence locations: 400 #> • Total absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> #> [[4]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqph" #> • reg: 2 #> #> ── Info #> • Species: Virtual species #> • Replicates: 4 #> • Total presence locations: 400 #> • Total absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> ``` -------------------------------- ### Run Grid Search for Model Tuning Source: https://consbiol-unibern.github.io/SDMtune/reference/SDMtune-class.html Example demonstrating how to use gridSearch to find optimal hyperparameters for an SDM model. Requires pre-loaded environmental data and training/testing datasets. ```R # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo")), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") #> ℹ Extracting predictor information for presence locations #> ✔ Extracting predictor information for presence locations [21ms] #> #> ℹ Extracting predictor information for absence/background locations #> ✔ Extracting predictor information for absence/background locations [48ms] #> # Split presence locations in training (80%) and testing (20%) datasets datasets <- trainValTest(data, test = 0.2, only_presence = TRUE) train <- datasets[[1]] test <- datasets[[2]] # Train a model model <- train(method = "Maxnet", data = train, fc = "l") # Define the hyperparameters to test h <- list(reg = 1:5, fc = c("lqp", "lqph")) # Run the gridSearch function using as metric the AUC output <- gridSearch(model, hypers = h, metric = "auc", test = test) #> Grid Search ■■■■■■ 18% | ETA: 18s - 00:00:04 #> Grid Search ■■■■■■■■■■■■ 36% | ETA: 11s - 00:00:6.4 #> Grid Search ■■■■■■■■■■■■■■■■■ 55% | ETA: 7s - 00:00:8.7 #> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 91% | ETA: 1s - 00:00:12.5 #> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:12.6 #> output #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Tested hyperparameters #> • fc: "lqp" and "lqph" #> • reg: 1, 2, 3, 4, and 5 ``` -------------------------------- ### Check Maxent Installation Source: https://consbiol-unibern.github.io/SDMtune/index.html This function verifies that Java JDK and rJava are installed and that the maxent.jar file is in the correct directory. It is essential for a successful Maxent model training. ```r checkMaxentInstallation() ``` -------------------------------- ### checkMaxentInstallation Source: https://consbiol-unibern.github.io/SDMtune/reference/checkMaxentInstallation.html Checks if Maxent is correctly installed by verifying Java, the 'rJava' package, and the 'maxent.jar' file. ```APIDOC ## checkMaxentInstallation ### Description Checks if Maxent is correctly installed. If `verbose` is set to `TRUE`, it provides messages indicating which components are correctly installed or missing. ### Function Signature ```R checkMaxentInstallation(verbose = TRUE) ``` ### Arguments * **verbose** (logical) - Optional. If `TRUE`, the function outputs messages detailing the installation status of Java, the 'rJava' package, and the 'maxent.jar' file. Defaults to `TRUE`. ### Value * `TRUE` if Maxent is correctly installed. * `FALSE` otherwise. ### Details For a correct Maxent configuration, the following must be met: * Java must be installed. * The R package "rJava" must be installed. * The file "maxent.jar" must be located in the appropriate directory. ### Examples ```R # Check installation with verbose output checkMaxentInstallation() #> ✔ Java is installed. #> ✔ The packege rJava is installed. #> ✔ The file maxent.jar is present in the correct folder. #> [1] TRUE # Check installation without verbose output checkMaxentInstallation(verbose = FALSE) #> [1] TRUE ``` ``` -------------------------------- ### Prepare data and train SDMmodelCV model Source: https://consbiol-unibern.github.io/SDMtune/reference/predict-SDMmodelCV-method.html Example demonstrating how to acquire environmental variables, prepare presence and background locations, create an SWD object, and train an SDMmodelCV model using the Maxnet method with cross-validation folds. ```R # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo")), "ex", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") #> ℹ Extracting predictor information for presence locations #> ✔ Extracting predictor information for presence locations [20ms] #> #> ℹ Extracting predictor information for absence/background locations #> ✔ Extracting predictor information for absence/background locations [46ms] #> # Create 4 random folds splitting only the presence data folds <- randomFolds(data, k = 4, only_presence = TRUE) model <- train(method = "Maxnet", data = data, fc = "l", folds = folds) ``` -------------------------------- ### Generate Model Report Example Source: https://consbiol-unibern.github.io/SDMtune/reference/modelReport.html Demonstrates how to use the modelReport function after preparing environmental data, presence/background locations, and training an SDM model. Ensure all necessary data and models are set up before calling this function. ```R # If you run the following examples with the function example(), # you may want to set the argument ask like following: example("modelReport", # ask = FALSE) # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo")), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") #> ℹ Extracting predictor information for presence locations #> ✔ Extracting predictor information for presence locations [20ms] #> #> ℹ Extracting predictor information for absence/background locations #> ✔ Extracting predictor information for absence/background locations [46ms] #> # Split presence locations in training (80%) and testing (20%) datasets datasets <- trainValTest( data, test = 0.2, only_presence = TRUE) train <- datasets[[1]] test <- datasets[[2]] # Train a model model <- train( method = "Maxnet", data = train, fc = "lq") # Create the report if (FALSE) { # \dontrun{ modelReport(model, type = "cloglog", folder = "my_folder", test = test, response_curves = TRUE, only_presence = TRUE, jk = TRUE, env = predictors, permut = 2)} # } ``` -------------------------------- ### Example: Order Results by Test AUC Source: https://consbiol-unibern.github.io/SDMtune/reference/combineCV.html This example shows how to order the results from the `gridSearch` function based on the 'test_AUC' column in descending order to identify the best performing hyperparameters. ```R # Order results by highest test AUC output@results[order(-output@results$test_AUC), ] #> fc reg train_AUC test_AUC diff_AUC #> 2 lqph 1 0.8729872 0.8542633 0.01872400 #> 1 lqp 1 0.8644779 0.8527638 0.01171417 #> 4 lqph 2 0.8670053 0.8504928 0.01651250 #> 3 lqp 2 0.8613124 0.8503793 0.01093317 ``` -------------------------------- ### Install SDMtune from CRAN Source: https://consbiol-unibern.github.io/SDMtune/index.html Installs the latest stable release of the SDMtune package from the Comprehensive R Archive Network (CRAN). ```R install.packages("SDMtune") ``` -------------------------------- ### Acquire and Prepare Environmental Data Source: https://consbiol-unibern.github.io/SDMtune/reference/corVar.html This code block demonstrates how to acquire environmental variables using terra::rast, sample background locations, and create a SWD object using prepareSWD. This setup is necessary before using the corVar function. ```R # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare background locations bg_coords <- terra::spatSample(predictors, size = 10000, method = "random", na.rm = TRUE, xy = TRUE, values = FALSE) #> Warning: [spatSample] fewer cells returned than requested # Create SWD object bg <- prepareSWD(species = "Virtual species", a = bg_coords, env = predictors, categorical = "biome") #> ℹ Extracting predictor information for absence/background locations #> ✔ Extracting predictor information for absence/background locations [72ms] #> ``` -------------------------------- ### Run Grid Search with AUC Metric Source: https://consbiol-unibern.github.io/SDMtune/reference/gridSearch.html Execute grid search using the AUC metric. This example shows the typical output including progress bars and the resulting performance metrics for each hyperparameter combination. ```r output <- gridSearch(model, hypers = h, metric = "auc", test = test) #> Grid Search ■■■■■■■■■■■■■ 40% | ETA: 4s - 00:00:2.5 #> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■ 80% | ETA: 1s - 00:00:4.8 #> Grid Search ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s - 00:00:4.8 #> output@results #> fc reg train_AUC test_AUC diff_AUC #> 1 lqp 1 0.8648872 0.8471450 0.01774219 #> 2 lqph 1 0.8747291 0.8450225 0.02970656 #> 3 lqp 2 0.8611684 0.8448300 0.01633844 #> 4 lqph 2 0.8683447 0.8411400 0.02720469 output@models #> [[1]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqp" #> • reg: 1 #> #> ── Info #> • Species: Virtual species #> • Presence locations: 320 #> • Absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> #> [[2]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqph" #> • reg: 1 #> #> ── Info #> • Species: Virtual species #> • Presence locations: 320 #> • Absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> #> [[3]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqp" #> • reg: 2 #> #> ── Info #> • Species: Virtual species #> • Presence locations: 320 #> • Absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> #> [[4]] #> #> ── Object of class: ── #> #> Method: Maxnet #> #> ── Hyperparameters #> • fc: "lqph" #> • reg: 2 #> #> ── Info #> • Species: Virtual species #> • Presence locations: 320 #> • Absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" #> ``` ```r output@results[order(-output@results$test_AUC), ] #> fc reg train_AUC test_AUC diff_AUC #> 1 lqp 1 0.8648872 0.8471450 0.01774219 #> 2 lqph 1 0.8747291 0.8450225 0.02970656 #> 3 lqp 2 0.8611684 0.8448300 0.01633844 #> 4 lqph 2 0.8683447 0.8411400 0.02720469 ``` -------------------------------- ### Define Hyperparameter Values for Grid Search Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/tune-hyperparameters.html Create a named list to specify the hyperparameter values to be tested in `gridSearch`. This example defines values for the regularization multiplier. ```R # Define the values for the regularization multiplier h <- list(reg = seq(0.2, 1, 0.1)) ``` -------------------------------- ### Compute Confusion Matrix for Various Thresholds Source: https://consbiol-unibern.github.io/SDMtune/reference/confMatrix.html This example demonstrates how to compute a confusion matrix for a trained Maxnet model using the 'cloglog' output type. It shows how to obtain the matrix for all unique predicted values across thresholds ranging from 0 to 1 and displays the head and tail of the resulting data frame. ```r files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") # Train a model model <- train(method = "Maxnet", data = data, fc = "l") # Get the confusion matrix for thresholds ranging from 0 to 1 cm <- confMatrix(model, type = "cloglog") head(cm) tail(cm) ``` -------------------------------- ### Compute Confusion Matrix for a Specific Threshold Source: https://consbiol-unibern.github.io/SDMtune/reference/confMatrix.html This example shows how to compute a confusion matrix for a trained model at a single, specified threshold (0.6) and using the 'logistic' output type. This is useful for evaluating model performance at a particular decision point. ```r confMatrix(model, type = "logistic", th = 0.6) ``` -------------------------------- ### Prepare Data for ROC Curve Plotting Source: https://consbiol-unibern.github.io/SDMtune/reference/plotROC.html This code block demonstrates the necessary steps to prepare environmental data and species occurrence data, including creating an SWD object and splitting it into training and testing sets. This setup is required before training a model and plotting its ROC curve. ```R files <- list.files(path = file.path(system.file(package = "dismo")), "ex", pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") # Split presence locations in training (80%) and testing (20%) datasets datasets <- trainValTest(data, test = 0.2, only_presence = TRUE) train <- datasets[[1]] test <- datasets[[2]] ``` -------------------------------- ### Get Correlation Among Environmental Variables Source: https://consbiol-unibern.github.io/SDMtune/reference/corVar.html This example shows how to use corVar to get the correlation among all environmental variables in a SWD object using the 'spearman' method. ```R # Get the correlation among all the environmental variables corVar(bg, method = "spearman") #> Var1 Var2 value #> 1 bio1 bio6 0.95135409 #> 2 bio12 bio16 0.94475589 #> 3 bio6 bio7 -0.87344980 #> 4 bio1 bio8 0.84596493 #> 5 bio16 bio6 0.74712692 #> 6 bio6 bio8 0.72867234 #> 7 bio1 bio7 -0.71191347 #> 8 bio16 bio7 -0.70275684 #> 9 bio1 bio16 0.70235845 #> 10 bio12 bio17 0.69149337 #> 11 bio12 bio6 0.68648556 #> 12 bio12 bio7 -0.67400573 #> 13 bio5 bio8 0.64898352 #> 14 bio1 bio12 0.62722952 #> 15 bio1 bio5 0.53417673 #> 16 bio16 bio17 0.46633132 #> 17 bio16 bio8 0.45811313 #> 18 bio7 bio8 -0.44179392 #> 19 bio12 bio8 0.38856291 #> 20 bio17 bio7 -0.34811898 #> 21 bio5 bio6 0.30086090 #> 22 bio17 bio6 0.26811250 #> 23 bio1 bio17 0.18529719 #> 24 bio16 bio5 0.15713642 #> 25 bio17 bio5 -0.13452379 #> 26 bio5 bio7 0.10995326 #> 27 bio12 bio5 0.09855589 #> 28 bio17 bio8 0.09219238 ``` -------------------------------- ### Get Environmental Variables Above Correlation Threshold Source: https://consbiol-unibern.github.io/SDMtune/reference/corVar.html This example demonstrates how to use corVar to filter and retrieve environmental variables with a Pearson correlation coefficient greater than or equal to a specified threshold (0.8 in this case). ```R # Get the environmental variables that have a correlation greater or equal to # the given threshold corVar(bg, method = "pearson", cor_th = 0.8) #> Var1 Var2 value #> 1 bio12 bio16 0.9314559 #> 2 bio1 bio6 0.9247660 #> 3 bio6 bio7 -0.8576052 #> 4 bio1 bio8 0.8552133 ``` -------------------------------- ### Prepare presence and background locations Source: https://consbiol-unibern.github.io/SDMtune/reference/train.html This code snippet shows how to prepare presence and background location data, likely for use in species distribution modeling. It assumes 'virtualSp' object is available. ```R p_coords <- virtualSp$presence bg_coords <- virtualSp$background ``` -------------------------------- ### Prepare Data and Split into Training, Validation, and Testing Sets Source: https://consbiol-unibern.github.io/SDMtune/articles/hyper-tuning.html This snippet demonstrates how to load environmental predictors, prepare species occurrence data using `prepareSWD`, and then split the data into training, validation, and testing sets using `trainValTest`. The `train` function is then used to train a Maxnet model with default settings on the training data. ```R library(SDMtune) library(zeallot) # Prepare data files <- list.files(path = file.path(system.file(package = "dismo")), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) data <- prepareSWD(species = "Virtual species", p = virtualSp$presence, a = virtualSp$background, env = predictors, categorical = "biome") # Split data in training, validation and testing datasets c(train, val, test) %<-% trainValTest(data, val = 0.2, test = 0.2, only_presence = TRUE, seed = 61516) cat("# Training : ", nrow(train@data)) cat("\n# Validation: ", nrow(val@data)) cat("\n# Testing : ", nrow(test@data)) # Train Maxnet model with default settings model <- train ("Maxnet", data = train) ``` -------------------------------- ### prepareSWD Function Source: https://consbiol-unibern.github.io/SDMtune/reference/prepareSWD.html Creates an SWD object using species name, environmental raster data, and presence/absence coordinates. ```APIDOC ## prepareSWD ### Description Given the coordinates, the species' name and the environmental variables, the function creates an SWD object (sample with data). ### Usage ```R prepareSWD( species, env, p = NULL, a = NULL, categorical = NULL, verbose = TRUE ) ``` ### Arguments * **species** (character): The name of the species. * **env** (rast): Raster containing the environmental variables used to extract the values at coordinate locations. * **p** (data.frame): The coordinates of the presence locations. * **a** (data.frame): The coordinates of the absence/background locations. * **categorical** (vector): Indicates which of the environmental variables are categorical. * **verbose** (logical): If `TRUE`, prints informative messages. ### Value An SWD object. ### Details The SWD object is created in a way that the presence locations are always before than the absence/background locations. ### Examples ```R # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo")), "ex", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create the SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") print(data) ``` ``` -------------------------------- ### Get Tunable Hyperparameters Source: https://consbiol-unibern.github.io/SDMtune/articles/presence-absence.html Retrieves a list of hyperparameters that can be tuned for the given model type using the getTunableArgs function. ```R getTunableArgs(model) ``` -------------------------------- ### Create Random Folds for Cross-Validation Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/train-tune-presence-absence-models.html Generate k-folds for cross-validation using the training data. This example creates a 4-fold partition. ```R folds <- randomFolds(train, k = 4, seed = 25) ``` -------------------------------- ### Prepare SWD Object for addSamplesToBg Source: https://consbiol-unibern.github.io/SDMtune/reference/addSamplesToBg.html This code prepares an SWD object by acquiring environmental variables and defining presence and background locations. It is a prerequisite for using the addSamplesToBg function. ```R # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") ``` -------------------------------- ### Predict for Training Locations Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/make-predictions.html Use the `predict()` function to get predictions for the training locations. Specify the output type, such as 'cloglog'. ```r pred <- predict(default_model, data = data, type = "cloglog") ``` -------------------------------- ### Explore SDMmodel Object Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/train-model.html Print an `SDMmodel` object to view its summary, including the training method, hyperparameters, species information, location counts, and environmental variables used. ```r default_model #> #> ── Object of class: ── #> #> Method: Maxent #> #> ── Hyperparameters #> • fc: "lqph" #> • reg: 1 #> • iter: 500 #> #> ── Info #> • Species: Virtual species #> • Presence locations: 400 #> • Absence locations: 5000 #> #> ── Variables #> • Continuous: "bio1", "bio12", "bio16", "bio17", "bio5", "bio6", "bio7", and #> "bio8" #> • Categorical: "biome" ``` -------------------------------- ### Check Maxent Version with dismo Source: https://consbiol-unibern.github.io/SDMtune/index.html Use this command to check the version of Maxent used by the dismo package. Ensure Java JDK and rJava are installed. ```r dismo::maxent() ``` -------------------------------- ### show method for SDMmodel Source: https://consbiol-unibern.github.io/SDMtune/reference/SDMmodel-class.html The `show` method is an S4 generic function used to display the contents of an SDMmodel object. It provides a summary of the data and model components. ```APIDOC ## show(object) ### Description Displays the contents of an SDMmodel object. ### Method S4 method for class 'SDMmodel' ### Parameters #### Arguments * **object** (SDMmodel) - The SDMmodel object to display. ``` -------------------------------- ### Load SDMtune Package Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/evaluate-model.html Load the SDMtune package before using its functions. This is a standard first step for any SDMtune analysis. ```r library(SDMtune) #> #> _____ ____ __ ___ __ #> / ___/ / __ \ / |/ // /_ __ __ ____ ___ #> \__ \ / / / // /|_/ // __// / / // __ \ / _ \ #> ___/ // /_/ // / / // /_ / /_/ // / / // __/ #> /____//_____//_/ /_/ \__/ \__,_//_/ /_/ \___/ version 1.3.3 #> #> To cite this package in publications type: citation("SDMtune"). ``` -------------------------------- ### Get Threshold Values for SDM Model Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/make-predictions.html Retrieve commonly used threshold values from a trained SDM model object. Use this to determine presence/absence criteria for your predictions. ```r ths <- thresholds(default_model, type = "cloglog") ths ``` -------------------------------- ### Create SWD Object Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/prepare-data.html Use prepareSWD to create an SWD object. Specify presence (p) and background (a) coordinates, environmental predictors (env), and identify categorical variables. ```r data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") ``` -------------------------------- ### Get Threshold Values for Presence/Absence Map Source: https://consbiol-unibern.github.io/SDMtune/articles/basic-use.html Retrieves commonly used threshold values from an SDMmodel object. Use the 'type' argument to specify the threshold calculation method, such as 'cloglog'. ```R ths <- thresholds(model, type = "cloglog") ths ``` -------------------------------- ### Train an ANN Model with Default Settings Source: https://consbiol-unibern.github.io/SDMtune/articles/presence-absence.html Trains an Artificial Neural Network (ANN) model using the prepared training data and specified number of neurons. Cross-validation folds are used for training. ```R set.seed(25) model <- train("ANN", data = train, size = 10, folds = folds) ``` -------------------------------- ### Get MaxEnt Thresholds from SDM Model Source: https://consbiol-unibern.github.io/SDMtune/reference/maxentTh.html Use this function to retrieve a data frame of threshold values computed by MaxEnt. Requires an SDMmodel object trained with the 'Maxent' method. ```r files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") # Train a Maxent model model <- train(method = "Maxent", data = data, fc = "l") maxentTh(model) ``` -------------------------------- ### Prepare Data for Presence-Absence Modeling Source: https://consbiol-unibern.github.io/SDMtune/articles/presence-absence.html Loads environmental predictors and species occurrence data, then prepares it for SDMtune using the prepareSWD function. Ensures data is split into training and testing sets and creates cross-validation folds. ```R library(SDMtune) library(zeallot) # Prepare data files <- list.files(path = file.path(system.file(package = "dismo")), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) p_coords <- virtualSp$presence a_coords <- virtualSp$absence data <- prepareSWD(species = "Virtual species", p = p_coords, a = a_coords, env = predictors[[1:8]]) # Split data in training and testing datasets c(train, test) %<-% trainValTest(data, test = 0.2, seed = 25) cat("# Training : ", nrow(train@data)) cat("\n# Testing : ", nrow(test@data)) # Create folds folds <- randomFolds(train, k = 4, seed = 25) ``` -------------------------------- ### Compute cloglog thresholds for a Maxnet model Source: https://consbiol-unibern.github.io/SDMtune/reference/thresholds.html Use this snippet to obtain cloglog thresholds for a trained Maxnet model. Ensure the model object is already created and the 'dismo' package is installed. ```r # Acquire environmental variables files <- list.files(path = file.path(system.file(package = "dismo")), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") # Split presence locations in training (80%) and testing (20%) datasets datasets <- trainValTest(data, test = 0.2, only_presence = TRUE) train <- datasets[[1]] test <- datasets[[2]] # Train a model model <- train(method = "Maxnet", data = train, fc = "l") # Get the cloglog thresholds thresholds(model, type = "cloglog") ``` -------------------------------- ### Optimize Model using Genetic Algorithm Source: https://consbiol-unibern.github.io/SDMtune/articles/hyper-tuning.html Employ `optimizeModel` which utilizes a genetic algorithm to find optimal or near-optimal hyperparameter solutions. This method learns from trained models to guide the search. ```r exp_7 <- optimizeModel(model, hypers = h, metric = "auc", test = val, pop = 15, gen = 2, seed = 798) ``` -------------------------------- ### Prepare SWD object for data splitting Source: https://consbiol-unibern.github.io/SDMtune/reference/trainValTest.html This code prepares the SWD object by loading environmental variables and defining presence and background locations. It is a prerequisite for using the `trainValTest` function. ```R files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE) predictors <- terra::rast(files) # Prepare presence and background locations p_coords <- virtualSp$presence bg_coords <- virtualSp$background # Create SWD object data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords, env = predictors, categorical = "biome") ``` -------------------------------- ### Split Data for Training and Testing Source: https://consbiol-unibern.github.io/SDMtune/articles/articles/train-tune-presence-absence-models.html Split the prepared SWD data into training and testing sets, holding apart 20% for testing. This is crucial for model evaluation. ```R library(zeallot) c(train, test) %<-% trainValTest(data, test = 0.2, seed = 25) ``` -------------------------------- ### Get Threshold Values for Presence/Absence Map Source: https://consbiol-unibern.github.io/SDMtune/articles/make-predictions.html Retrieve commonly used threshold values from a trained SDM model object. Use the `type` argument to specify the desired threshold calculation method. ```R ths <- thresholds(default_model, type = "cloglog") ths ```