Skip to content

Commit 5743fd1

Browse files
committed
last modifications to updated vignettes
1 parent cf5cbf2 commit 5743fd1

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

vignettes/model_predictions.Rmd

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ knitr::opts_chunk$set(
3030
- [No extrapolation](#no-extrapolation)
3131
- [Binarize predictions](#binarize-predictions)
3232
- [Saving Predictions](#saving-predictions)
33-
- [Detecting changes between single scenarios](#detecting-changes-between-single-scenarios)
33+
- [Detecting changes in predictions](#detecting-changes-in-predictions)
3434

3535
<hr>
3636

@@ -91,7 +91,7 @@ fitted_model_glm
9191

9292
# Model predictions
9393

94-
To predict selected models for a single scenario, you need a `fitted_models` object and the corresponding predictor variables. These predictor variables can be provided as either a `SpatRaster` or a `data.frame`. The names of the variables (or columns in the `data.frame`) must precisely match those used for model calibration or those used when running PCA (if `do_pca = TRUE` was set in the `prepare_data()` function; see [Prepare Data for Model Calibration](prepare_data.html) for more details).
94+
To predict selected models for a single scenario, you need a `fitted_models` object and the corresponding variables. These variables can be provided as either a `SpatRaster` or a `data.frame`. The names of the variables (or columns in the `data.frame`) must precisely match those used for model calibration or those used when running PCA (if `do_pca = TRUE` was set in the `prepare_data()` function; see [Prepare Data for Model Calibration](prepare_data.html) for more details).
9595

9696
<br>
9797

@@ -254,7 +254,7 @@ terra::plot(p_raw$General_consensus$mean, main = "Raw", zlim = c(0, 1))
254254

255255
## Clamping variables
256256

257-
By default, predictions are performed with free extrapolation (`extrapolation_type = "E"`). This can be problematic when the peak of suitability occurs at the extremes of a predictor's range. For example, let's examine the response curve of the Maxnet model for `bio_7` (Temperature Annual Range):
257+
By default, predictions are performed with free extrapolation (`extrapolation_type = "E"`). This can be problematic when the peak of suitability occurs at the extremes of a variable's range. For example, let's examine the response curve of the Maxnet model for `bio_7` (Temperature Annual Range):
258258

259259
```{r response bio_7}
260260
response_curve(models = fitted_model_maxnet, variable = "bio_7",
@@ -327,7 +327,7 @@ terra::plot(new_bio7, main = "Hypothetical bio_7", type = "interval")
327327

328328
Note that when we clamp the variables, regions with extremely low values of (the hypothetical) `bio_7` exhibit lower predicted suitability values compared to when free extrapolation is allowed.
329329

330-
By default, when `extrapolation_type = "EC"` is set, all predictor variables are clamped. You can specify which variables to clamp using the `var_to_restrict` argument.
330+
By default, when `extrapolation_type = "EC"` is set, all variables are clamped. You can specify which variables to clamp using the `var_to_restrict` argument.
331331

332332
<br>
333333

@@ -357,7 +357,7 @@ terra::plot(new_bio7, main = "Hypothetical bio_7", type = "interval")
357357

358358
In this example, a large portion of the predicted area shows zero suitability. This is because, in this hypothetical scenario, much of the region has `bio_7` values lower than those in the training data, which has a minimum of 15ºC. Suitability values greater than zero are only in areas where `bio_7` falls within the training range.
359359

360-
By default, when `extrapolation_type = "NE"` is set, all predictor variables are considered for this process. You can specify a subset of variables to be considered for extrapolation using the `var_to_restrict` argument.
360+
By default, when `extrapolation_type = "NE"` is set, all variables are considered for this process. You can specify a subset of variables to be considered for extrapolation using the `var_to_restrict` argument.
361361

362362
<br>
363363

@@ -438,38 +438,45 @@ p_save <- predict_selected(models = fitted_model_maxnet,
438438
Alternatively, we can use `writeRaster()` to save specific output predictions manually. For example, to save only the mean layer from the general consensus results:
439439

440440
```{r writeRaster, eval = FALSE}
441-
writeRaster(p_maxnet$General_consensus$mean,
442-
filename = file.path(tempdir(), "Mean_consensus.tif"))
441+
terra::writeRaster(p_maxnet$General_consensus$mean,
442+
filename = file.path(tempdir(), "Mean_consensus.tif"))
443443
```
444444

445-
# Detecting changes between single scenarios
445+
<br>
446+
447+
# Detecting changes in predictions
446448

447-
To compare predictions from two single scenarios representing different time periods (e.g., present vs. future or present vs. past), the function `prediction_changes()` can be used to identify areas of **loss (contraction)**, **gain (expansion)**, and **stability (no change)** in suitable conditions.
449+
To compare predictions between two single scenarios representing different time periods (e.g., present vs. future or present vs. past), the function `prediction_changes()` can be used. This function helps to identify **loss (contraction)**, **gain (expansion)**, and **stability (no change)** of suitable areas.
448450

449451
As an example, we will project the fitted model to a **single GCM** representing future climatic conditions:
450452

451453
```{r import var future}
454+
# Read layers representing future conditions
452455
future_var <- terra::rast(system.file("extdata",
453456
"wc2.1_10m_bioc_ACCESS-CM2_ssp585_2081-2100.tif",
454457
package = "kuenm2"))
455-
plot(future_var)
458+
459+
# Plot future layers
460+
terra::plot(future_var)
456461
```
457462

458-
Next, we need to rename the variables so that they match the variable names used when fitting the models:
463+
<br>
464+
465+
Next, we need to rename the variables so that they match the variable names used when fitting the models. After that, we will also apappend the static soil variable to the set of future variables.
459466

460467
```{r}
468+
# renaming layers to match names of variables used to fit the model
461469
names(future_var) <- sub("bio0", "bio", names(future_var))
462470
names(future_var) <- sub("bio", "bio_", names(future_var))
463471
names(var)
464472
names(future_var)
465-
```
466473
467-
We also need to append the static soil variable to the set of future predictors:
468-
469-
```{r}
474+
# Adding soil layer to future variable set
470475
future_var <- c(future_var, var$SoilType)
471476
```
472477

478+
<br>
479+
473480
Now we can generate predictions under future environmental conditions:
474481

475482
```{r}
@@ -479,26 +486,28 @@ p_future <- predict_selected(models = fitted_model_maxnet,
479486
progress_bar = FALSE)
480487
481488
# Plot consensus (mean)
482-
plot(c(p_maxnet$General_consensus$mean,
483-
p_future$General_consensus$mean),
484-
main = c("Present", "Future (SSP 585, 2081-2100)"))
485-
489+
terra::plot(c(p_maxnet$General_consensus$mean,
490+
p_future$General_consensus$mean),
491+
main = c("Present", "Future (SSP 585, 2081-2100)"))
486492
```
487493

488-
To identify how suitable areas change between scenarios, we can use `prediction_changes()`. This function binarizes the predictions using the threshold stored in the fitted models and then classifies each cell as gain, loss, or stable suitability.
494+
<br>
495+
496+
To identify how suitable areas change between scenarios, we can use `prediction_changes()`. This function computes binary layers from the predictions using the threshold stored in the fitted models, compares current and future predictions, and then classifies each cell as gain, loss, or stable.
489497

490-
```{r}
498+
```{r detect changes}
491499
# Compute changes between scenarios
492500
p_changes <- prediction_changes(current_predictions = p_maxnet$General_consensus$mean,
493501
new_predictions = p_future$General_consensus$mean,
494502
fitted_models = fitted_model_maxnet,
495503
predicted_to = "future")
504+
496505
# Plot result
497506
terra::plot(p_changes)
498507
```
499508

500-
In this example, we are comparing current predictions with future predictions, so the argument `predicted_to = "future"` is used. If the comparison were with past predictions, this argument should be set accordingly to ensure that gains and losses are classified correctly.
509+
<br>
501510

502-
The `prediction_changes()` function is designed to compute changes between single scenarios, meaning that the new scenario is represented by one GCM. If projections include multiple GCMs, the function `projection_changes()` should be used instead.
511+
In this example, we are comparing current and future predictions, so we set `predicted_to = "future"`. If a comparison with past predictions is needed, this argument should be set accordingly to ensure that categories of change or stability are assigned correctly.
503512

504-
For more details on projecting models to multiple scenarios, see the vignette [6. Project Models to Multiple Scenarios](model_projections.html).
513+
The `prediction_changes()` function is designed to compute changes between single scenarios, meaning that the new scenario is represented by one set of layers. If projections include multiple GCMs, the function `projection_changes()` should be used instead. For more details on projecting models and detecting changes with summaries across multiple scenarios, see the vignette [6. Project Models to Multiple Scenarios](model_projections.html).

vignettes/model_projections.Rmd

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ knitr::opts_chunk$set(
2121
- [Description](#description)
2222
- [Getting ready](#getting-ready)
2323
- [Fitted models](#fitted-models)
24-
- [Predictors for projections](#predictors-for-projections)
25-
- [Preparing predictors from WorldClim](#preparing-predictors-from-worldclim)
26-
- [Format for renaming](#format-for-renaming)
27-
- [Static variables](#static-variables)
28-
- [Organizing files](#organizing-files)
24+
- [Variables for projections](#variables-for-projections)
25+
- [Variables from WorldClim](#variables-from-worldclim)
26+
- [Format for renaming](#format-for-renaming)
27+
- [Static variables](#static-variables)
28+
- [Organizing files](#organizing-files)
2929
- [Preparing data for projections](#preparing-data-for-projections)
3030
- [Projecting to multiple scenarios](#projecting-to-multiple-scenarios)
3131
- [Importing results from projections](#importing-results-from-projections)
@@ -80,15 +80,15 @@ fitted_model_maxnet
8080

8181
<br>
8282

83-
# Predictors for projections
83+
# Variables for projections
8484

85-
Predicting models for a single scenario requires a single `SpatRaster` object containing the predictor variables (as detailed in [Predict Models to a Single Scenario](model_predictions.html)). In contrast, projecting models to multiple scenarios requires a folder that stores predictor variables for each scenario organized in a certain way.
85+
Predicting models for a single scenario requires a single `SpatRaster` object containing the variables (as detailed in [Predict Models to a Single Scenario](model_predictions.html)). In contrast, projecting models to multiple scenarios requires a folder that stores variables for each scenario organized in a certain way.
8686

8787
To ensure the following automated process can correctly track variables, the data must follow a strict hierarchical directory structure. At the top level, a *Base Directory* serves as the primary container for all project data. Inside this base folder, the first level of organization consists of subfolders for distinct *Time Periods*, such as future years (e.g., "2070", "2100") or paleoclimate eras (e.g., "Mid-holocene", "LGM"). Within each period folder, *if applicable*, you should include subfolders at the second level for each *Emission Scenario* (e.g., "ssp126", "ssp585"). Finally, within each emission scenario or time period folder, the third level should include a separate folder for each *General Circulation Model* (GCM) (e.g., "BCC-CSM2-MR", "MIROC6") to house the actual raster variables. This structured organization enables the function to automatically access and process the data according to period, emission scenario, and GCM.
8888

8989
<br>
9090

91-
## Preparing predictors from WorldClim
91+
## Variables from WorldClim
9292

9393
The package provides a function to import future climate variables downloaded from WorldClim (version 2.1). This function renames the files and organizes them into folders categorized by period/year, emission scenario (Shared Socioeconomic Pathways; SSPs), and General Circulation Model (GCM). This simplifies the preparation of climate data, ensuring all required variables are properly structured for modeling projections.
9494

@@ -97,7 +97,7 @@ To use this function, download the [future raster variables from WorldClim 2.1](
9797
The package also provides an example of raw variables downloaded from WorldClim 2.1. This example includes bioclimatic predictions for the periods "2041-2060" and "2081-2100", for two SSPs (125 and 585) and two GCMs (ACCESS-CM2 and MIROC6), at 10 arc-minutes resolution.
9898

9999
```{r}
100-
# See raster files with future predictors provided as example
100+
# See raster files with future variables provided as example
101101
# The data is located in the "inst/extdata" folder.
102102
in_dir <- system.file("extdata", package = "kuenm2")
103103
list.files(in_dir)
@@ -165,7 +165,7 @@ Now, we can organize and structure the files using the `organize_future_worldcli
165165

166166
<br>
167167

168-
### Format for renaming
168+
## Format for renaming
169169

170170
The argument `name_format` defines the format for renaming variables. The names of the variables in the `SpatRaster` must precisely match those used when preparing data, even if a PCA was performed internally (if `do_pca = TRUE`; see [Prepare Data for Model Calibration](prepare_data.html) for details). If the variables used to create the models were "bio_1", "bio_2", etc., the variables representing other scenarios must be "bio_1", "bio_2", etc. However, if the names don't match exactly, projections can fail (always check uppercase letters or zeros before single-digit numbers (e.g., "Bio_01", "Bio_02", etc.). The function `organize_future_worldclim()` provides four renaming options:
171171

@@ -186,7 +186,7 @@ The variables follow the standards of the first option (`"bio_"`).
186186

187187
<br>
188188

189-
### Static variables
189+
## Static variables
190190

191191
When predicting to other times, some variables could be static (i.e., they remain unchanged in scenarios of projections). The `static_variables` argument allows users to append static variables alongside the Bioclimatic ones. First, let's bring `soilType`, which will remain static in future scenarios (we will use it in a later step).
192192

@@ -200,7 +200,7 @@ soiltype <- var$SoilType
200200

201201
<br>
202202

203-
### Organizing files
203+
## Organizing files
204204

205205
Now, let's organize the WorldClim files with the `organize_future_worldclim()` function:
206206

@@ -264,7 +264,7 @@ After organizing variables, the next step is to create an object that prepares t
264264

265265
To prepare data for model projections across multiple scenarios, storing the paths to the raster layers representing each scenario, we use the function `prepare_projection()`.
266266

267-
In contrast to `predict_selected()`, which requires a `SpatRaster` object, when projecting to multiple scenarios, we need the paths to the folders where the raster files are stored. This includes the variables for the present time, which were used to calibrate and fit the models. Currently, we only have the future climate files. The present-day predictor variables must reside in the same base directory as the processed future variables. Let's copy the raster layers used for model fitting to a folder we can easily direct the function that runs the next step:
267+
In contrast to `predict_selected()`, which requires a `SpatRaster` object, when projecting to multiple scenarios, we need the paths to the folders where the raster files are stored. This includes the variables for the present time, which were used to calibrate and fit the models. Currently, we only have the future climate files. The present-day variables must reside in the same base directory as the processed future variables. Let's copy the raster layers used for model fitting to a folder we can easily direct the function that runs the next step:
268268

269269
```{r copy present}
270270
# Create a "Current_raw" folder in a temporary directory
@@ -300,7 +300,7 @@ pr <- prepare_projection(models = fitted_model_maxnet,
300300

301301
<br>
302302

303-
The `projection_data` object summarizes information about all the scenarios we will project to, and shows the root directory where the predictors are stored:
303+
The `projection_data` object summarizes information about all the scenarios we will project to, and shows the root directory where the variables are stored:
304304

305305
```{r print pr}
306306
pr
@@ -312,7 +312,7 @@ If we check the structure of the `prepared_projection` object, we can see it's a
312312

313313
* Paths to all variables representing distinct scenarios in subfolders.
314314
* The pattern used to identify the format of raster files within the folders (by default, `*.tif`).
315-
* The names of the predictors.
315+
* The names of the variables.
316316
* A list of class `prcomp` if a Principal Component Analysis (PCA) was performed on the set of variables with `prepare_data()`.
317317

318318
```{r str pr, eval = FALSE}
@@ -461,12 +461,7 @@ terra::plot(p_2060_ssp126, cex.main = 0.8)
461461

462462
With the `model_projections` object, we can compute changes in suitable areas (`projection_changes()`), explore variability patterns coming from replicates, parameterizations, and GCMs (`projection_variability()`), and perform analysis of extrapolation risks (`projection_mop()`). For more details, check [Explore Variability and Uncertainty in Projections](variability_and_uncertainty.html).
463463

464-
<br>
465-
466-
```{r par_reset}
467-
# Reset plotting parameters
468-
par(original_par)
469-
```
464+
<br>
470465

471466
# Detecting changes in projections
472467

@@ -599,3 +594,10 @@ changes <- readRDS(file.path(out_dir, "Projection_changes/changes_projections.rd
599594
<br>
600595

601596
After saving, this object can be used to import specific results with the `import_results()` function.
597+
598+
<br>
599+
600+
```{r par_reset}
601+
# Reset plotting parameters
602+
par(original_par)
603+
```

vignettes/variability_and_uncertainty.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ For more details about model projections, see ["Project Models to a Single Scena
8181
# Import calib_results_maxnet
8282
data("fitted_model_maxnet", package = "kuenm2")
8383
84-
# Import path to raster files with future predictors provided as example
84+
# Import path to raster files with future variables provided as example
8585
# The data is located in the "inst/extdata" folder.
8686
in_dir <- system.file("extdata", package = "kuenm2")
8787

0 commit comments

Comments
 (0)