stan_icar | R Documentation |
The intrinsic conditional auto-regressive (ICAR) model for spatial count data. Options include the BYM model, the BYM2 model, and a solo ICAR term.
stan_icar(
formula,
slx,
re,
data,
C,
family = poisson(),
type = c("icar", "bym", "bym2"),
scale_factor = NULL,
prior = NULL,
ME = NULL,
centerx = FALSE,
censor_point,
prior_only = FALSE,
chains = 4,
iter = 2000,
refresh = 500,
keep_all = FALSE,
slim = FALSE,
drop = NULL,
pars = NULL,
control = NULL,
quiet = FALSE,
...
)
formula |
A model formula, following the R formula syntax. Binomial models can be specified by setting the left hand side of the equation to a data frame of successes and failures, as in |
slx |
Formula to specify any spatially-lagged covariates. As in, |
re |
To include a varying intercept (or "random effects") term, alpha_re ~ N(0, alpha_tau) alpha_tau ~ Student_t(d.f., location, scale). Before using this term, read the |
data |
A |
C |
Spatial connectivity matrix which will be used to construct an edge list for the ICAR model, and to calculate residual spatial autocorrelation as well as any user specified |
family |
The likelihood function for the outcome variable. Current options are |
type |
Defaults to "icar" (partial pooling of neighboring observations through parameter |
scale_factor |
For the BYM2 model, optional. If missing, this will be set to a vector of ones. See |
prior |
A named list of parameters for prior distributions (see
.
|
ME |
To model observational uncertainty (i.e. measurement or sampling error) in any or all of the covariates, provide a list of data as constructed by the |
centerx |
To center predictors on their mean values, use |
censor_point |
Integer value indicating the maximum censored value; this argument is for modeling censored (suppressed) outcome data, typically disease case counts or deaths. For example, the US Centers for Disease Control and Prevention censors (does not report) death counts that are nine or fewer, so if you're using CDC WONDER mortality data you could provide |
prior_only |
Draw samples from the prior distributions of parameters only. |
chains |
Number of MCMC chains to estimate. |
iter |
Number of samples per chain. . |
refresh |
Stan will print the progress of the sampler every |
keep_all |
If |
slim |
If |
drop |
Provide a vector of character strings to specify the names of any parameters that you do not want MCMC samples for. Dropping parameters in this way can improve sampling speed and reduce memory usage. The following parameter vectors can potentially be dropped from ICAR models:
If |
pars |
Optional; specify any additional parameters you'd like stored from the Stan model. |
control |
A named list of parameters to control the sampler's behavior. See |
quiet |
Controls (most) automatic printing to the console. By default, any prior distributions that have not been assigned by the user are printed to the console. If |
... |
Other arguments passed to sampling. |
The intrinsic conditional autoregressive (ICAR) model for spatial data was introduced by Besag et al. (1991). The Stan code for the ICAR component of the model and the BYM2 option is from Morris et al. (2019) with adjustments to enable non-binary weights and disconnected graph structures (see Freni-Sterrantino (2018) and Donegan (2021)).
The exact specification depends on the type
argument.
For Poisson models for count data, y, the basic model specification (type = "icar"
) is:
y ~ Poisson(e^{O + \mu + \phi})
\phi \sim ICAR(\tau_s)
\tau_s \sim Gauss(0, 1)
where \mu
contains an intercept and potentially covariates. The spatial trend phi
has a mean of zero and a single scale parameter \tau_s
(which user's will see printed as the parameter named spatial_scale
).
The ICAR prior model is a CAR model that has a spatial autocorrelation parameter \rho
equal to 1 (see stan_car). Thus the ICAR prior places high probability on a very smooth spatially (or temporally) varying mean. This is rarely sufficient to model the amount of variation present in social and health data. For this reason, the BYM model is typically employed.
Often, an observational-level random effect term, theta
, is added to capture (heterogeneous or unstructured) deviations from \mu + \phi
. The combined term is referred to as a convolution term:
convolution = \phi + \theta.
This is known as the BYM model (Besag et al. 1991), and can be specified using type = "bym"
:
y \sim Poisson(e^{O + \mu + \phi + \theta})
\phi \sim ICAR(\tau_s)
\theta \sim Gaussian(0, \tau_{ns})
\tau_s \sim Gaussian(0, 1)
\tau_{ns} \sim Gaussian(0, 1)
The model is named after Besag, York, and Mollié (1991).
Riebler et al. (2016) introduce a variation on the BYM model (type = "bym2"
). This specification combines \phi
and \theta
using a mixing parameter \rho
that controls the proportion of the variation that is attributable to the spatially autocorrelated term \phi
rather than the spatially unstructured term \theta
. The terms share a single scale parameter \tau
:
convolution = [sqrt(\rho * S) * \tilde{\phi} + sqrt(1 - \rho) \tilde{\theta}] * \tau
\tilde{\phi} \sim Gaussian(0, 1)
\tilde{\theta} \sim Gaussian(0, 1)
\tau \sim Gaussian(0, 1)
The terms \tilde{\phi}
, \tilde{\theta}
are standard normal deviates, \rho
is restricted to values between zero and one, and S
is the 'scale_factor' (a constant term provided by the user). By default, the 'scale_factor' is equal to one, so that it does nothing. Riebler et al. (2016) argue that the interpretation or meaning of the scale of the ICAR model depends on the graph structure of the connectivity matrix C
. This implies that the same prior distribution assigned to \tau_s
will differ in its implications if C
is changed; in other words, the priors are not transportable across models, and models that use the same nominal prior actually have different priors assigned to \tau_s
.
Borrowing R
code from Morris (2017) and following Freni-Sterrantino et al. (2018), the following R
code can be used to create the 'scale_factor' S
for the BYM2 model (note, this requires the INLA R package), given a spatial adjacency matrix, C
:
## create a list of data for stan_icar icar.data <- geostan::prep_icar_data(C) ## calculate scale_factor for each of k connected group of nodes k <- icar.data$k scale_factor <- vector(mode = "numeric", length = k) for (j in 1:k) { g.idx <- which(icar.data$comp_id == j) if (length(g.idx) == 1) { scale_factor[j] <- 1 next } Cg <- C[g.idx, g.idx] scale_factor[j] <- scale_c(Cg) }
This code adjusts for 'islands' or areas with zero neighbors, and it also handles disconnected graph structures (see Donegan and Morris 2021). Following Freni-Sterrantino (2018), disconnected components of the graph structure are given their own intercept term; however, this value is added to \phi
automatically inside the Stan model. Therefore, the user never needs to make any adjustments for this term. (To avoid complications from using a disconnected graph structure, you can apply a proper CAR model instead of the ICAR: stan_car
).
Note, the code above requires the scale_c
function; it has package dependencies that are not included in geostan
. To use scale_c
, you have to load the following R
function:
#' compute scaling factor for adjacency matrix, accounting for differences in spatial connectivity #' #' @param C connectivity matrix #' #' @details #' #' Requires the following packages: #' #' library(Matrix) #' library(INLA); #' library(spdep) #' library(igraph) #' #' @source Morris (2017) #' scale_c <- function(C) { geometric_mean <- function(x) exp(mean(log(x))) N = dim(C)[1] Q = Diagonal(N, rowSums(C)) - C Q_pert = Q + Diagonal(N) * max(diag(Q)) * sqrt(.Machine$double.eps) Q_inv = inla.qinv(Q_pert, constr=list(A = matrix(1,1,N),e=0)) scaling_factor <- geometric_mean(Matrix::diag(Q_inv)) return(scaling_factor) }
The ICAR models can also incorporate spatially-lagged covariates, measurement/sampling error in covariates (particularly when using small area survey estimates as covariates), missing outcome data, and censored outcomes (such as arise when a disease surveillance system suppresses data for privacy reasons). For details on these options, please see the Details section in the documentation for stan_glm.
An object of class class geostan_fit
(a list) containing:
Summaries of the main parameters of interest; a data frame
Residual spatial autocorrelation as measured by the Moran coefficient.
an object of class stanfit
returned by rstan::stan
a data frame containing the model data
The edge list representing all unique sets of neighbors and the weight attached to each pair (i.e., their corresponding element in the connectivity matrix C
Spatial connectivity matrix
the user-provided or default family
argument used to fit the model
The model formula provided by the user (not including ICAR component)
The slx
formula
A list with two name elements, formula
and Data
, containing the formula re
and a data frame with columns id
(the grouping variable) and idx
(the index values assigned to each group).
Prior specifications.
If covariates are centered internally (centerx = TRUE
), then x_center
is a numeric vector of the values on which covariates were centered.
A data frame with the name of the spatial parameter ("phi"
if type = "icar"
else "convolution"
) and method (toupper(type)
).
Connor Donegan, connor.donegan@gmail.com
Besag, J. (1974). Spatial interaction and the statistical analysis of lattice systems. Journal of the Royal Statistical Society: Series B (Methodological), 36(2), 192-225.
Besag, J., York, J., and Mollié, A. (1991). Bayesian image restoration, with two applications in spatial statistics. Annals of the Institute of Statistical Mathematics, 43(1), 1-20.
Donegan, Connor and Morris, Mitzi (2021). Flexible functions for ICAR, BYM, and BYM2 models in Stan. Code repository. https://github.com/ConnorDonegan/Stan-IAR
Donegan, Connor (2021b). Building spatial conditional autoregressive (CAR) models in the Stan programming language. OSF Preprints. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.31219/osf.io/3ey65")}.
Freni-Sterrantino, Anna, Massimo Ventrucci, and Håvard Rue (2018). A Note on Intrinsic Conditional Autoregressive Models for Disconnected Graphs. Spatial and Spatio-Temporal Epidemiology, 26: 25–34.
Morris, Mitzi (2017). Spatial Models in Stan: Intrinsic Auto-Regressive Models for Areal Data. https://mc-stan.org/users/documentation/case-studies/icar_stan.html
Morris, M., Wheeler-Martin, K., Simpson, D., Mooney, S. J., Gelman, A., & DiMaggio, C. (2019). Bayesian hierarchical spatial models: Implementing the Besag York Mollié model in stan. Spatial and spatio-temporal epidemiology, 31, 100301.
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.
shape2mat, stan_car, stan_esf, stan_glm, prep_icar_data
data(sentencing)
C <- shape2mat(sentencing, "B")
log_e <- log(sentencing$expected_sents)
fit.bym <- stan_icar(sents ~ offset(log_e),
family = poisson(),
data = sentencing,
type = "bym",
C = C,
chains = 2, iter = 800) # for speed only
# spatial diagnostics
sp_diag(fit.bym, sentencing)
# check effective sample size and convergence
library(rstan)
rstan::stan_ess(fit.bym$stanfit)
rstan::stan_rhat(fit.bym$stanfit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.