diff --git a/2_buffer_and_randomPoints/buffer_and_randomPoints.Rmd b/2_buffer_and_randomPoints/buffer_and_randomPoints.Rmd index 0d5d8b0..81f53f3 100644 --- a/2_buffer_and_randomPoints/buffer_and_randomPoints.Rmd +++ b/2_buffer_and_randomPoints/buffer_and_randomPoints.Rmd @@ -36,11 +36,10 @@ Then, load all required packages. ```{r load} library(rJava) library(raster) -#library(modleR) library(dplyr) +#library(modleR) #eu estou usando uma cópia local para desenvolvimento devtools::load_all("../../modleR") -#devtools::load_all("/home/sara/Dropbox/JBRJ/modleR/modleR_pkg_local") ``` # The example data set @@ -80,9 +79,30 @@ points(train_set[,2:3], col = "red", pch = 19) points(test_set[,2:3], col = "blue", pch = 19) ``` -# Pseudoabsence selection without any buffer +# Function `create_buffer()` and `setup_sdmdata()` + +Here, for all types of buffer, first we create an object with `create_buffer()` using different types of buffer and then generate the background values with `randomPoints()` from `dismo` package. + +Another option for creating any type of buffer is using function `setup_sdmdata()` from `modleR`, this function calls `create_buffer()` and performs the sampling, so there is no need to call for `dismo::randomPoints()` or `modleR::create_buffer()` from `modleR`. + +We implemented three types of buffer that can be combined: (A) geographic distance buffers (B) environmental distance buffer (C) a "minimal distance" exclusion buffer, to avoida areas that are very close to the occurrence points. + +- __(A)__ geographic distance buffers: + - `max`: the maximum distance between any two occurrence points + - `mean`: the mean distance between all occurrence points + - `median`: the median of the pairwise distance between occurrence points + - `distance`: the user can specify a particular distance to be used as buffer width - in raster units +- __(A')__ Instead of using a distance for the buffer construction, option `user` allows the user to provide their own shapefile as available area (M) to sample pseudoabsences + +- (B) an `environmental distance` buffer that samples within areas with environmental affinities +- a minimum distance (`mindist`) exclusion buffer, that excludes points that are too close to the occurrence points, in order to avoid overfitting + +Figure 1 explains the possible combinations of these three kinds of buffers. + + + +## Pseudoabsence selection without any buffer -We use `randomPoints` from `dismo` package and `create_buffer` from `modleR` to extract background values without using a buffer. First, directly from `randomPoints`. @@ -97,33 +117,40 @@ plot(predictor, legend = F, main = "nobuffer randomPoints") points(rand, col = "red") ``` +then from `setup_sdmdata`: -# Function `create_buffer` +Here we specify `buffer_type = NULL` to perform background point generation. -Here, for all types of buffer, first we create an object with `create_buffer()` using different types of buffer and then generate the background values with `randomPoints()` from `dismo` package. In `create_buffer()` function we implemented three types of buffer: +```{r sdmdatanull, eval = T} +# background point generation inside setup_sdmdata +a <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[,-1], + predictors = example_vars, + models_dir = "./buffer_res/ss_no_buf", + buffer_type = NULL, # no buffer + clean_dupl = T, + clean_nas = T, + seed = 512) +# plotting background point generation +knitr::include_graphics("./buffer_res/ss_no_buf/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") +``` + + +## Pseudoabsence selection using `create_buffer()` and `randomPoints()` or `setup_smdata()` -- five distance buffers: - - `max`: maximum distance between occurrence points - - `mean`: mean distance between occurrence points - - `median`: median of distance between occurrence points - - `distance`: specify a particular distance from each point to sample pseudoabsences - - a `user` buffer, where the user can specify their own shapefile -- an `environmental distance` buffer that samples within areas with environmental affinities -- a minimum distance (`mindist`) exclusion buffer, that excludes points that are too close to the occurrence points, in order to avoid overfitting -Figure 1 explains the possible combinations of these three kinds of buffers. -## Buffer with maximum distance `max` +### Inclusion buffer with maximum distance `max` -In this example, we use `buffer_type = "max"` to generate our first object. +In this example, we use `buffer_type = "maximum"` to generate our first object. ```{r buffermax, echo = F, eval = T, warning = F} #par(mfrow = c(1,1)) # creating an object with create_buffer with maximum distance buffer type buf.max <- create_buffer(occurrences = coord1sp[,c(2,3)], predictors = predictor, - buffer_type = "max") + buffer_type = "maximum") # creating 500 background values from buf.max buf.max.p <- dismo::randomPoints(buf.max, n = 500, @@ -140,7 +167,7 @@ plot(buf.max, add = T, legend = F, points(buf.max.p, col = "blue", pch = 19, cex = 0.7) ``` -## Buffer with `mean` distance +### Inclusion buffer with `mean` distance In this example, we use `buffer_type = "mean"`. @@ -166,7 +193,7 @@ plot(buf.mean, add = T, legend = F, points(buf.mean.p, col = "blue") ``` -## Buffer with `median` of distance +### Inclusion buffer with `median` of distances In this example, we use `buffer_type="median"` @@ -192,7 +219,7 @@ plot(buf.med, add = T, legend = F, points(buf.med.p, col = "blue") ``` -## Buffer with a specific `distance` +### Inclusion buffer with a specific `distance` In this example we specify a particular distance from each point to sample pseudoabsences inside the buffer. We use `buffer_type = "distance"` and `dist_buf = 5`. Be aware that `dist_buf` must be set when using a distance buffer. @@ -218,7 +245,7 @@ plot(buf.dist, add = T, legend = F, points(buf.dist.p, col = "blue") ``` -## A user-defined buffer `user` +### Inclusion buffer within a user-defined shapefile (`user` and `buffer_shape`) In this example we specify a shapefile that we use as the buffer. Please note that a `buffer_shape` must be included in order to use this buffer. @@ -244,24 +271,27 @@ plot(buf.user, add = T, legend = F, points(buf.user.p, col = "blue") ``` -## The environmental distance buffer +### Inclusion buffer using a euclidean environmental distance -In additon to geographic distance buffers, we can apply an environmental distance buffer, that sets the area available for pseudoabsence sampling as a function of the environemntal conditions found at the occurrence points. -Since environemntal distances can take large values, the user may set a maximum envorinmental distance, expressed in quantile form, to set a maximum distance in order tu cut (and create) this buffer. The default value is 0.5, the median of the distances found along the areas. +In addition to geographic distance buffers, we can apply an environmental distance buffer, that sets the area available for pseudoabsence sampling as a function of the environmental conditions found at the occurrence points. +Since environmental distances can take large values, the user may set a maximum envorinmental distance, expressed in quantile form, to set a maximum distance in order tu cut (and create) this buffer. The default value is 0.5, the median of the distances found along the areas. -NOTE: CURRENTLY THE ENVIRONMENTAL DISTANCE BUFFER CANNONT BE APPLIED SIMULTANEOUSLY WITH OTHER DISTANCE BUFFERS BUT IT SHOULD +NOTE: THE ENVIRONMENTAL DISTANCE BUFFER CAN BE APPLIED SIMULTANEOUSLY WITH OTHER DISTANCE BUFFERS +#### Sem buffer_type ```{r} #the environmental distance filter works with a stack, rather than a single layer (ö testar com poucos pontos, pois não faz sentido cortar pela mediana com n 1 ou 2) predictor <- example_vars buf.env <- create_buffer(occurrences = coord1sp[,c(2,3)], - predictors = predictor, - buffer_type = "environmental_distance", - env_distance = "centroid", - max_env_dist = 0.5) + predictors = predictor, + buffer_type = "none", + env_buffer = T, + env_distance = "centroid", + max_env_dist = 0.5) + # using buf.env to generate 500 pseudoabsences buf.env.p <- dismo::randomPoints(buf.env, n = 500, @@ -272,455 +302,124 @@ buf.env.p <- dismo::randomPoints(buf.env, plot(predictor[[1]], legend = F, main = "environmental distance buffer") ## adding buff -plot(buf.env, add = T, legend = F, +plot(buf.env[[1]], add = T, legend = F, col = scales::alpha("grey", 0.8), border = "black") ## adding buf.user.p points(buf.env.p, col = "blue") -``` - - -## A mindist _exclusion_ buffer - -In addition to the combination of distance and environmental buffers, we implmemented - -```{r MINDIST, eval = T} -mindist <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[, -1], - predictors = example_vars, - models_dir = "./buffer_res/mindist", - buffer_type = "distance", - dist_min = 3, - clean_dupl = T, - clean_na = T, - dist_buf = 7) -knitr::include_graphics("./buffer_res/mindist/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -mx_dmod <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/mindist", - algo = "maxent", - write_png = T) - -knitr::include_graphics("./buffer_res/mindist/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.png") -mx_dmod$TSS ``` -# user e mindist -```{r user} -myshapefile <- rgdal::readOGR("../data/myshapefile.shp") -user <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[, -1], - predictors = example_vars, - models_dir = "./buffer_res/usrshp", - buffer_type = "user", - buffer_shape = myshapefile, - dist_min = 3, - clean_dupl = T, - clean_nas = T) -usr <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/usrshp", - algo = "maxent", - write_png = T) - -knitr::include_graphics("./buffer_res/usrshp/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png",dpi = 150) - -knitr::include_graphics("./buffer_res/usrshp/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.png") -usr$TSS -``` - - -` - -### com mindist +#### Com `buffer_type` ```{r} -user <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[, -1], - predictors = example_vars, - models_dir = "./buffer_res/ambmin", - buffer_type = "environmental_distance", - env_distance = "centroid", - max_env_dist = 0.5, - dist_min = 1, - clean_dupl = T, - clean_nas = T, - write_buffer = T) - -eusr <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/ambmin", - algo = "maxent", - write_png = T) -``` - -```{r, echo = F} -knitr::include_graphics("./buffer_res/ambmin/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -knitr::include_graphics("./buffer_res/ambmin/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.png") -``` +#the environmental distance filter works with a stack, rather than a single layer (ö testar com poucos pontos, pois não faz sentido cortar pela mediana com n 1 ou 2) +buf.env <- create_buffer(occurrences = coord1sp[,c(2,3)], + predictors = predictor, + buffer_type = "maximum", + env_buffer = T, + env_distance = "centroid", + max_env_dist = 0.5) +# using buf.env to generate 500 pseudoabsences +buf.env.p <- dismo::randomPoints(buf.env, + n = 500, + excludep = T, + p = pts) +# plotting environmental layer with background values +## environmental layer +plot(predictor[[1]], + legend = F, main = "environmental distance buffer x maximum distance buffer") +## adding buff +plot(buf.env, add = T, legend = F, + col = scales::alpha("grey", 0.8), border = "black") +## adding buf.user.p +points(buf.env.p, col = "blue") +``` -# Within `setup_sdmdata` -Another option for implementing no buffer or any type of buffer is using function `setup_sdmdata()` from `modleR`. In this case there's no need to call for `randomPoints()` from `dismo` package or `create_buffer()` from `modleR` since it is implemented inside `setup_sdmdata()`. -## `setup_sdmdata` without buffer +### The `mindist` _exclusion_ buffer -Here we specify `buffer_type = NULL` to perform background point generation. +In addition to the combination of distance and environmental buffers, we implemented the __exclusion__ of points that are too close to the occurrence records, to control for overfitting. All distance buffers can be used with the additional parameter `mindist`. -```{r sdmdatanull, eval = T} -# background point generation inside setup_sdmdata -a <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[,-1], +```{r MINDIST, eval = T} +#maxdist + mindist +m <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[, -1], predictors = example_vars, - models_dir = "./buffer_res/setupsdmdatanul", - buffer_type = NULL, # no buffer + models_dir = "./buffer_res/mindist_maxdist", + buffer_type = "maximum", + dist_min = 3, clean_dupl = T, - clean_nas = T, - seed = 512) -# checking the output -head(a) -# plotting background point generation -knitr::include_graphics("./buffer_res/setupsdmdatanul/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -``` - -## `setup_sdmdata` with maximum distance - -Here we specify `buffer_type = max` to use maximum distance between occurrence points to perform background point generation. - -```{r sdmdatamax, eval = T} -# background point generation inside setup_sdmdata with maximum distance -b <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[,-1], + clean_na = T) +knitr::include_graphics("./buffer_res/mindist_maxdist/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") +#mean + mindist +m <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[, -1], predictors = example_vars, - models_dir = "./buffer_res/setupsdmdatamax", - real_absences = NULL, - buffer_type = "max", # maximum distance between points + models_dir = "./buffer_res/mindist_meandist", + buffer_type = "mean", + dist_min = 3, clean_dupl = T, - clean_nas = T, - seed = 512) -# chacking the output -head(b) -# plotting backgrounf point generation -knitr::include_graphics("./buffer_res/setupsdmdatamax/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -``` - -## `setup_sdmdata` with mean distance - -Here we specify `buffer_type = mean` to use maximum distance between occurrence points to perform background point generation. - -```{r sdmdatamean, eval = T} -# background point generation inside setup_sdmdata with mean distance -c <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[,-1], + clean_na = T) +knitr::include_graphics("./buffer_res/mindist_meandist/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") +#median + mindist +m <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[, -1], predictors = example_vars, - models_dir = "./buffer_res/setupsdmdatamean", - buffer_type = "mean", # setting mean distance between occurrence points + models_dir = "./buffer_res/mindist_mediandist", + buffer_type = "median", + dist_min = 3, clean_dupl = T, - clean_nas = T, - seed = 512) -# checking the output -head(c) -# plotting background point generation -knitr::include_graphics("./buffer_res/setupsdmdatamean/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -``` - -## `setup_sdmdata` with median of distance between occurrence points - -Here we specify `buffer_type = median` to use median of distance between occurrence points to perform background point generation. + clean_na = T) +knitr::include_graphics("./buffer_res/mindist_mediandist/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -```{r sdmdatamedian, eval = T} -# background point generation inside setup_sdmdata with mean distance -d <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[,-1], +#geog.distance + mindist +m <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[, -1], predictors = example_vars, - models_dir = "./buffer_res/setupsdmdatamed", - buffer_type = "median", # median of distance between occcurrence points + models_dir = "./buffer_res/mindist_distance", + buffer_type = "distance", + dist_min = 3, clean_dupl = T, - clean_nas = T, - seed = 512) -# checking the output -head(d) -# plotting background point generation -knitr::include_graphics("./buffer_res/setupsdmdatamed/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -``` - -## `setup_sdmdata` with a specific distance between occurrence points - -Here we specify `buffer_type = distance` and `dist_buf = 5` to use 5 degree buffer from each occurrence point to perform background point generation. Note that it is mandatory to set `dist_buf` when using a distance buffer. + clean_na = T, + dist_buf = 7) +knitr::include_graphics("./buffer_res/mindist_distance/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -```{r sdmdatadist, eval = T} -# background point generation inside setup_sdmdata with a specific distance -e <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[,-1], +#user + mindist +myshapefile <- rgdal::readOGR("../data/myshapefile.shp") +m <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[, -1], predictors = example_vars, - models_dir = "./buffer_res/setupsdmdatadist", - buffer_type = "distance", # specific distance - dist_buf = 5, # must set dist_buf! + models_dir = "./buffer_res/mindist_usrshp", + buffer_type = "user", + buffer_shape = myshapefile, + dist_min = 3, clean_dupl = T, - clean_nas = T, - seed = 512) -# checking the output -head(e) -# plotting the background point generation -knitr::include_graphics("./buffer_res/setupsdmdatadist/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -``` - -# Effect of different types of buffer on model projection and evaluation - -Now we want to inspect how different types of buffer affect the prediction of the species distribution model. - -## Using no buffer on model predictions - -Here, we use the algorithm `maxent` without any buffer to generate a model for the species *Abarema langsdorfii*. - -A simple model with no buffer: - -```{r enm1, message = F, eval = T} -# running a maxent model for Abarema langsdorfii with no buffer -no_buf <- setup_sdmdata(species_name = especies[1], # sp 1 Abarema langsdorfii - occurrences = coord1sp[, -1], # coordinates for the species - predictors = example_vars, # example environmental data - models_dir = "./buffer_res/buftest1", - clean_dupl = T, - clean_na = T) -mx_mod <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/buftest1", - algo = "maxent") -# creating a raster object with the output of the model -no_buf_m <- raster("./buffer_res/buftest1/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.tif") -# plotting the model -plot(no_buf_m) -# adding political limts to the map -maps::map(,,add = T) -``` - -## Using `median` buffer on model predictions - -Again, we use the algorithm `algo = maxent` but now we set `buffer_type = median` to generate a model for the species *Abarema langsdorfii*. - -With a median buffer: - -```{r enm2, message = F, eval = T} -# Running a maxent model for A. langsdorfii with a median buffer -mn_buf <- setup_sdmdata(species_name = especies[1], # sp 1 Abarema langsdorfii - occurrences = coord1sp[, -1], # coordinates for the species - predictors = example_vars, # example environmental data - models_dir = "./buffer_res/buftest2", - clean_dupl = T, - clean_na = T, - buffer_type = "median") -mn_mod <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/buftest2", - algo = "maxent", - write_png = T) -# creating a raster object with the output of the model -knitr::include_graphics("./buffer_res/buftest2/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.png") -``` - -## Comparing no buffer with `median` buffer - -Fitting and evaluating the models with different background samples has a different outcome: the model with a median has a slightly worse performance **BUT** a tendency **NOT** to overfit. - -```{r remedy001, fig.cap="Comparing the sdmdata datasets", eval = T, echo=FALSE, fig.width=6, fig.height=3} - -knitr::include_graphics("./buffer_res/buftest1/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") ; -knitr::include_graphics("./buffer_res/buftest2/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -``` - -# Effect of buffer on the background data in MaxEnt - -Let's create a dataset for training and test using `setup_sdmdata`: - -```{r maxent, cache = F, eval = T} -# Creating the test data set -sdmdata_test <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp, - predictors = example_vars, - models_dir = "./buffer_res/sdmdatatestmaxent", - real_absences = NULL, - buffer_type = "median", - n_back = 500, - partition_type = "bootstrap", - boot_n = 1, - boot_proportion = 0.7) # 70% of original data + clean_nas = T) +knitr::include_graphics("./buffer_res/mindist_usrshp/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") +#ENV + mindist +m <- setup_sdmdata(species_name = especies[1], + occurrences = coord1sp[, -1], + predictors = example_vars, + models_dir = "./buffer_res/envdist_usrshp", + buffer_type = "environmental_distance", + dist_min = 3, + clean_dupl = T, + clean_nas = T) +knitr::include_graphics("./buffer_res/envdist_usrshp/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") ``` -The training test corresponds to: +# una figura para la vignette ```{r} -# checking the sdmdata_test -head(sdmdata_test) -``` +buf <- c("a", "user", "maximum", "mean","median") +env <- c(F, T) +dist_min <- c(0, 1) +expand.grid(buffer_type = buf, env_filter = env, dist_min = dist_min) -The column `pa` in `sdmdata_test` corresponds to presences and absences and `0` in `boot1` corresponds to the training data set. - -```{r, eval = T} -# counting presences and absences in training and test data sets -count(sdmdata_test, pa, boot1) -``` - -Let's create three objects: one for the training data set named `prestrain` and two for the test data set being one for presences (`prestest`) and other for absences (`backtest`). - -```{r, eval = T} -# the training points -prestrain <- sdmdata_test %>% - filter(boot1 == 0, pa == 1) %>% - dplyr::select(lon, lat) -# the test points -## presence test points -prestest <- sdmdata_test %>% - filter(boot1 == 1, pa == 1) %>% - dplyr::select(lon, lat) -## absence test points -backtest <- sdmdata_test %>% - filter(boot1 == 1, pa == 0) %>% - dplyr::select(lon, lat) -``` - -We want to know if there are differences when you fit a model to the buffer environmental space, especially maxent. - -First, we crop the environmental data to our buffer - -```{r remedy002, cache = F, eval = T} - -cropped_pred <- modleR::crop_model(example_vars, buf.med) -plot(cropped_pred[[1]]) -``` - -#### For Bioclim, Mahal, domain, it doesn't make a difference - -```{r bioclim, eval = T} -b <- dismo::bioclim(example_vars, prestrain) -b2 <- dismo::bioclim(cropped_pred, prestrain) -identical(b,b2) -``` - -```{r mahal, eval = T} -c <- dismo::mahal(example_vars, prestrain) -c2 <- dismo::mahal(cropped_pred, prestrain) -identical(c,c2) -``` - -```{r dom, eval = T} -dm <- dismo::domain(example_vars, prestrain) -dm2 <- dismo::domain(cropped_pred, prestrain) -identical(dm, dm2) -``` - - -#### But for Maxent, as an algorithm that uses the background data, it does - -```{r max, eval = T} -mx <- dismo::maxent(example_vars, prestrain) -mx2 <- dismo::maxent(cropped_pred, prestrain) -identical(mx, mx2) -``` - -Let's project these models - -```{r proj, fig.height= 8, eval = T} -mod_cont1 <- dismo::predict(example_vars, mx) -mod_cont2 <- dismo::predict(example_vars, mx2) -par(mfrow = c(2, 1), mar = c(2, 2, 2, 2)) -plot(mod_cont1, main = "maxent fit with the whole environmental background") -plot(mod_cont2, main = "maxent fit with the buffered environmental background") -``` - -The model with the largest background tends to overfit to the known distribution, while the model fit within the buffer and projected to the largest background predicts more areas outside the known distribution. - -Let's evaluate these models - -```{r, eval = T} -library(dismo) - -eval_mx <- dismo::evaluate(p = prestest, - a = backtest, - model = mx, - x = example_vars) -eval_mx2 <- dismo::evaluate(p = prestest, - a = backtest, - model = mx2, - x = example_vars) - -eval_mx -eval_mx2 -(TSS <- max(eval_mx@TPR + eval_mx@TNR) - 1) -(TSS2 <- max(eval_mx2@TPR + eval_mx2@TNR) - 1) -``` - -The model fit within the buffer has a better predictive capacity according to their TSSvalues. - -Let's compare this as implemented in the package - - -## Maxent with no buffer - -```{r, eval = T} -mx <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[, -1], - predictors = example_vars, - models_dir = "./buffer_res/maxenttest" - ) -no_buf <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/maxenttest", - algo = "maxent") - -no_buf_m <- raster("./buffer_res/maxenttest/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.tif") -plot(no_buf_m) -maps::map(,,add = T) -knitr::include_graphics("./buffer_res/maxenttest/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -no_buf$TSS ``` -## Maxent with a median buffer - -```{r mxbufmedian, eval = T} -md_mx <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[, -1], - predictors = example_vars, - models_dir = "./buffer_res/maxentmax", - buffer_type = "median" - ) -mx_buf <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/maxentmax", - algo = "maxent") - -no_buf_m <- raster("./buffer_res/maxentmax/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.tif") -plot(no_buf_m) -maps::map(,,add = T) -knitr::include_graphics("./buffer_res/maxentmax/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -mx_buf$TSS -``` - -## Maxent with a distance buffer - -```{r mxbufdist, eval = T} -mx_dbuf <- setup_sdmdata(species_name = especies[1], - occurrences = coord1sp[, -1], - predictors = example_vars, - models_dir = "./buffer_res/maxentdist", - buffer_type = "distance", - clean_dupl = T, - clean_na = T, - dist_buf = 4) -mx_dmod <- do_any(species_name = especies[1], - predictors = example_vars, - models_dir = "./buffer_res/maxentdist", - algo = "maxent") - -dbuf_mx <- raster("./buffer_res/maxentdist/Abarema_langsdorffii/present/partitions/maxent_cont_Abarema_langsdorffii_1_1.tif") -plot(dbuf_mx) -maps::map(,,add = T) -knitr::include_graphics("./buffer_res/maxentdist/Abarema_langsdorffii/present/data_setup/sdmdata_Abarema_langsdorffii.png") -mx_dmod$TSS -``` - -