View source: R/generateSpFromFun.R
generateSpFromFun  R Documentation 
This function generates a virtual species distribution from a stack of environmental variables and a defined set of responses to each environmental parameter.
generateSpFromFun(
raster.stack,
parameters,
rescale = TRUE,
formula = NULL,
species.type = "multiplicative",
rescale.each.response = TRUE,
plot = FALSE
)
raster.stack 
a SpatRaster object, in which each layer represent an environmental variable. 
parameters 
a list containing the functions of response of the species to environmental variables with their parameters. See details. 
rescale 

formula 
a character string or 
species.type 

rescale.each.response 

plot 

Online tutorial for this function
This function proceeds in two steps:
The response to each environmental variable is calculated with the
functions provided
in parameters
. This results in a suitability of each variable.
By default, each response is rescaled between 0 and 1. Disable with
rescale.each.response = FALSE
The final environmental suitability is calculated according to the
chosen species.type
.
By default, the final suitability is rescaled between 0 and 1.
Disable with
rescale = FALSE
The SpatRaster stack containing environmental variables must have consistent
names,
because they will be checked with the parameters
. For example, they
can be named
var1, var2, etc. Names can be checked and set with names(my.stack)
.
The parameters
have to be carefully created, otherwise the function
will not work:
Either see formatFunctions
to easily create your list of
parameters
Or create a list
defined according to the following template:
list(
var1 = list(fun = 'fun1', args = list(arg1 = ..., arg2 = ...,
etc.)),
var2 = list(fun = 'fun2', args = list(arg1 = ..., arg2 = ...,
etc.)))
It is important to keep the same names in the parameters as in the stack of
environmental
variables. Similarly, argument names must be identical to argument names in
the associated
function (e.g., if you use fun = 'dnorm'
, then args should look like
list(mean = 0, sd = 1)
).
See the example section below for more examples.
Any response function that can be applied to the environmental variables can
be chosen here. Several functions are proposed in this package:
linearFun
, logisticFun
and
quadraticFun
.
Another classical example is the normal distribution:
stats::dnorm()
.
Users can also create and use their own functions very easily.
If rescale.each.response = TRUE
, then the probability response to each
variable will be normalised between 0 and 1 according to the following
formula:
P.rescaled = (P  min(P)) / (max(P)  min (P))
This rescaling has a strong impact on response functions, so users may
prefer to
use rescale.each.response = FALSE
and apply their own rescaling within
their response functions.
a list
with 3 elements:
approach
: the approach used to generate the species,
i.e., "response"
details
: the details and parameters used to generate the
species
suitab.raster
: the raster containing the environmental
suitability of the virtual species
The structure of the virtualspecies object can be seen using str()
Boris Leroy leroy.boris@gmail.com
with help from C. N. Meynard, C. Bellard & F. Courchamp
generateSpFromPCA
to generate a virtual species with
a PCA approach
# Create an example stack with two environmental variables
a < matrix(rep(dnorm(1:100, 50, sd = 25)),
nrow = 100, ncol = 100, byrow = TRUE)
env < c(rast(a * dnorm(1:100, 50, sd = 25)),
rast(a * 1:100))
names(env) < c("variable1", "variable2")
plot(env) # Illustration of the variables
# Easy creation of the parameter list:
# see in real time the shape of the response functions
parameters < formatFunctions(variable1 = c(fun = 'dnorm', mean = 1e04,
sd = 1e04),
variable2 = c(fun = 'linearFun', a = 1, b = 0))
# If you provide env, then you can see the shape of response functions:
parameters < formatFunctions(x = env,
variable1 = c(fun = 'dnorm', mean = 1e04,
sd = 1e04),
variable2 = c(fun = 'linearFun', a = 1, b = 0))
# Generation of the virtual species
sp1 < generateSpFromFun(env, parameters)
sp1
par(mfrow = c(1, 1))
plot(sp1)
# Manual creation of the parameter list
# Note that the variable names are the same as above
parameters < list(variable1 = list(fun = 'dnorm',
args = list(mean = 0.00012,
sd = 0.0001)),
variable2 = list(fun = 'linearFun',
args = list(a = 1, b = 0)))
# Generation of the virtual species
sp1 < generateSpFromFun(env, parameters, plot = TRUE)
sp1
plot(sp1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.