| Title: | Bayesian Spatial Panel Data Models with Convex Combinations of Weight Matrices |
|---|---|
| Description: | Bayesian Markov chain Monte Carlo (MCMC) estimation of spatial panel data models including Spatial Autoregressive (SAR), Spatial Durbin Model (SDM), Spatial Error Model (SEM), Spatial Durbin Error Model (SDEM), and Spatial Lag of X (SLX) specifications with fixed effects. Supports convex combinations of multiple spatial weight matrices and Bayesian Model Averaging (BMA) over subsets of weight matrices. Implements the convex combination spatial weight matrix methodology of Debarsy and LeSage (2021) <doi:10.1080/07350015.2020.1840993> and the Bayesian spatial panel data models of LeSage and Pace (2009, ISBN:9781420064247). |
| Authors: | Mustapha Wasseja Mohammed [aut, cre] |
| Maintainer: | Mustapha Wasseja Mohammed <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.2 |
| Built: | 2026-05-17 07:45:21 UTC |
| Source: | https://github.com/cran/spmixW |
Coefficient Extractor for spmixW Objects
## S3 method for class 'spmixW' coef(object, ...)## S3 method for class 'spmixW' coef(object, ...)
object |
An object of class |
... |
Further arguments (ignored). |
Named numeric vector of posterior means.
Coefficient Extractor for spmixW_bma Objects
## S3 method for class 'spmixW_bma' coef(object, ...)## S3 method for class 'spmixW_bma' coef(object, ...)
object |
An object of class |
... |
Further arguments (ignored). |
Named numeric vector of BMA-averaged posterior means.
Computes log-marginal likelihoods for SLX, SDM, and SDEM specifications
and returns posterior model probabilities. A convenience wrapper around
lmarginal_panel.
compare_models(formula, data, W, id, time = NULL, effects = "twoway", ...)compare_models(formula, data, W, id, time = NULL, effects = "twoway", ...)
formula |
A formula of the form |
data |
A data frame containing all variables referenced in the formula,
plus the |
W |
A spatial weight matrix (N x N) or a list of M weight matrices for convex combination models. When a list is provided, the convex combination variant of the specified model is used automatically. |
id |
Character: name of the column in |
time |
Character or NULL: name of the column identifying time periods.
If |
effects |
Character: fixed-effects specification. One of |
... |
Additional arguments passed to the prior list (e.g.,
|
A data frame with columns: model, log_marginal,
probability.
coords <- cbind(runif(50), runif(50)) W <- make_knw(coords, k = 5) panel <- simulate_panel(N = 50, T = 8, W = W, rho = 0.4, beta = c(1, -0.5), seed = 42) comp <- compare_models(y ~ x1 + x2, data = panel, W = as.matrix(W), id = "region", time = "year", effects = "twoway") print(comp)coords <- cbind(runif(50), runif(50)) W <- make_knw(coords, k = 5) panel <- simulate_panel(N = 50, T = 8, W = W, rho = 0.4, beta = c(1, -0.5), seed = 42) comp <- compare_models(y ~ x1 + x2, data = panel, W = as.matrix(W), id = "region", time = "year", effects = "twoway") print(comp)
Removes fixed effects from panel data via the within transformation.
Data must be sorted by time then by spatial unit: all N regions in period 1,
then all N regions in period 2, etc. (i.e., is stored at
position ).
demean_panel(y, X, N, Time, model = 0L)demean_panel(y, X, N, Time, model = 0L)
y |
Numeric vector of length |
X |
Numeric matrix of dimension |
N |
Integer: number of cross-sectional units (regions). |
Time |
Integer: number of time periods. |
model |
Integer controlling fixed-effects specification:
|
The demeaning follows the standard panel FE within transformation:
Model 1 (region FE): subtract region means across time.
Model 2 (time FE): subtract time-period means across regions.
Model 3 (two-way FE): subtract both region and time means, then add back the grand mean (to avoid double subtraction).
A list with elements:
Demeaned y (length ).
Demeaned X ().
Region means of y (length N); zero if model %in% c(0,2).
Region means of X (); zero if model %in% c(0,2).
Time means of y (length T); zero if model %in% c(0,1).
Time means of X (); zero if model %in% c(0,1).
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
N <- 10; Time <- 5; k <- 2 set.seed(42) y <- rnorm(N * Time) X <- matrix(rnorm(N * Time * k), ncol = k) dm <- demean_panel(y, X, N, Time, model = 3) # Verify the demeaned y has (approximately) zero region and time meansN <- 10; Time <- 5; k <- 2 set.seed(42) y <- rnorm(N * Time) X <- matrix(rnorm(N * Time * k), ncol = k) dm <- demean_panel(y, X, N, Time, model = 3) # Verify the demeaned y has (approximately) zero region and time means
Rapidly evaluates using pre-computed
trace terms from log_det_taylor.
eval_taylor_lndet(traces, rho, gamma, order = NULL)eval_taylor_lndet(traces, rho, gamma, order = NULL)
traces |
Output from |
rho |
Numeric scalar: spatial autoregressive parameter. |
gamma |
Numeric vector of length M: convex weights (must sum to 1). |
order |
Integer: Taylor order to use (default: use all available).
Must be <= |
Scalar: approximate .
Returns a one-row data frame of model-level summary statistics.
glance.spmixW(x, ...)glance.spmixW(x, ...)
x |
An object of class |
... |
Additional arguments (ignored). |
A one-row data frame.
Glance Method for spmixW_bma Objects
glance.spmixW_bma(x, ...)glance.spmixW_bma(x, ...)
x |
An object of class |
... |
Additional arguments (ignored). |
A one-row data frame.
Computes log-marginal likelihoods for three spatial panel specifications
(SLX, SDM, SDEM) under diffuse priors on and a
uniform prior on . Used for Bayesian model comparison.
lmarginal_panel(y, X, W, N, Time, prior = list())lmarginal_panel(y, X, W, N, Time, prior = list())
y |
Numeric vector of length NT (should be demeaned if FE are used). |
X |
Numeric matrix NT x k (WITHOUT intercept for FE models). |
W |
Spatial weight matrix (N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
prior |
Optional list with fields:
|
The SLX model has no spatial parameter, so its marginal is analytic.
The SDM and SDEM marginals require numerical integration over
(or ) using the trapezoid rule on a fine grid.
For SDM, the concentrated log-marginal profile is:
where are OLS residuals from y and Wy on the SDM design matrix.
A list with:
Log-marginal for SLX model.
Log-marginal for SDM model (integrated over rho).
Log-marginal for SDEM model (integrated over lambda).
Vector of all three log-marginals.
Posterior model probabilities (assuming equal priors).
LeSage, J.P. (2014). "What Regional Scientists Need to Know about Spatial Econometrics." Review of Regional Studies, 44(1), 13-32.
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
set.seed(1) N <- 30; Time <- 10 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) X <- matrix(rnorm(N * Time * 2), ncol = 2) y <- rnorm(N * Time) res <- lmarginal_panel(y, X, W, N, Time) res$probsset.seed(1) N <- 30; Time <- 10 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) X <- matrix(rnorm(N * Time * 2), ncol = 2) y <- rnorm(N * Time) res <- lmarginal_panel(y, X, W, N, Time) res$probs
Computes for a grid of values
using the eigenvalues of . This is exact but requires computing
all eigenvalues of , which is .
log_det_exact(W, rmin = -1, rmax = 1, grid_step = 0.001)log_det_exact(W, rmin = -1, rmax = 1, grid_step = 0.001)
W |
A square (sparse or dense) spatial weight matrix ( |
rmin |
Numeric scalar: lower bound of |
rmax |
Numeric scalar: upper bound of |
grid_step |
Numeric scalar: grid spacing (default |
Given eigenvalues of :
The result is pre-computed on a fine grid and used for griddy Gibbs sampling of the spatial autoregressive parameter.
A matrix with two columns:
Grid of values.
Corresponding values.
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
W <- matrix(c(0, 0.5, 0.5, 0, 0.5, 0, 0, 0.5, 0.5, 0, 0, 0.5, 0, 0.5, 0.5, 0), 4, 4) detval <- log_det_exact(W) head(detval)W <- matrix(c(0, 0.5, 0.5, 0, 0.5, 0, 0, 0.5, 0.5, 0, 0, 0.5, 0, 0.5, 0.5, 0), 4, 4) detval <- log_det_exact(W) head(detval)
Approximates using the stochastic trace estimator
of Barry and Pace (1999). Suitable for large spatial weight matrices where
exact eigenvalue computation is infeasible.
log_det_mc(W, rmin = -1, rmax = 1, grid_step = 0.001, order = 50L, iter = 30L)log_det_mc(W, rmin = -1, rmax = 1, grid_step = 0.001, order = 50L, iter = 30L)
W |
A square sparse spatial weight matrix ( |
rmin |
Numeric scalar: lower bound of |
rmax |
Numeric scalar: upper bound of |
grid_step |
Numeric scalar: grid spacing (default |
order |
Integer: number of terms in the Taylor expansion (default |
iter |
Integer: number of random vectors for trace estimation (default |
Uses the identity:
The traces are estimated stochastically:
where are random vectors.
The second-order trace is computed exactly as
for greater accuracy.
A matrix with two columns:
Grid of values.
Corresponding approximate values.
Pace, R.K. and Barry, J.P. (1997). "Quick Computation of Spatial Autoregressive Estimators." Geographical Analysis, 29(3), 232-247.
Barry, R.P. and Pace, R.K. (1999). "Monte Carlo estimates of the log determinant of large sparse matrices." Linear Algebra and its Applications, 289, 41-54.
library(Matrix) N <- 100 W <- sparseMatrix( i = c(1:N, 1:(N-1)), j = c(c(2:N, 1), 2:N), x = rep(0.5, 2*N - 1), dims = c(N, N) ) detval <- log_det_mc(W) head(detval)library(Matrix) N <- 100 W <- sparseMatrix( i = c(1:N, 1:(N-1)), j = c(c(2:N, 1), 2:N), x = rep(0.5, 2*N - 1), dims = c(N, N) ) detval <- log_det_mc(W) head(detval)
Pre-computes trace cross-terms for the Taylor series approximation to
where
.
log_det_taylor(Wlist, max_order = 4L)log_det_taylor(Wlist, max_order = 4L)
Wlist |
A list of M sparse or dense weight matrices, each |
max_order |
Integer: maximum Taylor order (default 4, supports 2-8).
Higher orders give more accurate log-det approximations at the cost of
more pre-computation time. The number of trace terms at order p is
|
The Taylor approximation is:
(the p=1 term vanishes for zero-diagonal W).
Since , the trace expands via multinomial:
These cross-terms are pre-computed once (expensive for high orders) and
then rapidly evaluated for any via Kronecker products.
For M=3 weight matrices, the number of trace terms by order:
| Order | Terms |
| 4 | 81 |
| 5 | 243 |
| 6 | 729 |
| 7 | 2187 |
| 8 | 6561 |
A list with elements:
A named list where traces[[p]] is a vector of
length containing all
cross-terms for order p (p = 2, ..., max_order).
The maximum Taylor order stored.
Number of weight matrices.
Dimension of each weight matrix.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
N <- 20 W1 <- matrix(0, N, N); W2 <- matrix(0, N, N) for (i in 1:N) { W1[i, (i %% N) + 1] <- 1; W2[i, ((i-2) %% N) + 1] <- 1 } W1 <- W1 / rowSums(W1); W2 <- W2 / rowSums(W2) traces <- log_det_taylor(list(W1, W2), max_order = 6) eval_taylor_lndet(traces, rho = 0.5, gamma = c(0.7, 0.3))N <- 20 W1 <- matrix(0, N, N); W2 <- matrix(0, N, N) for (i in 1:N) { W1[i, (i %% N) + 1] <- 1; W2[i, ((i-2) %% N) + 1] <- 1 } W1 <- W1 / rowSums(W1); W2 <- W2 / rowSums(W2) traces <- log_det_taylor(list(W1, W2), max_order = 6) eval_taylor_lndet(traces, rho = 0.5, gamma = c(0.7, 0.3))
Log-Likelihood for spmixW Objects
## S3 method for class 'spmixW' logLik(object, ...)## S3 method for class 'spmixW' logLik(object, ...)
object |
An object of class |
... |
Further arguments (ignored). |
The log-likelihood evaluated at the posterior mean.
Constructs a row-normalised binary spatial weight matrix based on k-nearest neighbours from a coordinate matrix.
make_knw(coords, k, row_normalise = TRUE)make_knw(coords, k, row_normalise = TRUE)
coords |
An |
k |
Integer: number of nearest neighbours. |
row_normalise |
Logical: if |
Euclidean distances are computed between all pairs of coordinates. For each
unit, the k closest neighbours receive weight 1 (before normalisation).
The matrix is generally asymmetric (i may be j's neighbour without j being
i's neighbour).
A sparse weight matrix. If row_normalise = TRUE,
each row sums to 1.
set.seed(1) coords <- cbind(runif(20), runif(20)) W <- make_knw(coords, k = 5) dim(W) range(Matrix::rowSums(W)) # all 1 if row-normalisedset.seed(1) coords <- cbind(runif(20), runif(20)) W <- make_knw(coords, k = 5) dim(W) range(Matrix::rowSums(W)) # all 1 if row-normalised
Converts a vector of log-marginal likelihoods to posterior model probabilities assuming equal prior model probabilities.
model_probs(lmarginal)model_probs(lmarginal)
lmarginal |
Numeric vector of log-marginal likelihood values, one per model. |
Assumes equal prior probabilities across models. Computes:
where is the log-marginal likelihood for model .
The subtraction of the maximum prevents numerical overflow.
A numeric vector of posterior model probabilities summing to 1.
LeSage, J.P. (2014). "What Regional Scientists Need to Know about Spatial Econometrics." Review of Regional Studies, 44(1), 13-32.
lm <- c(-100, -98, -105) model_probs(lm)lm <- c(-100, -98, -105) model_probs(lm)
Divides each row of a spatial weight matrix by its row sum so that all rows sum to one. Zero-sum rows (isolates) are left unchanged.
normw(W)normw(W)
W |
A square matrix or sparse matrix (class |
A sparse matrix of the same dimension with rows summing to 1 (except for isolate rows that remain zero).
W <- matrix(c(0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0), 4, 4) Wn <- normw(W) Matrix::rowSums(Wn) # all onesW <- matrix(c(0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0), 4, 4) Wn <- normw(W) Matrix::rowSums(Wn) # all ones
Estimates a Bayesian panel regression model (no spatial component) with optional spatial and/or time-period fixed effects via MCMC sampling. Supports both homoscedastic and heteroscedastic error structures.
ols_panel(y, X, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())ols_panel(y, X, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())
y |
Numeric vector of length |
X |
Numeric matrix of dimension |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total number of MCMC draws (including burn-in). |
nomit |
Integer: number of initial draws to discard as burn-in. |
prior |
A list of prior hyperparameters:
|
The model is:
where , .
MCMC sampling steps:
Draw from the multivariate normal
posterior (conjugate update).
Draw from the inverse-gamma posterior.
(Heteroscedastic only) Draw each
from .
An S3 object of class "spmixW" containing:
Posterior mean of (length k).
Posterior mean of .
coda::mcmc object:
matrix of retained draws.
coda::mcmc object: retained draws.
Posterior mean of the variance weights
(length NT; all ones if homoscedastic).
Fitted values including fixed effects.
Residuals .
R-squared.
Squared correlation between actual and fitted demeaned values.
Spatial fixed effects (if model %in% c(1,3)).
Time fixed effects (if model %in% c(2,3)).
Estimated intercept.
Posterior t-statistics (mean / sd of draws).
Metadata.
Wall-clock time in seconds.
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
Geweke, J. (1993). "Bayesian Treatment of the Independent Student-t Linear Model." Journal of Applied Econometrics, 8(S1), S19-S40.
set.seed(123) N <- 20; Time <- 10; k <- 2 beta_true <- c(1.5, -0.8) X <- matrix(rnorm(N * Time * k), ncol = k) y <- X %*% beta_true + rnorm(N * Time, sd = 0.5) res <- ols_panel(y, X, N, Time, ndraw = 2000, nomit = 500, prior = list(model = 1, rval = 4)) print(res)set.seed(123) N <- 20; Time <- 10; k <- 2 beta_true <- c(1.5, -0.8) X <- matrix(rnorm(N * Time * k), ncol = k) y <- X %*% beta_true + rnorm(N * Time, sd = 0.5) res <- ols_panel(y, X, N, Time, ndraw = 2000, nomit = 500, prior = list(model = 1, rval = 4)) print(res)
Produces a 2x2 diagnostic plot layout: trace and density for the spatial parameter, trace for sigma^2, and an effects comparison (or gamma densities for convex combination models).
## S3 method for class 'spmixW' plot(x, ...)## S3 method for class 'spmixW' plot(x, ...)
x |
An object of class |
... |
Further arguments (ignored). |
Invisible NULL.
Produces a multi-panel plot showing model probabilities and BMA posterior densities.
## S3 method for class 'spmixW_bma' plot(x, ...)## S3 method for class 'spmixW_bma' plot(x, ...)
x |
An object of class |
... |
Further arguments (ignored). |
Invisible NULL.
Print Method for spmixW Objects
## S3 method for class 'spmixW' print(x, digits = 4, ...)## S3 method for class 'spmixW' print(x, digits = 4, ...)
x |
An object of class |
digits |
Integer: number of significant digits to print. |
... |
Further arguments (ignored). |
Invisibly returns x.
Print Method for spmixW_bma Objects
## S3 method for class 'spmixW_bma' print(x, digits = 4, ...)## S3 method for class 'spmixW_bma' print(x, digits = 4, ...)
x |
An object of class |
digits |
Integer: significant digits. |
... |
Further arguments (ignored). |
Invisibly returns x.
Estimates SAR convex combination models for all non-empty
subsets of M weight matrices and averages estimates by posterior model
probabilities computed from log-marginal likelihoods.
sar_conv_bma( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sar_conv_bma( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: MCMC draws per model (recommend >= 10000). |
nomit |
Integer: burn-in draws per model. |
prior |
List of prior hyperparameters (passed to each model). |
An S3 object of class "spmixW_bma" containing:
BMA posterior mean of beta.
BMA posterior mean of rho.
BMA posterior mean of gamma (M x 1).
BMA posterior mean of sigma^2.
Vector of posterior model probabilities.
Vector of log-marginal likelihoods per model.
BMA-averaged MCMC draws.
BMA-averaged effects draws.
Matrix showing which W's are in each model.
Number of models evaluated.
List of per-model results summaries.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
Estimates a SAR panel model where the spatial weight matrix is a convex
combination with
on the unit simplex. Uses Metropolis-Hastings for both and
, with Taylor series log-determinant approximation.
sar_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sar_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (recommend >= 10000). |
nomit |
Integer: burn-in draws. |
prior |
List of prior hyperparameters. See Details. |
The model is:
The MCMC sampler draws:
from multivariate normal (conjugate).
from inverse-gamma.
via Metropolis-Hastings random walk
with adaptive tuning (target acceptance 40-60\
via Metropolis-Hastings with reversible
jump proposal on the simplex (matching LeSage's coin-flip method).
Pre-computes so that
for any :
where .
An S3 object of class "spmixW" with:
Posterior mean of beta.
Posterior mean of rho.
Posterior mean of gamma (M x 1).
MCMC draws for beta, rho, sigma, gamma.
Acceptance rate for rho MH sampler.
Acceptance rate for gamma MH sampler.
Effects estimates.
Pre-computed Taylor traces (for reuse).
The convex combination models use a Taylor series approximation for the
log-determinant. The default order is 6 (extending the original order 4
of Debarsy and LeSage 2021). Approximation accuracy improves with larger N: at N=500 and
rho=0.6, expect approximately 0.13 upward bias in rho; at N=3000+ the
bias is negligible (<0.02). Users can control this via
prior$taylor_order. For small-N applications where precise rho
estimation is critical, compare results against sar_panel
with a known single W matrix as a benchmark.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
LeSage, J.P. (2020). "Fast MCMC estimation of multiple W-matrix spatial regression models and Metropolis-Hastings Monte Carlo log-marginal likelihoods." Journal of Geographical Systems, 22(1), 47-75.
Estimates a Bayesian Spatial Autoregressive (SAR) panel model via MCMC:
with griddy Gibbs sampling for and LeSage-Pace scalar
effects estimates (direct, indirect, total).
sar_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())sar_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())
y |
Numeric vector of length |
X |
Numeric matrix |
W |
Spatial weight matrix ( |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (including burn-in). |
nomit |
Integer: burn-in draws to discard. |
prior |
List of prior hyperparameters (see
|
The griddy Gibbs sampler for evaluates the conditional
log-posterior on a fine grid using the concentrated likelihood
(beta integrated out), then uses inverse-CDF sampling via the
trapezoid rule.
Effects are computed using the scalar summary measures of
LeSage and Pace (2009, Ch. 4) based on stochastic trace estimates
of powers of .
An S3 object of class "spmixW" with all fields from
ols_panel plus:
Posterior mean of .
coda::mcmc of draws.
Matrix (p x 5): direct effects (mean, t-stat, p-value, lower, upper).
Matrix (p x 5): indirect effects.
Matrix (p x 5): total effects.
MCMC draws of effects.
Log-determinant grid (for reuse in subsequent calls).
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
set.seed(1) N <- 30; Time <- 10; rho_true <- 0.5 coords <- cbind(runif(N), runif(N)) W <- normw(make_knw(coords, k = 5, row_normalise = FALSE)) W <- as.matrix(W) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) y <- solve(diag(N*Time) - rho_true * Wbig) %*% (X %*% c(1, -0.5) + rnorm(N*Time)) res <- sar_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000, prior = list(model = 0, rval = 0)) print(res)set.seed(1) N <- 30; Time <- 10; rho_true <- 0.5 coords <- cbind(runif(N), runif(N)) W <- normw(make_knw(coords, k = 5, row_normalise = FALSE)) W <- as.matrix(W) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) y <- solve(diag(N*Time) - rho_true * Wbig) %*% (X %*% c(1, -0.5) + rnorm(N*Time)) res <- sar_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000, prior = list(model = 0, rval = 0)) print(res)
Bayesian Model Averaging for SDEM Convex Combination Panel
sdem_conv_bma( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sdem_conv_bma( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: MCMC draws per model (recommend >= 10000). |
nomit |
Integer: burn-in draws per model. |
prior |
List of prior hyperparameters (passed to each model). |
An S3 object of class "spmixW_bma".
Estimates a SDEM panel model with :
sdem_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sdem_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (recommend >= 10000). |
nomit |
Integer: burn-in draws. |
prior |
List of prior hyperparameters. See Details. |
Augments X with [X, W1*X, W2*X, ..., WM*X] and calls
sem_conv_panel.
For SDEM, direct effects are the coefficients, indirect effects
are the coefficients summed across W matrices, and total =
direct + indirect. The spatial error parameter does not affect effects.
An S3 object of class "spmixW" with convex combination fields
plus SDEM-specific effects.
See sar_conv_panel for details on Taylor order and accuracy.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
Estimates a Spatial Durbin Error Model (SDEM) panel via MCMC. This is a
thin wrapper around sem_panel that augments X with WX.
sdem_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())sdem_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())
y |
Numeric vector of length |
X |
Numeric matrix |
W |
Spatial weight matrix ( |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (including burn-in). |
nomit |
Integer: burn-in draws to discard. |
prior |
List of prior hyperparameters (see
|
For SDEM, effects decomposition is straightforward (no spatial multiplier
on X, unlike SDM): direct effects are , indirect effects are
, and total = . The spatial error
parameter does not affect the effects decomposition.
An S3 object of class "spmixW" with SEM fields plus
SDEM-specific effects:
Direct effects = coefficients on X ().
Indirect effects = coefficients on WX ().
Total = direct + indirect.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
set.seed(1) N <- 30; Time <- 10; lambda_true <- 0.5 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) WX <- Wbig %*% X u <- solve(diag(N*Time) - lambda_true * Wbig) %*% rnorm(N*Time) y <- X %*% c(1, -0.5) + WX %*% c(0.3, 0.2) + u res <- sdem_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000) print(res)set.seed(1) N <- 30; Time <- 10; lambda_true <- 0.5 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) WX <- Wbig %*% X u <- solve(diag(N*Time) - lambda_true * Wbig) %*% rnorm(N*Time) y <- X %*% c(1, -0.5) + WX %*% c(0.3, 0.2) + u res <- sdem_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000) print(res)
Bayesian Model Averaging for SDM Convex Combination Panel
sdm_conv_bma( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sdm_conv_bma( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: MCMC draws per model (recommend >= 10000). |
nomit |
Integer: burn-in draws per model. |
prior |
List of prior hyperparameters (passed to each model). |
An S3 object of class "spmixW_bma".
Estimates a SDM panel model with :
sdm_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sdm_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (recommend >= 10000). |
nomit |
Integer: burn-in draws. |
prior |
List of prior hyperparameters. See Details. |
Unlike the standard SDM wrapper, the WX terms are pre-computed for each
individual since the convex combination changes at
each MCMC iteration. The augmented X is [X, W1*X, W2*X, ..., WM*X].
Internally augments X with [X, W1*X, W2*X, ..., WM*X] and
then calls sar_conv_panel for estimation. The effects decomposition accounts for all M
sets of WX coefficients.
An S3 object of class "spmixW" with convex combination fields.
See sar_conv_panel for details on Taylor order and accuracy.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
Estimates a Spatial Durbin Model (SDM) panel via MCMC. This is a thin
wrapper around sar_panel that augments the X matrix with
WX before calling the SAR estimator, then computes SDM-specific effects.
sdm_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())sdm_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())
y |
Numeric vector of length |
X |
Numeric matrix |
W |
Spatial weight matrix ( |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (including burn-in). |
nomit |
Integer: burn-in draws to discard. |
prior |
List of prior hyperparameters (see
|
Internally, this function augments X with WX and calls
sar_panel, following the delegation pattern of
LeSage and Pace (2009, Ch. 10). The SDM effects computation differs from SAR because the
spatial multiplier acts on both
and , yielding different direct/indirect decompositions.
An S3 object of class "spmixW" with all SAR fields.
The beta vector contains (length
or if X has an intercept). Effects are SDM-style: both
and contribute to the spatial multiplier.
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
set.seed(1) N <- 30; Time <- 10; rho_true <- 0.4 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) WX <- Wbig %*% X y <- solve(diag(N*Time) - rho_true * Wbig) %*% (X %*% c(1, -0.5) + WX %*% c(0.3, 0.2) + rnorm(N*Time)) res <- sdm_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000) print(res)set.seed(1) N <- 30; Time <- 10; rho_true <- 0.4 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) WX <- Wbig %*% X y <- solve(diag(N*Time) - rho_true * Wbig) %*% (X %*% c(1, -0.5) + WX %*% c(0.3, 0.2) + rnorm(N*Time)) res <- sdm_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000) print(res)
Estimates a SEM panel model with :
sem_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )sem_conv_panel( y, X, Wlist, N, Time, ndraw = 25000L, nomit = 5000L, prior = list() )
y |
Numeric vector of length NT. |
X |
Numeric matrix NT x k. |
Wlist |
A list of M spatial weight matrices (each N x N). |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (recommend >= 10000). |
nomit |
Integer: burn-in draws. |
prior |
List of prior hyperparameters. See Details. |
Follows the same MH structure as sar_conv_panel but the
spatial parameter enters the error structure. The pre-computed arrays
ys and xs represent filtering.
An S3 object of class "spmixW" with gamma, rho (lambda),
acceptance rates, and MCMC draws.
See sar_conv_panel for details on Taylor order and accuracy.
Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial autoregressive models based on convex combinations of different types of connectivity matrices." Journal of Business & Economic Statistics, 40(2), 547-558.
Estimates a Bayesian Spatial Error Model (SEM) panel via MCMC:
with griddy Gibbs sampling for .
sem_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())sem_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())
y |
Numeric vector of length |
X |
Numeric matrix |
W |
Spatial weight matrix ( |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (including burn-in). |
nomit |
Integer: burn-in draws to discard. |
prior |
List of prior hyperparameters (see
|
The SEM differs from SAR in that the spatial parameter enters the error
structure. The MCMC sampler filters both y and X by
before drawing . The griddy Gibbs for evaluates
the concentrated log-posterior on a grid, where for each grid point the
filtered data and are
used to compute the residual sum of squares.
An S3 object of class "spmixW" with fields similar to
ols_panel plus:
Posterior mean of (spatial error parameter).
coda::mcmc of draws.
Log-determinant grid.
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
set.seed(1) N <- 30; Time <- 10; lambda_true <- 0.5 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) u <- solve(diag(N*Time) - lambda_true * Wbig) %*% rnorm(N*Time) y <- X %*% c(1, -0.5) + u res <- sem_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000, prior = list(model = 0, rval = 0)) print(res)set.seed(1) N <- 30; Time <- 10; lambda_true <- 0.5 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) u <- solve(diag(N*Time) - lambda_true * Wbig) %*% rnorm(N*Time) y <- X %*% c(1, -0.5) + u res <- sem_panel(as.numeric(y), X, W, N, Time, ndraw = 5000, nomit = 2000, prior = list(model = 0, rval = 0)) print(res)
Generates synthetic spatial panel data from a SAR/SDM/SEM/SDEM DGP,
returning a ready-to-use data frame for spmodel.
simulate_panel( N, Time = 10L, W, gamma = NULL, rho = 0, beta, theta = NULL, sigma2 = 1, effects = "twoway", seed = NULL )simulate_panel( N, Time = 10L, W, gamma = NULL, rho = 0, beta, theta = NULL, sigma2 = 1, effects = "twoway", seed = NULL )
N |
Integer: number of cross-sectional units (regions). |
Time |
Integer: number of time periods (use 1 for cross-sectional). |
W |
A single N x N weight matrix, or a list of M weight matrices for convex combination DGPs. |
gamma |
Numeric vector of convex weights (required when |
rho |
Numeric: spatial autoregressive parameter (default 0). |
beta |
Numeric vector of true regression coefficients (length k). |
theta |
Numeric vector of WX coefficients for SDM/SDEM DGPs
(default NULL). Must have same length as |
sigma2 |
Numeric: error variance (default 1). |
effects |
Character: fixed-effects specification.
|
seed |
Integer or NULL: random seed for reproducibility. |
The DGP is:
where (or just W if a single matrix),
is omitted for SAR/SEM DGPs, and FE are generated as
(region) and (time).
A data frame with columns:
Integer region identifier (1 to N).
Integer time period identifier (omitted if Time = 1).
Simulated response variable.
Simulated predictor variables (standard normal).
The data frame is sorted by time then region, ready for
spmodel.
coords <- cbind(runif(80), runif(80)) W1 <- make_knw(coords, k = 4) W2 <- make_knw(coords, k = 8) panel <- simulate_panel( N = 80, T = 10, W = list(W1, W2), gamma = c(0.7, 0.3), rho = 0.5, beta = c(1, -1), seed = 42 ) res <- spmodel(y ~ x1 + x2, data = panel, W = list(geography = W1, trade = W2), model = "sar", id = "region", time = "year", effects = "twoway", ndraw = 8000, nomit = 2000) print(res)coords <- cbind(runif(80), runif(80)) W1 <- make_knw(coords, k = 4) W2 <- make_knw(coords, k = 8) panel <- simulate_panel( N = 80, T = 10, W = list(W1, W2), gamma = c(0.7, 0.3), rho = 0.5, beta = c(1, -1), seed = 42 ) res <- spmodel(y ~ x1 + x2, data = panel, W = list(geography = W1, trade = W2), model = "sar", id = "region", time = "year", effects = "twoway", ndraw = 8000, nomit = 2000) print(res)
Estimates a Spatial Lag of X (SLX) panel model via MCMC. This is a thin
wrapper around ols_panel that augments X with WX.
slx_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())slx_panel(y, X, W, N, Time, ndraw = 5500L, nomit = 1500L, prior = list())
y |
Numeric vector of length |
X |
Numeric matrix |
W |
Spatial weight matrix ( |
N |
Integer: number of cross-sectional units. |
Time |
Integer: number of time periods. |
ndraw |
Integer: total MCMC draws (including burn-in). |
nomit |
Integer: burn-in draws to discard. |
prior |
List of prior hyperparameters (see
|
No spatial autoregressive parameter is estimated. Effects decomposition:
direct = , indirect = , total = .
An S3 object of class "spmixW" with OLS fields plus
SLX effects (direct, indirect, total).
LeSage, J.P. and Pace, R.K. (2009). Introduction to Spatial Econometrics. Taylor & Francis/CRC Press.
set.seed(1) N <- 30; Time <- 10 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) WX <- Wbig %*% X y <- X %*% c(1, -0.5) + WX %*% c(0.3, 0.2) + rnorm(N * Time) res <- slx_panel(as.numeric(y), X, W, N, Time, ndraw = 3000, nomit = 1000) print(res)set.seed(1) N <- 30; Time <- 10 coords <- cbind(runif(N), runif(N)) W <- as.matrix(normw(make_knw(coords, k = 5, row_normalise = FALSE))) Wbig <- kronecker(diag(Time), W) X <- matrix(rnorm(N * Time * 2), ncol = 2) WX <- Wbig %*% X y <- X %*% c(1, -0.5) + WX %*% c(0.3, 0.2) + rnorm(N * Time) res <- slx_panel(as.numeric(y), X, W, N, Time, ndraw = 3000, nomit = 1000) print(res)
A user-friendly interface that accepts a formula and data frame, automatically handles sorting, fixed-effects mapping, and dispatches to the appropriate low-level estimation function.
spmodel( formula, data, W, model = "sar", id, time = NULL, effects = "twoway", heteroscedastic = TRUE, rval = NULL, ndraw = 5500L, nomit = 1500L, thin = 1L, taylor_order = 6L, ... )spmodel( formula, data, W, model = "sar", id, time = NULL, effects = "twoway", heteroscedastic = TRUE, rval = NULL, ndraw = 5500L, nomit = 1500L, thin = 1L, taylor_order = 6L, ... )
formula |
A formula of the form |
data |
A data frame containing all variables referenced in the formula,
plus the |
W |
A spatial weight matrix (N x N) or a list of M weight matrices for convex combination models. When a list is provided, the convex combination variant of the specified model is used automatically. |
model |
Character string specifying the model type. One of:
|
id |
Character: name of the column in |
time |
Character or NULL: name of the column identifying time periods.
If |
effects |
Character: fixed-effects specification. One of |
heteroscedastic |
Logical: if |
rval |
Numeric: degrees-of-freedom for heteroscedastic errors. Overrides
|
ndraw |
Integer: total MCMC draws. Default 5500. |
nomit |
Integer: burn-in draws. Default 1500. |
thin |
Integer: thinning interval. Default 1. |
taylor_order |
Integer: Taylor series order for convex combination models. Default 6. |
... |
Additional arguments passed to the prior list (e.g.,
|
Auto-sorting: The data is sorted by time first, then by region within each time period, to match the package's required stacking order (all N regions in period 1, then all N in period 2, etc.). Users do not need to pre-sort their data.
W list detection: When W is a list of matrices, the convex
combination variant of the model is used automatically (e.g., "sar"
becomes sar_conv_panel()).
Cross-sectional mode: When time = NULL, the function
automatically sets T=1 and effects = "none".
An S3 object of class "spmixW" or "spmixW_bma" with
additional metadata:
The formula used.
Name of the response variable.
Names of predictor variables.
Column names used for panel structure.
coords <- cbind(runif(80), runif(80)) W_geo <- make_knw(coords, k = 5) W_trade <- make_knw(coords, k = 10) panel <- simulate_panel(N = 80, T = 8, W = list(W_geo, W_trade), gamma = c(0.6, 0.4), rho = 0.5, beta = c(1.5, -0.8), seed = 123) res <- spmodel(y ~ x1 + x2, data = panel, W = list(geography = W_geo, trade = W_trade), model = "sar", id = "region", time = "year", effects = "twoway", ndraw = 8000, nomit = 2000) print(res)coords <- cbind(runif(80), runif(80)) W_geo <- make_knw(coords, k = 5) W_trade <- make_knw(coords, k = 10) panel <- simulate_panel(N = 80, T = 8, W = list(W_geo, W_trade), gamma = c(0.6, 0.4), rho = 0.5, beta = c(1.5, -0.8), seed = 123) res <- spmodel(y ~ x1 + x2, data = panel, W = list(geography = W_geo, trade = W_trade), model = "sar", id = "region", time = "year", effects = "twoway", ndraw = 8000, nomit = 2000) print(res)
Summary Method for spmixW Objects
## S3 method for class 'spmixW' summary(object, ...)## S3 method for class 'spmixW' summary(object, ...)
object |
An object of class |
... |
Further arguments (ignored). |
Invisibly returns object with MCMC diagnostics appended.
Summary Method for spmixW_bma Objects
## S3 method for class 'spmixW_bma' summary(object, ...)## S3 method for class 'spmixW_bma' summary(object, ...)
object |
An object of class |
... |
Further arguments (ignored). |
Invisibly returns object.
Returns a data frame of estimated parameters with standard errors, test statistics, p-values, and credible intervals.
tidy.spmixW(x, effects = TRUE, ...)tidy.spmixW(x, effects = TRUE, ...)
x |
An object of class |
effects |
Logical: if |
... |
Additional arguments (ignored). |
A data frame with columns: term, estimate,
std.error, statistic, p.value, conf.low,
conf.high, type.
Tidy Method for spmixW_bma Objects
tidy.spmixW_bma(x, ...)tidy.spmixW_bma(x, ...)
x |
An object of class |
... |
Additional arguments (ignored). |
A data frame with BMA-averaged coefficients and model probabilities.