You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/model_predictions.Rmd
+33-24Lines changed: 33 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ knitr::opts_chunk$set(
30
30
-[No extrapolation](#no-extrapolation)
31
31
-[Binarize predictions](#binarize-predictions)
32
32
-[Saving Predictions](#saving-predictions)
33
-
-[Detecting changes between single scenarios](#detecting-changes-between-single-scenarios)
33
+
-[Detecting changes in predictions](#detecting-changes-in-predictions)
34
34
35
35
<hr>
36
36
@@ -91,7 +91,7 @@ fitted_model_glm
91
91
92
92
# Model predictions
93
93
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).
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):
@@ -327,7 +327,7 @@ terra::plot(new_bio7, main = "Hypothetical bio_7", type = "interval")
327
327
328
328
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.
329
329
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.
331
331
332
332
<br>
333
333
@@ -357,7 +357,7 @@ terra::plot(new_bio7, main = "Hypothetical bio_7", type = "interval")
357
357
358
358
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.
359
359
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.
Alternatively, we can use `writeRaster()` to save specific output predictions manually. For example, to save only the mean layer from the general consensus results:
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.
448
450
449
451
As an example, we will project the fitted model to a **single GCM** representing future climatic conditions:
450
452
451
453
```{r import var future}
454
+
# Read layers representing future conditions
452
455
future_var <- terra::rast(system.file("extdata",
453
456
"wc2.1_10m_bioc_ACCESS-CM2_ssp585_2081-2100.tif",
454
457
package = "kuenm2"))
455
-
plot(future_var)
458
+
459
+
# Plot future layers
460
+
terra::plot(future_var)
456
461
```
457
462
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.
459
466
460
467
```{r}
468
+
# renaming layers to match names of variables used to fit the model
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)"))
486
492
```
487
493
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.
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>
501
510
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.
503
512
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).
Copy file name to clipboardExpand all lines: vignettes/model_projections.Rmd
+23-21Lines changed: 23 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -21,11 +21,11 @@ knitr::opts_chunk$set(
21
21
-[Description](#description)
22
22
-[Getting ready](#getting-ready)
23
23
-[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)
29
29
-[Preparing data for projections](#preparing-data-for-projections)
30
30
-[Projecting to multiple scenarios](#projecting-to-multiple-scenarios)
31
31
-[Importing results from projections](#importing-results-from-projections)
@@ -80,15 +80,15 @@ fitted_model_maxnet
80
80
81
81
<br>
82
82
83
-
# Predictors for projections
83
+
# Variables for projections
84
84
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.
86
86
87
87
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.
88
88
89
89
<br>
90
90
91
-
## Preparing predictors from WorldClim
91
+
## Variables from WorldClim
92
92
93
93
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.
94
94
@@ -97,7 +97,7 @@ To use this function, download the [future raster variables from WorldClim 2.1](
97
97
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.
98
98
99
99
```{r}
100
-
# See raster files with future predictors provided as example
100
+
# See raster files with future variables provided as example
101
101
# The data is located in the "inst/extdata" folder.
@@ -165,7 +165,7 @@ Now, we can organize and structure the files using the `organize_future_worldcli
165
165
166
166
<br>
167
167
168
-
###Format for renaming
168
+
## Format for renaming
169
169
170
170
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:
171
171
@@ -186,7 +186,7 @@ The variables follow the standards of the first option (`"bio_"`).
186
186
187
187
<br>
188
188
189
-
###Static variables
189
+
## Static variables
190
190
191
191
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).
192
192
@@ -200,7 +200,7 @@ soiltype <- var$SoilType
200
200
201
201
<br>
202
202
203
-
###Organizing files
203
+
## Organizing files
204
204
205
205
Now, let's organize the WorldClim files with the `organize_future_worldclim()` function:
206
206
@@ -264,7 +264,7 @@ After organizing variables, the next step is to create an object that prepares t
264
264
265
265
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()`.
266
266
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:
268
268
269
269
```{r copy present}
270
270
# Create a "Current_raw" folder in a temporary directory
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:
304
304
305
305
```{r print pr}
306
306
pr
@@ -312,7 +312,7 @@ If we check the structure of the `prepared_projection` object, we can see it's a
312
312
313
313
* Paths to all variables representing distinct scenarios in subfolders.
314
314
* 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.
316
316
* A list of class `prcomp` if a Principal Component Analysis (PCA) was performed on the set of variables with `prepare_data()`.
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).
0 commit comments