This function is used to create a samc-class object. There are
multiple options for creating this object.
Option 1: Raster or Matrix Maps
samc(data = matrix, absorption = matrix, fidelity = matrix, model = list())
samc(data = SpatRaster, absorption = SpatRaster, fidelity = SpatRaster, model = list())
samc(data = RasterLayer, absorption = RasterLayer, fidelity = RasterLayer, model = list())
The samc-class object can be created from a combination of
resistance (or conductance), absorption, and fidelity data. These different landscape data
inputs must be the same type (a matrix, SpatRaster, or RasterLayer), and have identical
properties, including dimensions, location of NA cells, and CRS (if using
raster inputs).
The data and absorption inputs are always mandatory for this approach. The
fidelity input is optional. If the fidelity input is not provided, then it
is assumed that there is no site fidelity (i.e., individuals will always move
to an adjacent cell each time step).
The model parameter is a mandatory list with the following template:
list(fun = `function`, dir = `numeric`, sym = `logical`). It is used when calculating
the values for the transition matrix. fun must be a mathematical function
taking a single vector x as input. x[1] contains the value for node i and
x[2] contains the value for node j from the data input to samc().
dir determines which neighboring nodes are used andmust be either 4 or 8.
symmetric is an optimization to reduce redundant work for when fun is communative.
It must be either TRUE or FALSE.
Special cases for fun exist to significantly speed up creation of the samc model.
They are selected using specific strings as the value for fun. Currently,
"1/mean(x)" and "x[2]" are implemented. Other special cases can be implemented
on request.
When using raster inputs, SpatRaster objects (from the terra package) are recommended
over RasterLayer objects (from the raster package). Internally, samc is using SpatRaster
objects, which means RasterLayer objects are being converted to SpatRaster objects,
which is a source of memory inefficiency.
Option 2: P Matrix
samc(data = matrix)
samc(data = dgCMatrix)
The data parameter can be used alone to create a samc-class object
directly from a preconstructed P matrix. This matrix must be either a base R
matrix, or a sparse matrix (dgCMatrix format) from the Matrix package. It
must meet the following requirements:
The number of rows must equal the number of columns (a square matrix)
Total absorption must be a single column on the right-hand side of the matrix
At the bottom of the matrix, there must be a row filled with 0's except
for the last element (bottom-right of the matrix diagonal), which must be set to 1
Every disconnected region of the matrix must have at least one non-zero
absorbing value
Each row must sum to 1
All values must be in the range of 0-1
Additionally, the columns and rows of the P matrix may be named (e.g., using
dimnames(), rowname(), colnames(), etc). When specifying origin or dest inputs
to metrics, these names may be used instead of cell numbers. This has the
advantage of making the code for an analysis easier to read and interpret,
which may also help to eliminate unintentional mistakes. There are two
requirements for naming the rows/cols of a P matrix. First, since the P matrix
represents a pairwise matrix, the row and column names must be the same. Second,
there must be no duplicate names. The exception to these rules is the very last
column and the very last row of the P matrix. Since these are not part of the
pairwise transition matrix, they may have whatever names the user prefers.
Additional Options
Additional options can be passed to samc(..., options) as a list. There
are several possible options: list(method = `character`, threads = `numeric`,
override = `logical`, precision = `character`). The use of options varies depending
on other inputs. Some options can be changed after model creation (see samc-class
documentation). It can be omitted for default behaviors.
method controls the type of mathematical algorithm used for the model. It
can be either "direct" (the default), "iter", or "conv". See the
Computation Methods vignette for details.
threads is a positive integer (default 1) that enables parallelization in
certain limited cases. See the Parallel Computing vignette for details.
override is a logical value (default: FALSE) that enables certain memory
intensive calculations. It only applies to the "direct" and "iter" methods.
See the samc-class reference for details.
precision controls the numerical precision of calculations. It can either be
"double" (~15 digits precision; the default) or "double" (~7 digits of
precision). Choosing the lower precision level can lead to significant speed
improvements, and reduces the memory requirements by approximately half. It
currently only applies to the "conv" method.
Additional Information
Depending on the data used to construct the samc-class object, some metrics
may cause crashes. This is a result of the underlying P matrix having specific
properties that make some equations unsolvable. One known case is a P matrix
that represents a disconnected graph, which can lead to the cond_passage()
function crashing. In terms of raster/matrix inputs, a disconnected graph
occurs when one or more pixels/cells are unreachable from other pixels/cells
due to the presence of a full barrier made up of NA values. In a raster, these
may be obvious as islands but can be as inconspicuous as a rogue isolated
pixel. There may be other currently unknown situations that lead to unsolvable
metrics.
Future work is planned towards identifying these issues during the creation of
the samc-class object and handling them appropriately to prevent inadvertent
crashes.
Version 3 Changes
Support for creating samc-class objects from TransitionLayer objects was removed
so that the package is not dependent on gdistance.
Version 2 Changes
Version 1.5.0 officially removed support for the deprecated resistance, tr_fun,
directions, p_mat, latlon, and override arguments. Old
code will have to be updated to the new samc() function structure in order to work.