Create Non-Linear Effects for INLA
Description
This function transforms selected covariates identified by pattern or name into non-linear terms using INLA’s f() syntax. It supports random walk models (rw1, rw2) and allows discretization by quantiles or equal intervals. Transformed covariates are returned as character vectors inside a list ready to be passed to the write_inla_formulas function.
cov_nl(
  covariates,
  pattern = NULL,
  name = NULL,
  model = "rw2",
  method = "quantile",
  n = 10,
  replicate = NULL,
  add = FALSE
)Arguments
- covariates: A character vector or list of character vectors. Usually from- cov_multior- cov_uni.
- pattern: Character vector of patterns to match covariates for transformation (e.g., “tmin” matches “tmin”, “tmin.l1”, etc.).
- name: Character vector of exact covariate names to transform.
- model: Character; either- "rw1"or- "rw2"to specify the non-linear INLA model.
- method: Character; either- "cut"or- "quantile"for discretization. Default is- "quantile".
- n: Integer; number of intervals or quantile bins. Must be >= 2. Default is 10.
- replicate: Optional character string indicating a replicate structure for non-linear effects.
- add: Logical; if- TRUE, adds the transformed covariates to the original ones. Default is- FALSE.
Details
- Use patternorname(or both) to specify which variables to transform.
- The method and n arguments discretize the covariate into evenly populated bins.
- The function supports discretization with either equal-width (cut) or quantile-based (quantile) bins.
- The model argument imposes smoothness on the grouped effect, capturing non-linear trends.
- Non-linear effects are created using .single_non_linear_eff_inla()(internal helper).
Returns
A list of character vectors. This object can be passed to the covariates argument in write_inla_formulas.
See Also
See Bayesian inference with INLA: Smoothing
for more information on smoothing and non-linear effects in R-INLA models.
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 non-linear transformation to tmin variables
cov_nl(covlist, pattern = "tmin", model = "rw2")
# Include original variables along with transformed ones
cov_nl(covlist, pattern = "tmin", model = "rw2", add = TRUE)