View source: R/parsing-advanced.R
camodel_mat | R Documentation |
Definition of a stochastic cellular automaton using arrays and matrices
of numbers instead of symbolic expressions (as in camodel
)
camodel_mat(
beta_0 = NULL,
beta_p = NULL,
beta_q = NULL,
all_states,
neighbors,
wrap,
fixed_neighborhood = FALSE,
epsilon = sqrt(.Machine[["double.eps"]]),
build_transitions = TRUE
)
beta_0 |
the constant term of the probabilities of transition (see Details section), given as a square matrix of real numbers |
beta_p |
the coefficient for p[i] for i between 1 and the number of states (see Details section), as a cubic array of numbers |
beta_q |
the coefficient for q[i] for i between 1 and the number of states (see Details section), as a cubic array of numbers |
all_states |
The complete set of states of the model (a character vector). If unspecified, it will be guessed from the transition rules, but it is a good idea to pass it here to make sure the model definition is correct. |
neighbors |
The number of neighbors to use in the cellular automaton (4 for 4-way or von-Neumann neigborhood, or 8 for an 8-way or Moore neighborhood) |
wrap |
If |
fixed_neighborhood |
When not using wrapping around the edges
( |
epsilon |
A small value under which the internal model coefficients values are considered to be equal to zero. The default value should work well here, except if you run models that have extremely small transition probabilities (<1e-8). |
build_transitions |
Transition definitions
(as if produced by |
camodel
will perform badly when defining models with a large number of
transitions or states. This function provides a much faster alternative, but it
assumes that all transitions from state i
to state j
follow the
following form:
P(i \rightarrow j) = \beta^0_{i, j} +
\beta^p_{i, j, 1} p_1 + \beta^p_{i, j, 2} p_2 + ... + \beta^p_{i, j, K} p_K +
\beta^q_{i, j, 1} q_1 + \beta^p_{i, j, 2} q_2 + ... + \beta^p_{i, j, K} q_K
where \beta_0
, \beta^p
and \beta_q
are constant model paramaters,
and p
, q
are the proportion of cells in the landscape and neighborhood
in each state, respectively. K
is the number of states in the model.
\beta^0
is a K x K square matrix, and \beta^p
and
\beta^q
are a K x K x K cubic arrays (with again, K the number of states).
The function will check that this is valid and throw an error if this is not true.
Make sure the values in those arrays are in the right order (it is
array[from, to, coef_for_state_k]
). To make sure the model definition is
correct, you can print the model on the R command line to see if transition
definitions were correctly specified.
The resulting object will be similar to what would be produced by
camodel
, but model-building will be much quicker for complicated models
(lots of states and transitions). You can further improve
performance by setting build_transitions
to FALSE
. In this case, the
generation of symbolic model transitions will be skipped entirely. They are not needed
to run the model, but can be handy to make sure you specified your model correctly.
Disable the generation of symbolic model transitions only when you are sure your model
is correctly-defined.
The diagonal entries in the matrix beta_0
are zero by definition, and the
function will warn if that is not the case. Note that this function will not check
whether the model can yield probabilities below zero or above one.
Durrett, Richard, and Simon A. Levin. 1994. “Stochastic Spatial Models: A User’s Guide to Ecological Applications.” Philosophical Transactions of the Royal Society of London. Series B: Biological Sciences 343 (1305): 329–50. https://doi.org/10.1098/rstb.1994.0028.
camodel
mod_classic <- camodel(
transition(from = "TREE",
to = "EMPTY",
prob = ~ 0.125 + 0.05 * q["EMPTY"] ),
transition(from = "EMPTY",
to = "TREE",
prob = ~ 0.2 * p["TREE"]),
neighbors = 4,
wrap = TRUE,
all_states = c("EMPTY", "TREE"),
check_model = "quick"
)
states <- c("EMPTY", "TREE")
beta_0_mat <- diag(2) * 0
colnames(beta_0_mat) <- rownames(beta_0_mat) <- states
beta_0_mat["TREE", "EMPTY"] <- 0.125
beta_p_array <- array(0, dim = rep(2, 3),
dimnames = list(states, states, states))
beta_p_array["EMPTY", "TREE", "TREE"] <- 0.2
beta_q_array <- array(0, dim = rep(2, 3),
dimnames = list(states, states, states))
beta_q_array["TREE", "EMPTY", "EMPTY"] <- 0.05
mod_mat <- camodel_mat(beta_0 = beta_0_mat,
beta_p = beta_p_array,
beta_q = beta_q_array,
all_states = states,
neighbors = 4,
wrap = TRUE)
# Voter model (defined as in Durrett & Levin, 1994, p342). See also ?ca_library
gamma <- 0.1
kappa <- 30
coefs <- array(0, dim = rep(kappa, 3))
for ( state in seq.int(kappa) ) {
coefs[ , state, state] <- gamma
coefs[state, state, ] <- 0
}
mod <- camodel_mat(
beta_q = coefs,
wrap = TRUE,
all_states = as.character(seq.int(kappa)),
neighbors = 8,
build_transitions = TRUE
)
# This model takes a long time to run for high values of kappa
nrows <- ncols <- 256
initmm <- generate_initmat(mod, rep(1/kappa, kappa),
nrow = nrows, ncol = ncols)
iters <- seq(0, 128)
run_camodel(mod, initmm, iters, control = list(engine = "compiled"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.