Package {SARAR}


Type: Package
Title: Bayesian Estimation of Spatial Autoregressive Models with Flexible Error Distributions
Version: 0.1.0
Author: Luis Hernando Vanegas [aut, cre], Luz Marina Rondón [aut]
Maintainer: Luis Hernando Vanegas <lhvanegasp@unal.edu.co>
Description: Provides Bayesian estimation and inference for spatial autoregressive models with spatial dependence in both the response variable and the error terms. The models accommodate Gaussian, Student-t, Laplace, skew-normal, and skew-Student-t error distributions. Posterior inference is performed using Markov chain Monte Carlo methods. The package also provides posterior summaries, convergence diagnostics, residual analysis, and model comparison criteria, including the Deviance Information Criterion (DIC) and the Watanabe-Akaike Information Criterion (WAIC). See LeSage and Pace (2009, ISBN:9781420064247), "Introduction to Spatial Econometrics".
Imports: Formula, coda, statmod, spdep
License: GPL (≥ 3)
BugReports: https://github.com/lhvanegasp/SARAR/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.3
Depends: R (≥ 3.5)
NeedsCompilation: no
Packaged: 2026-10-29 14:58:34 UTC; 57312
Repository: CRAN
Date/Publication: 2026-11-14 08:20:54 UTC

Deviance Information Criterion

Description

Computes the Deviance Information Criterion.

Usage

DIC(...)

Arguments

...

Arguments passed to methods.

Value

The result depends on the method used. For objects of class 'spreg', 'DIC.spreg()' returns a one-column numeric matrix containing the DIC values for the fitted models.


Deviance Information Criterion for Spatial Regression Models

Description

Computes the Deviance Information Criterion (DIC) for one or more fitted spatial regression models of class '"spreg"'.

Usage

## S3 method for class 'spreg'
DIC(...)

Arguments

...

One or more fitted objects of class '"spreg"' to compare.

Details

The criterion is calculated from the posterior mean deviance and the average deviance over the retained MCMC draws. Smaller values indicate a better trade-off between fit and model complexity, when comparing models fitted to the same response data.

Value

For 'DIC.spreg()', a one-column numeric matrix containing the Deviance Information Criterion (DIC) for each fitted model. Row names identify the model expressions, and the column is named '"DIC"'. Smaller values indicate a better trade-off between model fit and complexity when comparing models fitted to the same data.

See Also

[WAIC()]


Boston Contiguity Spatial Weight Matrix

Description

A row-standardized spatial weight matrix based on polygon contiguity among Boston census tracts.

Usage

W1

Format

A numeric matrix with one row and one column for each census tract in boston.

Details

The matrix is constructed using spdep::poly2nb() to identify polygon neighbors and spdep::nb2mat() with style = "W" to obtain row-standardized weights.

See Also

boston, W2


Boston Four-Nearest-Neighbor Spatial Weight Matrix

Description

A row-standardized spatial weight matrix based on symmetrized four-nearest-neighbor relationships among Boston census tract centroids.

Usage

W2

Format

A numeric matrix with one row and one column for each census tract in boston.

Details

The polygons are projected to EPSG:26986 before their centroids are calculated. The four nearest neighbors are identified using spdep::knearneigh(), converted to a neighbor list using spdep::knn2nb(), and symmetrized using spdep::make.sym.nb(). The matrix is then constructed using spdep::nb2mat() with style = "W".

See Also

boston, W1


Watanabe-Akaike Information Criterion

Description

Computes the Watanabe-Akaike Information Criterion.

Usage

WAIC(...)

Arguments

...

Arguments passed to methods.

Value

The result depends on the method used. For objects of class 'spreg', 'WAIC.spreg()' returns a one-column numeric matrix containing the DIC values for the fitted models.


Watanabe-Akaike Information Criterion for Spatial Regression Models

Description

Computes the Watanabe-Akaike Information Criterion (WAIC) for one or more fitted spatial regression models of class '"spreg"'.

Usage

## S3 method for class 'spreg'
WAIC(...)

Arguments

...

One or more fitted objects of class '"spreg"' to compare.

Details

The calculation uses the likelihood evaluated across retained posterior draws. Smaller values indicate a better expected predictive performance when models are fitted to the same response data.

Value

For 'WAIC.spreg()', a one-column numeric matrix containing the Watanabe-Akaike Information Criterion (WAIC) for each fitted model. Row names identify the model expressions, and the column is named '"WAIC"'. Smaller values indicate better expected predictive performance when comparing models fitted to the same response data.

See Also

[DIC()]


Convert Spatial Regression Draws to MCMC Objects

Description

Converts posterior draws stored in an object of class '"spreg"' into 'coda' MCMC objects, preserving parameter names and the burn-in/thinning metadata.

Usage

## S3 method for class 'spreg'
as.mcmc(x, ...)

Arguments

x

An object of class '"spreg"', typically returned by [spatialReg()].

...

Additional arguments (currently unused).

Value

An object of class '"spregmcmc"', a list containing 'coda::mcmc' objects for the available parameter groups and MCMC metadata.

See Also

[plot.spregmcmc()], [coda::mcmc()]


Boston Census Tracts

Description

An sf object containing polygon geometries and associated attributes for census tracts in Boston, Massachusetts.

Usage

boston

Format

An sf object containing the census tract polygons and their associated attributes.

Details

The data are read from the GeoPackage file boston_tracts.gpkg distributed with the spData package. The original geometries are used to construct the contiguity-based spatial weight matrix W1. A projected version of the geometries, using EPSG:26986, is used to calculate centroids for constructing W2.

Source

The spData package.

See Also

W1, W2


Effective Sample Sizes for Spatial Regression MCMC Draws

Description

Computes the effective sample size for each available parameter chain in an object of class '"spreg"'.

Usage

effectiveSize_SPREG(x)

Arguments

x

An object of class '"spreg"', typically returned by [spatialReg()].

Value

An object of class '"essspreg"', a list containing effective sample sizes for the location coefficients ('beta'), scale coefficients ('gamma'), and any spatial-dependence, skewness, or degrees-of-freedom parameters present in the fitted model.

See Also

[as.mcmc.spreg()], [coda::effectiveSize()]


Geweke Convergence Diagnostics for Spatial Regression MCMC Draws

Description

Computes the Geweke diagnostic z-scores for the parameter chains in an object of class '"spreg"'.

Usage

geweke_diagSPREG(x, frac1 = 0.1, frac2 = 0.5)

Arguments

x

An object of class '"spreg"', typically returned by [spatialReg()].

frac1

Fraction of the chain used for the first segment in the Geweke diagnostic. Defaults to '0.1'.

frac2

Fraction of the chain used for the second segment in the Geweke diagnostic. Defaults to '0.5'.

Value

An object of class '"gdspreg"', a list containing diagnostic z-scores for the available parameters and the two fractions used.

See Also

[coda::geweke.diag()]


Plot MCMC Chains for a Spatial Regression Model

Description

Produces trace and density plots for the parameter chains stored in an object of class '"spregmcmc"'. Separate graphics devices are opened for the available groups of parameters.

Usage

## S3 method for class 'spregmcmc'
plot(
  x,
  trace = TRUE,
  density = TRUE,
  smooth = FALSE,
  bwf = NULL,
  auto.layout = TRUE,
  ask = dev.interactive(),
  ...
)

Arguments

x

An object of class '"spregmcmc"', typically returned by [as.mcmc.spreg()].

trace

Logical; if 'TRUE', display trace plots.

density

Logical; if 'TRUE', display density plots.

smooth

Logical; if 'TRUE', request smoothed density estimates where supported by the plotting method.

bwf

Optional bandwidth adjustment passed to the underlying plotting method.

auto.layout

Logical; whether the underlying plotting method should choose a layout automatically.

ask

Logical; whether to prompt before drawing the next plot.

...

Additional graphical arguments passed to the underlying plot methods.

Value

Called for its plotting side effects.


Print a Spatial Regression Model Summary

Description

Prints the posterior summary produced by [summary.spreg()], displaying location and scale coefficients and any spatial-dependence, skewness, or degrees-of-freedom parameters available in the model.

Usage

## S3 method for class 'summary_spreg'
print(x, digits = max(3, getOption("digits") - 2), ...)

Arguments

x

An object of class '"summary_spreg"' returned by [summary.spreg()].

digits

Number of decimal places used for numeric output. The default is 'max(3, getOption("digits") - 2)'.

...

Additional arguments (currently unused).

Value

The function is called for its side effect of printing the summary; it does not return a value explicitly.


Set and Validate Prior Hyperparameters

Description

Sets default values and validates the hyperparameters of the prior distributions used in Bayesian spatial regression models. The hyperparameters are configured according to the model specification, including the error distribution and the presence of spatial dependence in the response variable and in the error terms.

Usage

priors(prior, dist, hete, em, ar)

Arguments

prior

A named list containing prior hyperparameters. Missing hyperparameters are assigned default values when applicable.

dist

A character string specifying the error distribution. Supported values include "Gaussian", "Student-t", "Laplace", "Skew-normal", and "Skew-Student-t".

hete

A logical value indicating whether the model includes covariate-dependent error variance.

em

A logical value indicating whether spatial dependence is included in the error terms through the parameter \lambda.

ar

A logical value indicating whether spatial dependence is included in the response variable through the parameter \rho.

Details

The function assigns default values to unspecified hyperparameters and checks that the supplied values satisfy the required numerical constraints. For regression location coefficients, the prior is parameterized by its mean and variance. For scale coefficients, the prior depends on whether heteroscedasticity is included in the model. Additional hyperparameters are handled when spatial autoregressive terms, skewness, or Student-t errors are present.

The following prior hyperparameters are supported:

mubeta, sigma2beta

Mean and variance of the Gaussian prior for the location regression coefficients. Defaults are 0 and 10^6, respectively.

mugamma, sigma2gamma

Mean and variance of the Gaussian prior for the coefficients governing the log error variance, used when hete = TRUE. Defaults are 0 and 10^6, respectively.

shape, rate

Shape and rate parameters of the Gamma prior for the error variance when hete = FALSE. Both default to 10^{-6}.

murho, sigma2rho

Mean and variance of the Gaussian prior for the spatial autoregressive parameter \rho, used when ar = TRUE. Defaults are 0 and 10^6, respectively.

mulambda, sigma2lambda

Mean and variance of the Gaussian prior for the spatial error parameter \lambda, used when em = TRUE. Defaults are 0 and 10^6, respectively.

mudelta, sigma2delta

Mean and variance of the Gaussian prior for the skewness parameter \delta, used for the skew-normal and skew-Student-t distributions. Defaults are 0 and 10^6, respectively.

anu, bnu

Shape and rate parameters of the Gamma prior for the degrees of freedom parameter \nu, used for Student-t and skew-Student-t errors. The default for anu is 1. The value of bnu must be supplied and be strictly positive. The value of anu must be greater than or equal to 1.

All Gaussian prior variances must be finite and strictly positive. Prior means must be finite numeric scalars. The Gamma prior parameters must be finite positive numeric scalars, with the additional constraint that anu must be at least 1.

Value

A named list containing the validated prior hyperparameters, including default values assigned to unspecified components.

See Also

spatialReg


Quantile-Type Residuals for a Spatial Regression Model

Description

Calculates quantile-type residuals from a fitted spatial regression model. Posterior means are used for the model parameters. For non-Gaussian error distributions, the residuals are transformed using the corresponding distribution function; simulation is used in the skewed-distribution cases.

Usage

## S3 method for class 'spreg'
residuals(object, ...)

Arguments

object

An object of class '"spreg"', typically returned by [spatialReg()].

...

Additional arguments (currently unused).

Value

A one-column matrix of quantile-type residuals with class '"residuals_spreg"'.

See Also

[spatialReg()]


Bayesian Spatial Regression with Flexible Error Distributions

Description

Fits a spatial regression model by Markov chain Monte Carlo (MCMC). The model can include spatial dependence in the response and in the error terms, heteroscedastic errors, and several symmetric or skewed error distributions.

Usage

spatialReg(
  formula,
  data,
  W1 = NULL,
  W2 = NULL,
  n.sim = 1000,
  n.burnin = 200,
  n.thin = 1,
  prior = list(),
  dist = c("Gaussian", "Student-t", "Laplace", "Skew-normal", "Skew-Student-t")
)

Arguments

formula

A model formula describing the response and location covariates, and optionally the scale covariates as a second right-hand side.

data

An optional data frame containing the variables in the formula. If omitted, variables are looked for in the formula's environment.

W1

Optional spatial weights matrix for spatial dependence in the response. Must be a square matrix of dimension equal to the number of observations, with nonnegative entries, and must be symmetric or row-standardized.

W2

Optional spatial weights matrix for spatial dependence in the error terms. It has the same dimension and structural requirements as 'W1'.

n.sim

Number of MCMC draws to retain after burn-in and thinning.

n.burnin

Number of initial MCMC iterations to discard.

n.thin

Thinning interval for the MCMC chain.

prior

A list for prior specifications. The current implementation initializes its prior settings internally; this argument is retained by the function interface.

dist

Error distribution. One of '"Gaussian"', '"Student-t"', '"Laplace"', '"Skew-normal"', or '"Skew-Student-t"'.

Details

The formula may contain one response, a first right-hand side for the location model, and an optional second right-hand side for the log-scale model, using the multi-part formula syntax supported by [Formula::Formula()]. If the second right-hand side is omitted, a constant scale is used.

The location coefficients are represented by 'beta'. The 'gamma' coefficients govern the log error variance when scale covariates are supplied. The parameter 'rho' describes spatial dependence in the response, and 'lambda' describes spatial dependence in the error terms. The skewness parameter 'delta' is returned for skewed distributions, and the degrees-of-freedom parameter 'nu' is returned for Student-t-based distributions.

Value

An object of class '"spreg"', a list containing posterior draws, MCMC settings, model information, the model frame data, and auxiliary quantities used by other methods. Depending on the fitted model, the posterior draws can include 'beta', 'gamma', 'rho', 'lambda', 'delta', and 'nu'; acceptance rates are also returned for selected updates.

References

LeSage, J. and Pace, R. K. (2009). *Introduction to Spatial Econometrics*. Chapman & Hall/CRC, Boca Raton.

Examples


data(oldcol, package="spdep")
W2 <- W1 <- spdep::nb2mat(COL.nb)
fit1 <- spatialReg(CRIME ~ INC + HOVAL, W1=W1, W2=W2, data=COL.OLD,
                   n.sim=500, n.burnin=500, n.thin=2, dist="Gaussian")
fit2 <- update(fit1, dist="Skew-normal")

################## Model comparison using DIC and WAIC
DIC(fit1,fit2)
WAIC(fit1,fit2)

################## Posterior summaries of the chosen model
summary(fit1)

################## Gaussian Q-Q plot of quantile-type residuals
resi <- residuals(fit1)
dev.new()
qqnorm(resi, pch=20, col="blue")
abline(0,1,lty=3)

################## Computes the Geweke z-scores for convergence diagnostics
geweke_diagSPREG(fit1)

################## Computes the effective sample size
effectiveSize_SPREG(fit1)



Summarize a Fitted Spatial Regression Model

Description

Calculates posterior summaries for a fitted spatial regression model, including means, standard deviations, twice one minus the posterior probability of direction, and highest posterior density (HPD) credible intervals.

Usage

## S3 method for class 'spreg'
summary(object, credible = 0.95, ...)

Arguments

object

An object of class '"spreg"', typically returned by [spatialReg()].

credible

Probability content used for the HPD credible interval. Defaults to '0.95'.

...

Additional arguments passed to or from methods (currently unused).

Value

An object of class '"summary_spreg"' containing summaries of the location and scale coefficients and, when present, spatial dependence, skewness, and degrees-of-freedom parameters.

See Also

[print.summary_spreg()], [spatialReg()]