mcmc_mra {BayesMRA}R Documentation

Bayesian Multi-resolution Spatial Regression


this function runs Markov Chain Monte Carlo to estimate the Bayesian multi-resolution spatial regression model.


  priors = NULL,
  M = 4,
  n_neighbors = 68,
  n_coarse_grid = 10,
  n_padding = 5L,
  n_cores = 1L,
  inits = NULL,
  config = NULL,
  verbose = FALSE,
  use_spam = TRUE,
  n_chain = 1



is a n vector of Gaussian data.


is a n x p matrix of fixed effects (like latitude, elevation, etc)


is a n x 2 matrix of observation locations.


is a list of parameter settings. The list params must contain the following values:

  • n_adapt: A positive integer number of adaptive MCMC iterations.

  • n_mcmc: A positive integer number of total MCMC iterations post adaptation.

  • n_thin: A positive integer number of MCMC iterations per saved sample.

  • n_message: A positive integer number of frequency of iterations to output a progress message. For example, n_message = 50 outputs progress messages every 50 iterations.


is the list of prior settings.


The number of resolutions.


The expected number of neighbors for each interior basis function. This determines the basis radius parameter.


The number of basis functions in one direction (e.g. n_coarse_grid = 10 results in a 10x10 course grid which is further extended by the number of additional padding basis functions given by n_padding.


The number of additional boundary points to add on each boundary. For example, n_padding = 5 will add 5 boundary knots to the both the left and right side of the grid).


is the number of cores for parallel computation using openMP.


is the list of initial values if the user wishes to specify initial values. If these values are not specified, then the initial values will be randomly sampled from the prior.


is the list of configuration values if the user wishes to specify initial values. If these values are not specified, then default a configuration will be used.


Should verbose output be printed? Typically this is only useful for troubleshooting.


is a boolean flag to determine whether the output is a list of spam matrix objects (use_spam = TRUE) or a an n x n sparse Matrix of class "dgCMatrix" use_spam = FALSE (see spam and Matrix packages for details).


is the MCMC chain id. The default is 1.


## genereate the locations
locs <- matrix(runif(20), 10, 2)
## generate some covariates and regression coefficients
X <- cbind(1, matrix(rnorm(30), 10, 3))
beta <- rnorm(ncol(X))

## simulate the MRA process
M <- 2
MRA <- mra_wendland_2d(locs, M = M, n_coarse_grid = 4)
W <-, MRA$W)

n_dims   <- rep(NA, length(MRA$W))
dims_idx <- NULL
for (i in 1:M) {
    n_dims[i] <- ncol(MRA$W[[i]])
    dims_idx  <- c(dims_idx, rep(i, n_dims[i]))
## set up the process precision matrices
Q_alpha <- make_Q_alpha_2d(sqrt(n_dims), c(0.9, 0.8))
Q_alpha_tau2 <- make_Q_alpha_tau2(Q_alpha, tau2 = c(2, 4))

## add in constraints so each resolution has random effects that sum to 0
A_constraint <- sapply(1:M, function(i){
    tmp = rep(0, sum(n_dims))
    tmp[dims_idx == i] <- 1
a_constraint <- rep(0, M)
alpha <- as.vector(spam::rmvnorm.prec.const(
    n = 1,
    mu = rep(0, nrow(W)),
    Q = Q_alpha_tau2,
    A = t(A_constraint),
    a = a_constraint))
## define the data
y <-  as.vector(X %*% beta + W %*% alpha + rnorm(10))

## define the params for MCMC fitting
params <- list(
    n_mcmc    = 5,
    n_adapt   = 5,
    n_thin    = 1,
    n_message = 5)

## define the model priors
priors <- list(
    alpha_tau2   = 1,
    beta_tau2    = 1,
    alpha_sigma2 = 1,
    beta_sigma2  = 1,
    mu_beta      = rep(0, ncol(X)),
    Sigma_beta   = 5 * diag(ncol(X)))

## Fit the MRA model using MCMC
out     <- mcmc_mra(
    y             = y,
    X             = X,
    locs          = locs,
    params        = params,
    priors        = priors,
    M             = 2,
    n_coarse_grid = 4,
    n_cores       = 1L,
    verbose       = FALSE

[Package BayesMRA version 1.0.0 Index]