Specify a Besag-York-Mollie 2 (BYM2) spatial random effect. BYM2 decomposes the spatial effect into a structured (ICAR) component and an unstructured (IID) component, with a mixing parameter controlling the proportion of variance attributable to spatial structure.
BYM2 is preferred over plain CAR when you want to:
Distinguish structured vs unstructured spatial variation
Have an interpretable spatial fraction parameter
Use the scaling from Riebler et al. (2016)
Arguments
- adjacency
Adjacency matrix (sparse or dense). A symmetric matrix where entry (i,j) is 1 if areas i and j are neighbors, 0 otherwise.
- level
Level at which spatial structure applies:
"group": Spatial effect at the grouping variable level (e.g., sites). Requiresgroup_varto be specified."obs": Spatial effect at the observation level.
- group_var
Name of the grouping variable in data (required if
level = "group").Logical; if TRUE (default), spatial effect enters both numerator and denominator linear predictors identically.
- scale_factor
Scaling factor for the ICAR component. If NULL (default), computed from the adjacency matrix following Riebler et al.
- parameterization
Parameterization of the spatial random effect. Either
"standard"(default) or"collapsed"to marginalize the spatial variance for improved mixing in sparse-graph regimes.
References
Riebler, A., Sorbye, S. H., Simpson, D., & Rue, H. (2016). An intuitive Bayesian spatial model for disease mapping that accounts for scaling. Statistical Methods in Medical Research, 25(4), 1145-1165.
Examples
# Create adjacency matrix for 10 regions (chain structure)
adj <- matrix(0, 10, 10)
for (i in 1:9) {
adj[i, i+1] <- adj[i+1, i] <- 1
}
# Create BYM2 spatial structure
bym2 <- spatial_bym2(adj, level = "group", group_var = "region")
print(bym2)
#> tulpaRatio spatial specification
#> ===========================
#>
#> Type: BYM2
#> Level: group
#> Spatial units: 10
#> Shared: Yes (enters both processes)
#> Group variable: region
#> Scale factor: 0.8169
# \donttest{
# Generate synthetic epidemiological data
set.seed(456)
n_regions <- 10
epi_data <- data.frame(
region = factor(1:n_regions),
age = rnorm(n_regions, 50, 10),
cases = rbinom(n_regions, size = 100, prob = 0.15),
population = rep(100L, n_regions)
)
# Disease mapping with BYM2 spatial smoothing
fit <- tratio(
cases | population ~ age,
spatial = spatial_bym2(adj, level = "group", group_var = "region"),
data = epi_data,
family = ratiod_binomial(),
mode = "laplace"
)
summary(fit)
#> ratio model summary
#> ===================
#>
#> Inference: Structured (Tier 2) via laplace
#> Family: binomial_fixed
#>
#> Fixed effects (numerator):
#> parameter mean sd 2.5% 97.5%
#> num:(Intercept) -2.145 0.699 -3.552 -0.812
#> num:age 0.009 0.014 -0.017 0.037
#>
#> Variance components:
#> parameter mean sd 2.5% 97.5%
#> sigma_spatial 0.191 0.112 0.040 0.476
#> rho 0.560 0.275 0.132 0.983
#>
#> Diagnostics:
#> Converged: TRUE
#> Log marginal: -35.72
#> Posterior samples: 1000
# }