Create Spatially or Temporally Varying Effects for INLA
Description
This function transforms covariates identified by pattern or name into varying effect terms of the form:f(unit, covariate, model = 'iid'), which allows covariates to have varying slopes across spatial or temporal units. The output can be used directly in the covariates argument of write_inla_formulas.
cov_varying(
  covariates,
  unit,
  pattern = NULL,
  name = NULL,
  model = "iid",
  constr = FALSE,
  add = FALSE
)Arguments
- covariates: A character vector or a list of character vectors of covariate names. Typically output from- cov_multi,- cov_uni, or- extract_names.
- unit: Character string specifying the unit of variation (e.g.,- "spat_id",- "year").
- pattern: A character vector specifying the prefix pattern(s) to match (e.g., “tmin” matches “tmin”, “tmin.l1”, etc.) for transformation.
- name: Character vector of exact variable names to be transformed.
- model: Character string specifying the INLA model for the varying effect. Currently, only- "iid"is supported.
- constr: Logical. If TRUE it will impose a sum-to-zero constraint to the random effect. Default is FALSE.
- add: Logical; if- TRUE, appends the transformed covariates to the original ones. Default is- FALSE.
Details
- Use patternorname(or both) to specify which covariates to transform.
- The resulting terms use INLA’s f()syntax:f(unit, covariate, model = "iid").
- Currently only supports "iid"models for varying effects.
Returns
A list of character vectors, each including covariates with varying effects. The output is suitable as input for write_inla_formulas.
Examples
data <- data.frame(tmin.l1 = rnorm(10), pdsi.l1 = rnorm(10))
covs <- extract_names(data, pattern = c("tmin", "pdsi"))
covlist <- cov_multi(covs, pattern = c("tmin", "pdsi"))
# Apply varying effect to tmin
cov_varying(covlist, pattern = "tmin", unit = "spat_id")
# Keep original and add varying effect terms
cov_varying(covlist, pattern = "tmin", unit = "spat_id", add = TRUE)