Package-Level & Extensions

Functions defined on the package itself, including the eight extension entry points whose methods live in ext/.

SpatialEconometrics.bayesian_sar — Function
bayesian_sar(W, X, y; n_samples = 1000, n_chains = 2, varnames = nothing, rng = Random.default_rng())

Sample the posterior of a SAR model by NUTS.

Requires Turing: run using Turing, Distributions to activate the SpatialEconometricsTuringExt extension. An intercept is added, so X holds covariates only.

Priors are diffuse - beta ~ MvNormal(0, 10I), rho ~ Uniform(-0.99, 0.99), sigma ~ truncated(Normal(0, 5), 0, Inf) - and are not configurable. Because they are diffuse, the posterior mean should land on the maximum-likelihood estimate for the same data, which is the most useful check on a fit. Each likelihood evaluation uses the Chebyshev approximation to the log-determinant, which is what makes thousands of draws affordable.

Returns a BayesianSpatialResult (a subtype of AbstractBayesianSpatialResult) carrying posterior_mean, posterior_std, coefnames and the raw chain.

Note

chain is not version-stable: Turing returns MCMCChains.Chains before v0.45 and FlexiChains.FlexiChain from v0.45 on, and the two index vector-valued parameters differently. Prefer posterior_mean and posterior_std, which are stable across both.

See also bayesian_sdm, bayesian_sem, fit_spatial.

source
SpatialEconometrics.bayesian_sdm — Function
bayesian_sdm(W, X, y; n_samples = 1000, n_chains = 2, varnames = nothing, rng = Random.default_rng())

Sample the posterior of a Spatial Durbin model by NUTS.

As bayesian_sar, but the design is augmented with WX, so coefnames carries W_-prefixed entries between the slopes and rho.

Requires using Turing, Distributions.

source
SpatialEconometrics.bayesian_sem — Function
bayesian_sem(W, X, y; n_samples = 1000, n_chains = 2, varnames = nothing, rng = Random.default_rng())

Sample the posterior of a spatial error model by NUTS.

Dependence is placed in the errors rather than in y: the likelihood is built from the filtered residual (I - lambda*W)(y - X*beta), and the spatial parameter is reported as lambda.

Requires using Turing, Distributions. See bayesian_sar for the priors and the note about chain.

source
SpatialEconometrics.make_queen_weights — Function
make_queen_weights(L; T = Float64) -> SparseMatrixCSC

Row-standardized queen-contiguity weights for an L x L regular lattice.

Like make_rook_weights but counting diagonal neighbours too, so interior cells have eight neighbours rather than four. Rows are divided by their neighbour count and sum to 1.

Denser connectivity spreads any given rho over more neighbours, so the two schemes are not interchangeable in a simulation: the implied strength of dependence per neighbour differs.

Examples

W = make_queen_weights(10)
count(!iszero, W) / 100          # average neighbours per unit
source
SpatialEconometrics.make_rook_weights — Function
make_rook_weights(L; T = Float64) -> SparseMatrixCSC

Row-standardized rook-contiguity weights for an L x L regular lattice.

Two cells are neighbours when they share an edge, so interior cells have four neighbours, edge cells three and corners two. Units are numbered row-major, u = (r - 1) * L + c, and each row is divided by its own neighbour count, so every row sums to 1 and rho is comparable across lattice sizes.

Returns an N x N sparse matrix with N = L^2. Intended for examples, tests and Monte Carlo work; build weights for real data with knn_weights, distance_weights or read_gal.

Examples

W = make_rook_weights(10)        # 100 units
sum(W, dims = 2)                 # all ones

See also make_queen_weights.

source
SpatialEconometrics.plot_coefficients — Function
plot_coefficients(model; level = 0.95)

Horizontal coefficient plot with confidence intervals. Returns a Makie.Figure.

Requires a Makie backend: run using CairoMakie (or GLMakie) to activate the SpatialEconometricsMakieExt extension. The critical value is the normal quantile for level; a level outside (0, 1) raises an ArgumentError.

source
SpatialEconometrics.plot_gwr_coefficients — Function
plot_gwr_coefficients(model, varname)

Map one GWR local coefficient over the estimation coordinates. Returns a Makie.Figure.

varname must be one of model.coefnames; an unknown name raises an error listing the names available.

Requires a Makie backend - see plot_coefficients.

source
SpatialEconometrics.plot_local_moran — Function
plot_local_moran(lisa, coords)

LISA cluster map, colouring units by cluster_type (:HH, :LL, :HL, :LH, :ns). Returns a Makie.Figure.

lisa is a LISAResult from moran_local and coords an N x 2 matrix for the same units; a mismatch raises DimensionMismatch rather than plotting the shorter of the two.

Requires a Makie backend - see plot_coefficients.

source
SpatialEconometrics.plot_moran_scatter — Function
plot_moran_scatter(y, W)

Moran scatter plot: standardized y against its spatial lag Wy, with the fitted slope drawn through it. Returns a Makie.Figure.

When W is row-standardized that slope is Moran's I, so it should agree with moran_global(y, W).I_stat.

Requires a Makie backend - see plot_coefficients.

source
SpatialEconometrics.robust_coeftable — Function
robust_coeftable(model, spec) -> RobustInferenceTable

Coefficient table showing robust and model-based standard errors together.

Printing both is the point: the gap between them is the diagnostic. If the robust errors are much larger, the model's own covariance was understating uncertainty, usually because of spatial correlation the specification did not capture.

spec is a DriscollKraayVCOV or ConleySpatialHAC; the covariance itself comes from robust_vcov.

Examples

m = fit_spatial(SAR(), W, X, y; varnames = ["income", "density"])
robust_coeftable(m, ConleySpatialHAC(coords, 2.0))
source
SpatialEconometrics.robust_vcov — Function
robust_vcov(model, spec) -> Matrix

Recompute a fitted model's coefficient covariance under a robust specification.

spec selects the estimator: DriscollKraayVCOV for spatial correlation of unrestricted form within each period plus serial correlation, or ConleySpatialHAC for distance-decayed spatial correlation using a kernel and cutoff.

Both are sandwich estimators. Because a spatial-lag model is estimated by maximum likelihood rather than least squares, the scores are first mapped through the Jacobian of the spatial transformation, so the result is a covariance for the same coef vector and can be substituted wherever the model's own vcov is used.

Accepts SpatialRegressionModel, SpatialPanelModel and DynamicSpatialPanelModel.

Examples

m = fit_spatial_panel(SAR(), W, X, y, N, T; fe = TwoWayFE())
V = robust_vcov(m, DriscollKraayVCOV(max_lag = 2))

robust_coeftable(m, DriscollKraayVCOV(max_lag = 2))
spatial_effects(m, DriscollKraayVCOV(max_lag = 2))

See also robust_coeftable, vcov_driscoll_kraay, vcov_conley.

References

Driscoll, J. & Kraay, A. (1998). Review of Economics and Statistics, 80(4). Conley, T. (1999). Journal of Econometrics, 92(1).

source
SpatialEconometrics.spatial_effects — Function
spatial_effects(model; max_power = 50, vcov_mat = model.vcov)
spatial_effects(model, spec::AbstractRobustVCOV; max_power = 50)

Decompose a fitted spatial model into direct, indirect and total marginal effects, with standard errors.

A coefficient in a spatial-lag model is not a marginal effect: since dy/dx' = (I - rho*W)^{-1} (I*beta + W*theta), a change in one unit's covariate feeds back through the whole system. The average diagonal of that matrix is the direct effect, the average off-diagonal row sum the indirect (spillover) effect, and their sum the total. Standard errors are obtained by the delta method.

Works on SpatialRegressionModel, SpatialPanelModel and DynamicSpatialPanelModel; for the dynamic case see dynamic_spatial_effects, which separates short-run from long-run.

Keywords

  • max_power: truncation of the series used for (I - rho*W)^{-1}. The default is ample for moderate rho; values near 1 converge more slowly.
  • vcov_mat: covariance to propagate. Pass a robust specification as the second positional argument instead to use robust_vcov.

Returns

A SpatialEffectsResult with direct, indirect, total and the matching _se vectors, aligned with varnames.

Examples

m  = fit_spatial(SDM(), W, X, y; varnames = ["income", "density"])
ef = spatial_effects(m)
ef.indirect ./ ef.indirect_se

spatial_effects(m, ConleySpatialHAC(coords, 2.0))

References

LeSage, J. & Pace, R. K. (2009). Introduction to Spatial Econometrics, ch. 2.

source