Package 'estimateW'

Title: Estimation of Spatial Weight Matrices
Description: Bayesian estimation of spatial weight matrices in spatial econometric panel models. Allows for estimation of spatial autoregressive (SAR), spatial Durbin (SDM), and spatially lagged explanatory variable (SLX) type specifications featuring an unknown spatial weight matrix. Methodological details are given in Krisztin and Piribauer (2022) <doi:10.1080/17421772.2022.2095426>.
Authors: Tamas Krisztin [aut, cre] , Philipp Piribauer [aut]
Maintainer: Tamas Krisztin <[email protected]>
License: GPL (>= 3)
Version: 0.0.1
Built: 2025-02-19 03:28:07 UTC
Source: https://github.com/tkrisztin/estimatew

Help Index


Probability density for a hierarchical prior setup for the elements of the adjacency matrix based on the beta binomial distribution

Description

A hierarchical prior setup can be used in W_priors to anchor the prior number of expected neighbors. Assuming a fixed prior inclusion probability p=1/2\underline{p}=1/2 for the off-diagonal entries in the binary nn by nn adjacency matrix Ω\Omega implies that the number of neighbors (i.e. the row sums of Ω\Omega) follows a Binomial distribution with a prior expected number of neighbors for the nn spatial observations of (n1)p(n-1)\underline{p}. However, such a prior structure has the potential undesirable effect of promoting a relatively large number of neighbors. To put more prior weight on parsimonious neighborhood structures and promote sparsity in Ω\Omega, the beta binomial prior accounts for the number of neighbors in each row of Ω\Omega.

Usage

bbinompdf(x, nsize, a, b, min_k = 0, max_k = nsize)

Arguments

x

Number of neighbors (scalar)

nsize

Number of potential neighbors: nsize=(n1)=(n-1)

a

Scalar prior parameter aa

b

Scalar prior parameter bb

min_k

Minimum prior number of neighbors (defaults to 0)

max_k

Maximum prior number of neighbors (defaults to nsize)

Details

The beta-binomial distribution is the result of treating the prior inclusion probability p\underline{p} as random (rather than being fixed) by placing a hierarchical beta prior on it. For the number of neighbors xx, the resulting prior on the elements of Ω\Omega, ωij\omega_{ij}, can be written as:

p(ωij=1x)Γ(a+x)Γ(b+(n1)x),p(\omega_{ij} = 1 | x)\propto \Gamma\left(a+ x \right)\Gamma\left(b+(n-1)-x\right),

where Γ()\Gamma(\cdot ) is the Gamma function, and aa and bb are hyperparameters from the beta prior. In the case of a=b=1a = b = 1, the prior takes the form of a discrete uniform distribution over the number of neighbors. By fixing a=1a = 1 the prior can be anchored around the expected number of neighbors mm through b=[(n1)m]/mb=[(n-1)-m]/m (see Ley and Steel, 2009).

The prior can be truncated by setting a minimum (min_k) and/or a maximum number of neighbors (max_k). Values outside this range have zero prior support.

Value

Prior density evaluated at x.

References

Ley, E., & Steel, M. F. (2009). On the effect of prior assumptions in Bayesian model averaging with applications to growth regression. Journal of Applied Econometrics, 24(4). doi:10.1002/jae.1057.


Set prior specifications for the slope parameters

Description

This function allows the user to specify custom values for Gaussian priors on the slope parameters.

Usage

beta_priors(
  k,
  beta_mean_prior = matrix(0, k, 1),
  beta_var_prior = diag(k) * 100
)

Arguments

k

The total number of slope parameters in the model.

beta_mean_prior

numeric kk by 11 matrix of prior means μβ\underline{\mu}_\beta.

beta_var_prior

A kk by kk matrix of prior variances Vβ\underline{V}_\beta. Defaults to a diagonal matrix with 100 on the main diagonal.

Details

For the slope parameters β\beta the package uses common Normal prior specifications. Specifically, p(β)N(μβ,Vβ)p(\beta)\sim\mathcal{N}(\underline{\mu}_\beta,\underline{V}_\beta).

This function allows the user to specify custom values for the prior hyperparameters μβ\underline{\mu}_\beta and Vβ\underline{V}_\beta. The default values correspond to weakly informative Gaussian priors with mean zero and a diagonal prior variance-covariance matrix with 100100 on the main diagonal.

Value

A list with the prior mean vector (beta_mean_prior), the prior variance matrix (beta_var_prior) and the inverse of the prior variance matrix (beta_var_prior_inv).


An R6 class for sampling slope parameters

Description

This class samples slope parameters with a Gaussian prior from the conditional posterior. Use the beta_priors class for setup.

Format

An R6Class generator object

Public fields

beta_prior

The current beta_priors

curr_beta

The current value of β\beta

Methods

Public methods


Method new()

Usage
beta_sampler$new(beta_prior)
Arguments
beta_prior

The list returned by beta_priors


Method sample()

Usage
beta_sampler$sample(Y, X, curr_sigma)
Arguments
Y

The NN by 11 matrix of responses

X

The NN by kk design matrix

curr_sigma

The variance parameter σ2\sigma^2


The four-parameter Beta probability density function

Description

A four-parameter Beta specification as the prior for the spatial autoregressive parameter ρ\rho, as proposed by LeSage and Parent (2007) .

Usage

betapdf(rho, a = 1, b = 1, rmin = 0, rmax = 1)

Arguments

rho

The scalar value for ρ\rho

a

The first shape parameter of the Beta distribution

b

The second shape parameter of the Beta distribution

rmin

Scalar ρmin\underline{\rho}_{min}: the minimum value of ρ\rho

rmax

Scalar ρmax\underline{\rho}_{max}: the maximum value of ρ\rho

Details

The prior density is given by:

p(ρ)1Beta(a,b)(ρρmin)(a1)(ρmaxρ)(b1)2a+b1p(\rho) \sim \frac{1}{Beta(a,b)} \frac{(\rho - \underline{\rho}_{min})^{(a-1)} (\underline{\rho}_{max} - \rho)^{(b-1)} }{2^{a + b - 1}}

where Beta(a,b)Beta(a, b) (a,b>0a,b > 0) represents the Beta function, Beta(a,b)=01ta1(1t)b1dtBeta(a, b)= \int_{0}^{1} t^{a-1} (1-t)^{b-1} dt.

Value

Density value evaluated at rho.

References

LeSage, J. P., and Parent, O. (2007) Bayesian model averaging for spatial econometric models. Geographical Analysis, 39(3), 241-267.


Covid incidences data

Description

COVID-19 data set provided by Johns Hopkins University (Dong et al., 2020). The database contains information on (official) daily infections for a large panel of countries around the globe in the very beginning of the outbreak from 17 February to 20 April 2020.

Usage

covid

Format

A data.frame object.

Details

Data is provided for countries: Australia (AUS), Bahrain (BHR), Belgium (BEL), Canada (CAN), China (CHN), Finland (FIN), France (FRA), Germany (DEU), Iran (IRN), Iraq (IRQ), Israel (ISR), Italy (ITA), Japan (JPN), Kuwait (KWT), Lebanon (LBN), Malaysia (MYS), Oman (OMN), Republic of Korea (KOR), Russian Federation (RUS), Singapore (SGP), Spain (ESP), Sweden (SWE), Thailand (THA), United Arab Emirates (ARE), United Kingdom (GBR), United States of America (USA), and Viet Nam (VNM).

The dataset includes daily data on the country specific maximum measured temperature (Temperature) and precipitation levels (Precipitation) as additional covariates (source: Dark Sky API). The stringency index (Stringency) put forward by Hale et al. (2020), which summarizes country-specific governmental policy measures to contain the spread of the virus. We use the biweekly average of the reported stringency index.

References

Dong, E., Du, H., and Gardner, L. (2020). An interactive web-based dashboard to track COVID-19 in real time. The Lancet Infectious Diseases, 20(5), 533–534. doi:10.1016/S1473-3099(20)30120-1.

Hale, T., Petherick, A., Phillips, T., and Webster, S. (2020). Variation in government responses to COVID-19. Blavatnik School of Government Working Paper, 31, 2020–2011. doi:10.1038/s41562-021-01079-8.

Krisztin, T., and Piribauer, P. (2022). A Bayesian approach for the estimation of weight matrices in spatial autoregressive models, Spatial Economic Analysis, 1-20. doi:10.1080/17421772.2022.2095426.

Krisztin, T., Piribauer, P., and Wögerer, M. (2020). The spatial econometrics of the coronavirus pandemic. Letters in Spatial and Resource Sciences, 13 (3), 209-218. doi:10.1007/s12076-020-00254-1.

Dong, E., Du, H., and Gardner, L. (2020). An interactive web-based dashboard to track COVID-19 in real time. The Lancet Infectious Diseases, 20(5), 533–534. doi:10.1016/S1473-3099(20)30120-1.


Efficient update of the log-determinant and the matrix inverse

Description

While updating the elements of the spatial weight matrix in SAR and SDM type spatial models in a MCMC sampler, the log-determinant term has to be regularly updated, too. When the binary elements of the adjacency matrix are treated unknown, the Matrix Determinant Lemma and the Sherman-Morrison formula are used for computationally efficient updates.

Usage

logdetAinvUpdate(ch_ind, diff, AI, logdet)

Arguments

ch_ind

vector of non-negative integers, between 1 and nn. Denotes which rows of AA should be updated.

diff

a numeric length(ch_ind) by n matrix. This value will be added to the corresponding rows of AA.

AI

numeric nn by nn matrix that is the inverse of A=(InρW)A = (I_n - \rho W). This inverse will be updated using the Sherman-Morrison formula.

logdet

single number that is the log-determinant of the matrix AA. This log-determinant will be updated through the Matrix Determinant Lemma.

Details

Let A=(InρW)A = (I_n - \rho W) be an invertible nn by nn matrix. vv is an nn by 11 column vector of real numbers and uu is a binary vector containing a single one and zeros otherwise. Then the Matrix Determinant Lemma states that:

A+uv=(1+vA1u)det(A)A + uv' = (1 + v'A^{-1}u)det(A)

.

This provides an update to the determinant, but the inverse of AA has to be updated as well. The Sherman-Morrison formula proves useful:

(A+uv)1=A1A1uvA11+vA1u(A + uv')^{-1} = A^{-1} \frac{A^{-1}uv'A^{-1}}{1 + v'A^{-1}u}

.

Using these two formulas, an efficient update of the spatial projection matrix determinant can be achieved.

Value

A list containing the updated nn by nn matrix A1A^{-1}, as well as the updated log determinant of AA

References

Sherman, J., and Morrison, W. J. (1950) Adjustment of an inverse matrix corresponding to a change in one element of a given matrix. The Annals of Mathematical Statistics, 21(1), 124-127.

Harville, D. A. (1998) Matrix algebra from a statistician's perspective. Taylor & Francis.


Pace and Barry's log determinant approximation

Description

Bayesian estimates of parameters of SAR and SDM type spatial models require the computation of the log-determinant of positive-definite spatial projection matrices of the form (InρW)(I_n - \rho W), where WW is a nn by nn spatial weight matrix. However, direct computation of the log-determinant is computationally expensive.

Usage

logdetPaceBarry(W, length.out = 200, rmin = -1, rmax = 1)

Arguments

W

numeric nn by nn non-negative spatial weights matrix, with zeros on the main diagonal.

length.out

single, integer number, has to be at least 51 (due to order of approximation). Sets how fine the grid approximation is. Default value is 200.

rmin

single number between -1 and 1. Sets the minimum value of the spatial autoregressive parameter ρ\rho. Has to be lower than rmax. Default value is -1.

rmax

single number between -1 and 1. Sets the maximum value of the spatial autoregressive parameter ρ\rho. Has to be higher than rmin. Default value is 1.

Details

This function wraps the log-determinant approximation by Barry and Pace (1999), which can be used to precompute the log-determinants over a grid of ρ\rho values.

Value

numeric length.out by 2 matrix; the first column contains the approximated log-determinants the second column the ρ\rho values ranging between rmin and rmax.

References

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(1-3), 41-54.


A Markov Chain Monte Carlo (MCMC) sampler for a linear panel model

Description

The sampler uses independent Normal-inverse-Gamma priors to estimate a linear panel data model. The function is used for an illustration on using the beta_sampler and sigma_sampler classes.

Usage

normalgamma(
  Y,
  tt,
  X = matrix(1, nrow(Y), 1),
  niter = 200,
  nretain = 100,
  beta_prior = beta_priors(k = ncol(X)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

X

numeric N×k1N \times k_1 design matrix of independent variables.

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 200.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 100.

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered model takes the form:

Yt=Xtβ+εt,Y_t = X_t \beta + \varepsilon_t,

with εtN(0,Inσ2)\varepsilon_t \sim N(0,I_n \sigma^2).

YtY_t (n×1n \times 1) collects the nn cross-sectional observations for time t=1,...,Tt=1,...,T. XtX_t (n×k1n \times k_1) is a matrix of explanatory variables. β\beta (k1×1k_1 \times 1) is an unknown slope parameter matrix.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), X=[X1,...,XT]X=[X_1',...,X_T']' (N×kN \times k), with N=nTN=nT, the final model can be expressed as:

Y=Xβ+ε,Y = X \beta + \varepsilon,

where εN(0,INσ2)\varepsilon \sim N(0,I_N \sigma^2). Note that the input data matrices have to be ordered first by the cross-sectional (spatial) units and then stacked by time.

Examples

n = 20; tt = 10; k = 3
X = matrix(stats::rnorm(n*tt*k),n*tt,k)
Y = X %*% c(1,0,-1) + stats::rnorm(n*tt,0,.5)
res = normalgamma(Y,tt,X)

Graphical summary of the estimated adjacency matrix Ω\Omega

Description

Graphical plot of the posterior probabilities of the estimated adjacency matrix Ω\Omega.

Usage

## S3 method for class 'estimateW'
plot(
  x,
  cols = c("white", "lightgrey", "black"),
  breaks = c(0, 0.5, 0.75, 1),
  ...
)

Arguments

x

estimateW object.

cols

Main colors to use for the plot

breaks

Breaks for the colors

...

further arguments are passed on to be invoked


Graphical summary of a generated spatial weight matrix

Description

Graphical summary of a generated spatial weight matrix

Usage

## S3 method for class 'sim_dgp'
plot(x, ...)

Arguments

x

sim_dgp object

...

further arguments are passed on to the invoked


Specify prior for the spatial autoregressive parameter and sampling settings

Description

Specify prior for the spatial autoregressive parameter and sampling settings

Usage

rho_priors(
  rho_a_prior = 1,
  rho_b_prior = 1,
  rho_min = 0,
  rho_max = 1,
  init_rho_scale = 1,
  griddy_n = 60,
  use_griddy_gibbs = TRUE,
  mh_tune_low = 0.4,
  mh_tune_high = 0.6,
  mh_tune_scale = 0.1
)

Arguments

rho_a_prior

Single number. Prior hyperparameter for the four-parameter beta distribution betapdf. Defaults to 1.

rho_b_prior

Single number. Prior hyperparameter for the four-parameter beta distribution betapdf. Defaults to 1.

rho_min

Minimum value for ρ\rho (default: 0)

rho_max

Maximum value for ρ\rho (default: 1)

init_rho_scale

For Metropolis-Hastings step the initial candidate variance (default: 1)

griddy_n

single integer number. Sets how fine the grid approximation is. Default value is 60.

use_griddy_gibbs

Binary value. Should griddy-Gibbs be used for ρ\rho estimation? use_griddy_gibbs=TRUE does not work if row_standardized_prior = FALSE is specified in the WW prior specification. if TRUE: griddy-Gibbs step for sampling ρ\rho; if FALSE: tuned random-walk Metropolis-Hastings step

mh_tune_low

Lower bound of acceptance rate for Metropolis-Hastings tuning (used if use_griddy_gibbs==FALSE)

mh_tune_high

Upper bound of acceptance rate for Metropolis-Hastings tuning (used if use_griddy_gibbs==FALSE)

mh_tune_scale

Scaling factor for Metropolis-Hastings tuning (used if use_griddy_gibbs==FALSE)


An R6 class for sampling the spatial autoregressive parameter ρ\rho

Description

An R6 class for sampling the spatial autoregressive parameter ρ\rho

An R6 class for sampling the spatial autoregressive parameter ρ\rho

Format

An R6Class generator object

Details

This class samples the spatial autoregressive parameter using either a tuned random-walk Metropolis-Hastings or a griddy Gibbs step. Use the rho_priors class for setup.

For the Griddy-Gibbs algorithm see Ritter and Tanner (1992).

Public fields

rho_prior

The current rho_priors

curr_rho

The current value of ρ\rho

curr_W

The current spatial weight matrix WW; an nn by nn matrix.

curr_A

The current spatial filter matrix IρWI - \rho W.

curr_AI

The inverse of curr_A

curr_logdet

The current log-determinant of curr_A

curr_logdets

A set of log-determinants for various values of ρ\rho. See the rho_priors function for settings of step site and other parameters of the grid.

Methods

Public methods


Method new()

Usage
rho_sampler$new(rho_prior, W = NULL)
Arguments
rho_prior

The list returned by rho_priors

W

An optional starting value for the spatial weight matrix WW


Method stopMHtune()

Function to stop the tuning of the Metropolis-Hastings step. The tuning of the Metropolis-Hastings step is usually carried out until half of the burn-in phase. Call this function to turn it off.

Usage
rho_sampler$stopMHtune()

Method setW()

Usage
rho_sampler$setW(newW, newLogdet = NULL, newA = NULL, newAI = NULL)
Arguments
newW

The updated spatial weight matrix WW.

newLogdet

An optional value for the log determinant corresponding to newW and curr_rho.

newA

An optional value for the spatial projection matrix using newW and curr_rho.

newAI

An optional value for the matrix inverse of newA.


Method sample()

Usage
rho_sampler$sample(Y, mu, sigma)
Arguments
Y

The nn by TT matrix of responses.

mu

The nn by TT matrix of means.

sigma

The variance parameter σ2\sigma^2.


Method sample_Griddy()

Usage
rho_sampler$sample_Griddy(Y, mu, sigma)
Arguments
Y

The nn by TT matrix of responses.

mu

The nn by TT matrix of means.

sigma

The variance parameter σ2\sigma^2.


Method sample_MH()

Usage
rho_sampler$sample_MH(Y, mu, sigma)
Arguments
Y

The nn by TT matrix of responses.

mu

The nn by TT matrix of means.

sigma

The variance parameter σ2\sigma^2.

References

Ritter, C., and Tanner, M. A. (1992). Facilitating the Gibbs sampler: The Gibbs stopper and the griddy-Gibbs sampler. Journal of the American Statistical Association, 87(419), 861-868.


A fast sampling step implemented in C++ for updating the spatial weight matrix.

Description

This function is intended to be called from the R6 class W_sampler.

Usage

sampleW_fast(
  Y,
  curr_sigma,
  mu,
  lag_mu,
  W_prior,
  curr_W,
  curr_w,
  curr_A,
  curr_AI,
  curr_logdet,
  curr_rho,
  nr_neighbors_prior,
  symmetric,
  spatial_error,
  row_standardized
)

Arguments

Y

The nn by tttt matrix of responses

curr_sigma

The variance parameter σ2\sigma^2

mu

The nn by tttt matrix of means.

lag_mu

nn by tttt matrix of means that will be spatially lagged with the estimated WW. Defaults to a matrix with zero elements.

W_prior

The current W_priors

curr_W

binary nn by nn spatial connectivity matrix Ω\Omega

curr_w

Row-standardized nn by nn spatial weight matrix WW

curr_A

The current spatial projection matrix IρWI - \rho W.

curr_AI

The inverse of curr_A

curr_logdet

The current log-determinant of curr_A

curr_rho

single number between -1 and 1 or NULL, depending on whether the sampler updates the spatial autoregressive parameter ρ\rho. Set while invoking initialize or using the function set_rho.

nr_neighbors_prior

An nn dimensional vector of prior weights on the number of neighbors (i.e. the row sums of the adjacency matrix Ω\Omega), where the first element denotes the prior probability of zero neighbors and the last those of n1n-1. A prior using only fixed inclusion probabilities for the entries in Ω\Omega would be an nn dimensional vector of 1/n1/n. Defaults to a bbinompdf prior, with prior parameters a=1a = 1, b=1b = 1.

symmetric

Binary value. Should the estimated adjacency matrix Ω\Omega be symmetric (default: FALSE)? if TRUE: Ω\Omega is forced symmetric; if FALSE: Ω\Omega not necessarily symmetric.

spatial_error

Should a spatial error model be constructed? Defaults to FALSE.

row_standardized

Binary value. Should the estimated WW matrix be row-standardized (default: TRUE)? if TRUE: row-stochastic WW; if FALSE: WW not row-standardized.


A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial autoregressive model (SAR) with exogenous spatial weight matrix.

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter beta prior for the spatial autoregressive parameter ρ\rho. The function is used as an illustration on using the beta_sampler, sigma_sampler, and rho_sampler classes.

Usage

sar(
  Y,
  tt,
  W,
  Z = matrix(1, nrow(Y), 1),
  niter = 200,
  nretain = 100,
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

W

numeric, non-negative and row-stochastic nn by nn exogenous spatial weight matrix. Must have zeros on the main diagonal.

Z

numeric N×k3N \times k_3 design matrix of independent variables. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model).

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 200.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 100.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients, generated by the smart constructor beta_priors.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial autoregressive model (SAR) takes the form:

Yt=ρWYt+Ztβ+εt,Y_t = \rho W Y_t + Z_t \beta + \varepsilon_t,

with εtN(0,Inσ2)\varepsilon_t \sim N(0,I_n \sigma^2). The row-stochastic nn by nn spatial weight matrix WW is non-negative and has zeros on the main diagonal. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. ZtZ_t (n×k3n \times k_3) is a matrix of explanatory variables. β\beta (k3×1k_3 \times 1) is an unknown slope parameter matrix.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), Z=[Z1,...,ZT]Z=[Z_1',...,Z_T']' (N×k3N \times k_3), with N=nTN=nT, the final model can be expressed as:

Y=ρW~Y+Zβ+ε,Y = \rho \tilde{W}Y + Z \beta + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,INσ2)\varepsilon \sim N(0,I_N \sigma^2). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time. This is a wrapper function calling sdm with no spatially lagged dependent variables.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n =n, tt = tt, rho = .5, beta3 = c(1,.5), sigma2 = .5)
res = sar(Y = dgp_dat$Y,tt = tt, W = dgp_dat$W,
          Z = dgp_dat$Z,niter = 100,nretain = 50)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial autoregressive model (SAR) with unknown spatial weight matrix

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter beta prior for the spatial autoregressive parameter ρ\rho. This is a wrapper function calling sdmw with no spatially lagged exogenous variables.

Usage

sarw(
  Y,
  tt,
  Z,
  niter = 100,
  nretain = 50,
  W_prior = W_priors(n = nrow(Y)/tt),
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

Z

numeric N×k3N \times k_3 design matrix of independent variables. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model).

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 100.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 50.

W_prior

list containing prior settings for estimating the spatial weight matrix WW. Generated by the smart constructor W_priors.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial autoregressive model (SAR) with unknown (nn by nn) spatial weight matrix WW takes the form:

Yt=ρWYt+Zβ+εt,Y_t = \rho W Y_t + Z \beta + \varepsilon_t,

with εtN(0,Inσ2)\varepsilon_t \sim N(0,I_n \sigma^2) and W=f(Ω)W = f(\Omega). The nn by nn matrix Ω\Omega is an unknown binary adjacency matrix with zeros on the main diagonal and f()f(\cdot) is the (optional) row-standardization function. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. ZtZ_t (n×k3n \times k_3) is a matrix of explanatory variables. β\beta (k3×1k_3 \times 1) is an unknown slope parameter vector.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k3N \times k_3), with N=nTN=nT. The final model can be expressed as:

Y=ρW~Y+Zβ+ε,Y = \rho \tilde{W}Y + Z \beta + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,INσ2)\varepsilon \sim N(0,I_N \sigma^2). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Estimation usually even works well in cases of n>>Tn >> T. However, note that for applications with n>200n > 200 the estimation process becomes computationally demanding and slow. Consider in this case reducing niter and nretain and carefully check whether the posterior chains have converged.

Value

List with posterior samples for the slope parameters, ρ\rho, σ2\sigma^2, WW, and average direct, indirect, and total effects.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n =n, tt = tt, rho = .5, beta3 = c(.5,1),
            sigma2 = .05,n_neighbor = 3,intercept = TRUE)
res = sarw(Y = dgp_dat$Y,tt = tt,Z = dgp_dat$Z,niter = 20,nretain = 10)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial Durbin error model (SDEM) with exogenous spatial weight matrix.

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter prior for the spatial autoregressive parameter ρ\rho. The function is used as an illustration on using the beta_sampler, sigma_sampler, and rho_sampler classes.

Usage

sdem(
  Y,
  tt,
  W,
  X = matrix(0, nrow(Y), 0),
  Z = matrix(1, nrow(Y), 1),
  niter = 200,
  nretain = 100,
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

W

numeric, non-negative and row-stochastic nn by nn exogenous spatial weight matrix. Must have zeros on the main diagonal.

X

numeric N×k1N \times k_1 design matrix of independent variables. These will be automatically spatially lagged. If no spatially lagged variable is included in the model a matrix with NN rows and zero columns should be supplied (the default value). Note: either XX or ZZ has to be a matrix with at least one column.

Z

numeric N×k3N \times k_3 design matrix of independent variables which are not spatially lagged. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model). Note: either XX or ZZ has to be a matrix with at least one column.

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 200.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 100.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors. The ordering of the priors is: (1) priors of XX, (2) priors of spatially lagged XX, (3) priors of ZZ.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial Durbin error model (SDEM) takes the form:

Yt=Xtβ1+WXtβ2+Zβ3+εt,Y_t = X_t \beta_1 + W X_t \beta_2 + Z \beta_3 + \varepsilon_t,

with εtN(0,(InρW)σ2)\varepsilon_t \sim N(0,(I_n - \rho W) \sigma^2). The row-stochastic nn by nn spatial weight matrix WW is non-negative and has zeros on the main diagonal. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. XtX_t (n×k1n \times k_1) and ZtZ_t (n×k2n \times k_2) are matrices of explanatory variables, where the former will also be spatially lagged. β1\beta_1 (k1×1k_1 \times 1), β2\beta_2 (k1×1k_1 \times 1) and β3\beta_3 (k2×1k_2 \times 1) are unknown slope parameter vectors.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), X=[X1,...,XT]X=[X_1',...,X_T']' (N×k1N \times k_1) and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k2N \times k_2), with N=nTN=nT, the final model can be expressed as:

Y=Xβ1+W~Xβ2+Zβ3+ε,Y = X \beta_1 + \tilde{W} X \beta_2 + Z \beta_3 + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,σ2(In(InρW))\varepsilon \sim N(0,\sigma^2 (I_n \otimes (I_n - \rho W) ). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n = n, tt = tt, rho = .5, beta1 = c(.5,1), beta2 = c(-1,.5),
                   beta3 = c(1.5), sigma2 = .5, spatial_error = TRUE)
res = sdem(Y = dgp_dat$Y, tt = tt,  W = dgp_dat$W, X = dgp_dat$X,
           Z = dgp_dat$Z, niter = 100, nretain = 50)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial Durbin error model (SDEM) with unknown spatial weight matrix

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter beta prior for the spatial autoregressive parameter ρ\rho. It is a wrapper around W_sampler.

Usage

sdemw(
  Y,
  tt,
  X = matrix(0, nrow(Y), 0),
  Z = matrix(1, nrow(Y), 1),
  niter = 100,
  nretain = 50,
  W_prior = W_priors(n = nrow(Y)/tt),
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

X

numeric N×k1N \times k_1 design matrix of independent variables. These will be automatically spatially lagged. If no spatially lagged variable is included in the model a matrix with NN rows and zero columns should be supplied (the default value). Note: either XX or ZZ has to be a matrix with at least one column.

Z

numeric N×k3N \times k_3 design matrix of independent variables which are not spatially lagged. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model). Note: either XX or ZZ has to be a matrix with at least one column.

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 100.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 50.

W_prior

list containing prior settings for estimating the spatial weight matrix WW. Generated by the smart constructor W_priors.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors. The ordering of the priors is: (1) priors of XX, (2) priors of spatially lagged XX, (3) priors of ZZ.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial Durbin error model (SDEM) with unknown (nn by nn) spatial weight matrix WW takes the form:

Yt=Xtβ1+WXtβ2+Zβ3+εt,Y_t = X_t \beta_1 + W X_t \beta_2 + Z \beta_3 + \varepsilon_t,

with εtN(0,(InρW)σ2)\varepsilon_t \sim N(0,(I_n - \rho W) \sigma^2) and W=f(Ω)W = f(\Omega). The nn by nn matrix Ω\Omega is an unknown binary adjacency matrix with zeros on the main diagonal and f()f(\cdot) is the (optional) row-standardization function. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. XtX_t (n×k1n \times k_1) and ZtZ_t (n×k2n \times k_2) are matrices of explanatory variables, where the former will also be spatially lagged. β1\beta_1 (k1×1k_1 \times 1), β2\beta_2 (k1×1k_1 \times 1) and β3\beta_3 (k2×1k_2 \times 1) are unknown slope parameter vectors.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), X=[X1,...,XT]X=[X_1',...,X_T']' (N×k1N \times k_1) and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k2N \times k_2), with N=nTN=nT. The final model can be expressed as:

Y=Xβ1+W~Xβ2+Zβ3+ε,Y = X \beta_1 + \tilde{W} X \beta_2 + Z \beta_3 + \varepsilon,

where εN(0,σ2(In(InρW))\varepsilon \sim N(0,\sigma^2 (I_n \otimes (I_n - \rho W) ). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Estimation usually even works well in cases of n>>Tn >> T. However, note that for applications with n>200n > 200 the estimation process becomes computationally demanding and slow. Consider in this case reducing niter and nretain and carefully check whether the posterior chains have converged.

Value

List with posterior samples for the slope parameters, ρ\rho, σ2\sigma^2, WW, and average direct, indirect, and total effects.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n =n, tt = tt, rho = .75, beta1 = c(.5,1),beta2 = c(-1,.5),
            beta3 = c(1.5), sigma2 = .05,n_neighbor = 3,intercept = TRUE, spatial_error = TRUE)
# res = sdemw(Y = dgp_dat$Y,tt = tt,X = dgp_dat$X,Z = dgp_dat$Z,niter = 20,nretain = 10)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial Durbin model (SDM) with exogenous spatial weight matrix.

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter prior for the spatial autoregressive parameter ρ\rho. The function is used as an illustration on using the beta_sampler, sigma_sampler, and rho_sampler classes.

Usage

sdm(
  Y,
  tt,
  W,
  X = matrix(0, nrow(Y), 0),
  Z = matrix(1, nrow(Y), 1),
  niter = 200,
  nretain = 100,
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

W

numeric, non-negative and row-stochastic nn by nn exogenous spatial weight matrix. Must have zeros on the main diagonal.

X

numeric N×k1N \times k_1 design matrix of independent variables. These will be automatically spatially lagged. If no spatially lagged variable is included in the model a matrix with NN rows and zero columns should be supplied (the default value). Note: either XX or ZZ has to be a matrix with at least one column.

Z

numeric N×k3N \times k_3 design matrix of independent variables which are not spatially lagged. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model). Note: either XX or ZZ has to be a matrix with at least one column.

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 200.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 100.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors. The ordering of the priors is: (1) priors of XX, (2) priors of spatially lagged XX, (3) priors of ZZ.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial Durbin model (SDM) takes the form:

Yt=ρWYt+Xtβ1+WXtβ2+Zβ3+εt,Y_t = \rho W Y_t + X_t \beta_1 + W X_t \beta_2 + Z \beta_3 + \varepsilon_t,

with εtN(0,Inσ2)\varepsilon_t \sim N(0,I_n \sigma^2). The row-stochastic nn by nn spatial weight matrix WW is non-negative and has zeros on the main diagonal. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. XtX_t (n×k1n \times k_1) and ZtZ_t (n×k2n \times k_2) are matrices of explanatory variables, where the former will also be spatially lagged. β1\beta_1 (k1×1k_1 \times 1), β2\beta_2 (k1×1k_1 \times 1) and β3\beta_3 (k2×1k_2 \times 1) are unknown slope parameter vectors.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), X=[X1,...,XT]X=[X_1',...,X_T']' (N×k1N \times k_1) and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k2N \times k_2), with N=nTN=nT, the final model can be expressed as:

Y=ρW~Y+Xβ1+W~Xβ2+Zβ3+ε,Y = \rho \tilde{W}Y + X \beta_1 + \tilde{W} X \beta_2 + Z \beta_3 + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,INσ2)\varepsilon \sim N(0,I_N \sigma^2). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n = n, tt = tt, rho = .5, beta1 = c(.5,1), beta2 = c(-1,.5),
                  beta3 = c(1.5), sigma2 = .5)
res = sdm(Y = dgp_dat$Y, tt = tt,  W = dgp_dat$W, X = dgp_dat$X,
          Z = dgp_dat$Z, niter = 100, nretain = 50)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial Durbin model (SDM) with unknown spatial weight matrix

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter beta prior for the spatial autoregressive parameter ρ\rho. It is a wrapper around W_sampler.

Usage

sdmw(
  Y,
  tt,
  X = matrix(0, nrow(Y), 0),
  Z = matrix(1, nrow(Y), 1),
  niter = 100,
  nretain = 50,
  W_prior = W_priors(n = nrow(Y)/tt),
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

X

numeric N×k1N \times k_1 design matrix of independent variables. These will be automatically spatially lagged. If no spatially lagged variable is included in the model a matrix with NN rows and zero columns should be supplied (the default value). Note: either XX or ZZ has to be a matrix with at least one column.

Z

numeric N×k3N \times k_3 design matrix of independent variables which are not spatially lagged. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model). Note: either XX or ZZ has to be a matrix with at least one column.

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 100.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 50.

W_prior

list containing prior settings for estimating the spatial weight matrix WW. Generated by the smart constructor W_priors.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors. The ordering of the priors is: (1) priors of XX, (2) priors of spatially lagged XX, (3) priors of ZZ.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial Durbin model (SDM) with unknown (nn by nn) spatial weight matrix WW takes the form:

Yt=ρWYt+Xtβ1+WXtβ2+Zβ3+εt,Y_t = \rho W Y_t + X_t \beta_1 + W X_t \beta_2 + Z \beta_3 + \varepsilon_t,

with εtN(0,Inσ2)\varepsilon_t \sim N(0,I_n \sigma^2) and W=f(Ω)W = f(\Omega). The nn by nn matrix Ω\Omega is an unknown binary adjacency matrix with zeros on the main diagonal and f()f(\cdot) is the (optional) row-standardization function. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. XtX_t (n×k1n \times k_1) and ZtZ_t (n×k2n \times k_2) are matrices of explanatory variables, where the former will also be spatially lagged. β1\beta_1 (k1×1k_1 \times 1), β2\beta_2 (k1×1k_1 \times 1) and β3\beta_3 (k2×1k_2 \times 1) are unknown slope parameter vectors.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), X=[X1,...,XT]X=[X_1',...,X_T']' (N×k1N \times k_1) and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k2N \times k_2), with N=nTN=nT. The final model can be expressed as:

Y=ρW~Y+Xβ1+W~Xβ2+Zβ3+ε,Y = \rho \tilde{W}Y + X \beta_1 + \tilde{W} X \beta_2 + Z \beta_3 + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,INσ2)\varepsilon \sim N(0,I_N \sigma^2). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Estimation usually even works well in cases of n>>Tn >> T. However, note that for applications with n>200n > 200 the estimation process becomes computationally demanding and slow. Consider in this case reducing niter and nretain and carefully check whether the posterior chains have converged.

Value

List with posterior samples for the slope parameters, ρ\rho, σ2\sigma^2, WW, and average direct, indirect, and total effects.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n =n, tt = tt, rho = .75, beta1 = c(.5,1),beta2 = c(-1,.5),
            beta3 = c(1.5), sigma2 = .05,n_neighbor = 3,intercept = TRUE)
res = sdmw(Y = dgp_dat$Y,tt = tt,X = dgp_dat$X,Z = dgp_dat$Z,niter = 20,nretain = 10)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial error model (SEM) with exogenous spatial weight matrix.

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter prior for the spatial autoregressive parameter ρ\rho. The function is used as an illustration on using the beta_sampler, sigma_sampler, and rho_sampler classes.

Usage

sem(
  Y,
  tt,
  W,
  Z = matrix(1, nrow(Y), 1),
  niter = 200,
  nretain = 100,
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

W

numeric, non-negative and row-stochastic nn by nn exogenous spatial weight matrix. Must have zeros on the main diagonal.

Z

numeric N×k3N \times k_3 design matrix of independent variables which are not spatially lagged. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model).

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 200.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 100.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial error model (SDEM) takes the form:

Yt=Zβ+εt,Y_t = Z \beta + \varepsilon_t,

with εtN(0,(InρW)σ2)\varepsilon_t \sim N(0,(I_n - \rho W) \sigma^2). The row-stochastic nn by nn spatial weight matrix WW is non-negative and has zeros on the main diagonal. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. ZtZ_t (n×k2n \times k_2) is a matrix of explanatory variables, where the former will also be spatially lagged. β\beta (k3×1k_3 \times 1) is an unknown slope parameter vector.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1) and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k2N \times k_2), with N=nTN=nT, the final model can be expressed as:

Y=Zβ3+ε,Y = Z \beta_3 + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,σ2(In(InρW))\varepsilon \sim N(0,\sigma^2 (I_n \otimes (I_n - \rho W) ). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n =n, tt = tt, rho = .5, beta3 = c(.5,1),
            sigma2 = .05,n_neighbor = 3,intercept = TRUE,spatial_error = TRUE)
res = sem(Y = dgp_dat$Y, tt = tt,  W = dgp_dat$W,
           Z = dgp_dat$Z, niter = 100, nretain = 50)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial error model (SEM) with unknown spatial weight matrix

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters, as well as a four-parameter beta prior for the spatial autoregressive parameter ρ\rho. This is a wrapper function calling sdemw with no spatially lagged exogenous variables.

Usage

semw(
  Y,
  tt,
  Z,
  niter = 100,
  nretain = 50,
  W_prior = W_priors(n = nrow(Y)/tt),
  rho_prior = rho_priors(),
  beta_prior = beta_priors(k = ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

Z

numeric N×k3N \times k_3 design matrix of independent variables. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model).

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 100.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 50.

W_prior

list containing prior settings for estimating the spatial weight matrix WW. Generated by the smart constructor W_priors.

rho_prior

list of prior settings for estimating ρ\rho, generated by the smart constructor rho_priors

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered panel spatial error model (SEM) with unknown (nn by nn) spatial weight matrix WW takes the form:

Yt=Zβ+εt,Y_t = Z \beta + \varepsilon_t,

with εtN(0,(InρW)σ2)\varepsilon_t \sim N(0,(I_n - \rho W) \sigma^2) and W=f(Ω)W = f(\Omega). The nn by nn matrix Ω\Omega is an unknown binary adjacency matrix with zeros on the main diagonal and f()f(\cdot) is the (optional) row-standardization function. ρ\rho is a scalar spatial autoregressive parameter.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. ZtZ_t (n×k3n \times k_3) is a matrix of explanatory variables. β\beta (k3×1k_3 \times 1) is an unknown slope parameter vector.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k3N \times k_3), with N=nTN=nT. The final model can be expressed as:

Y=Zβ+ε,Y = Z \beta + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,σ2(In(InρW))\varepsilon \sim N(0,\sigma^2 (I_n \otimes (I_n - \rho W) ). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Estimation usually even works well in cases of n>>Tn >> T. However, note that for applications with n>200n > 200 the estimation process becomes computationally demanding and slow. Consider in this case reducing niter and nretain and carefully check whether the posterior chains have converged.

Value

List with posterior samples for the slope parameters, ρ\rho, σ2\sigma^2, WW, and average direct, indirect, and total effects.

Examples

n = 20; tt = 10
dgp_dat = sim_dgp(n =n, tt = tt, rho = .5, beta3 = c(.5,1),
            sigma2 = .05,n_neighbor = 3,intercept = TRUE,spatial_error = TRUE)
res = semw(Y = dgp_dat$Y,tt = tt,Z = dgp_dat$Z,niter = 20,nretain = 10)

Set prior specification for the error variance using an inverse Gamma distribution

Description

Set prior specification for the error variance using an inverse Gamma distribution

Usage

sigma_priors(sigma_rate_prior = 0.001, sigma_shape_prior = 0.001)

Arguments

sigma_rate_prior

Sigma rate prior parameter (scalar), default: 0.0010.001.

sigma_shape_prior

Sigma shape prior parameter (scalar), default: 0.0010.001.

This function allows the user to specify priors for the error variance σ2\sigma^2.


An R6 class for sampling for sampling σ2\sigma^2

Description

This class samples nuisance parameter which an inverse Gamma prior from the conditional posterior. Use the sigma_priors class for setup.

Format

An R6Class generator object

Public fields

sigma_prior

The current sigma_priors

curr_sigma

The current value of σ2\sigma^2

Methods

Public methods


Method new()

Usage
sigma_sampler$new(sigma_prior)
Arguments
sigma_prior

The list returned by sigma_priors


Method sample()

Usage
sigma_sampler$sample(Y, mu)
Arguments
Y

The NN by 11 matrix of responses

mu

The NN by 11 matrix of means


Simulating from a data generating process

Description

This function can be used to generate data from a data generating process for SDM, SAR, SEM, and SLX type models.

Usage

sim_dgp(
  n,
  tt,
  rho,
  beta1 = c(),
  beta2 = c(),
  beta3 = c(),
  sigma2,
  n_neighbor = 4,
  W = NULL,
  do_symmetric = FALSE,
  intercept = FALSE,
  spatial_error = FALSE
)

Arguments

n

Number of spatial observations nn.

tt

Number of time observations TT.

rho

The true ρ\rho parameter

beta1

Vector of dimensions k1×1k_1 \times 1. Provides the values for β1\beta_1 Defaults to c(). Note: has to be of same length as β2\beta_2.

beta2

Vector of dimensions k1×1k_1 \times 1. Provides the values for β2\beta_2 Defaults to c(). Note: has to be fo same length as β1\beta_1.

beta3

Vector of dimensions k2×1k_2 \times 1. Provides the values for β3\beta_3 Defaults to c().

sigma2

The true σ2\sigma^2 parameter for the DGP. Has to be a scalar larger than zero.

n_neighbor

Number of neighbors for the generated n×nn \times n spatial weight WW matrix. Defaults to 4.

W

Ecogeneous spatial weight matrix for the data generating process. Defaults to NULL, in which case a nearest neighbour matrix with n_neighbor will be generated.

do_symmetric

Should the generated spatial weight matrix be symmetric? (default: FALSE)

intercept

Should the first column of ZZ be an intercept? Defaults to FALSE. If intercept = TRUE, β3\beta_3 has to be at least of length 1.

spatial_error

Should a spatial error model be constructed? Defaults to FALSE.

Details

For the SDM, SAR, and SLX models the generated spatial panel model takes the form

Y=ρWY+Xβ1+WXβ2+Zβ3+ϵ,Y = \rho W Y + X \beta_1 + W X \beta_2 + Z \beta_3 + \epsilon,

with ϵN(0,Inσ2)\epsilon \sim N(0,I_n\sigma^2).

For the SEM model the generated spatial panel model takes the form

Y=Xβ1+WXβ2+Zβ3+ϵ,Y = X \beta_1 + W X \beta_2 + Z \beta_3 + \epsilon,

with ϵN(0,(InρW)σ2)\epsilon \sim N(0,(I_n - \rho W)\sigma^2).

The function generates the N×1N \times 1 vector YY. The elements of the explanatory variable matrices XX (N×k1N \times k_1) and ZZ (N×k2N \times k_2) are randomly generated from a Gaussian distribution with zero mean and unity variance (N(0,1)N(0,1)).

The non-negative, row-stochastic nn by nn matrix WW is constructed using a k-nearest neighbor specification based on a randomly generated spatial location pattern, with coordinates sampled from a standard normal distribution.

Values for the parameters β1\beta_1, β2\beta_2, and β3\beta_3, as well as ρ\rho and σ2\sigma^2 have to be provided by the user. The length of β1\beta_1 and β2\beta_2 have to be equal.

  • A spatial Durbin model (SDM) is constructed if ρ\rho is not equal to zero, spatial_error is FALSE, and β1\beta_1, β2\beta_2, and β3\beta_3 are all supplied by the user.

  • A spatial autoregressive model is constructed if ρ\rho is not equal to zero, spatial_error is FALSE, and only β3\beta_3 is supplied by the user.

  • An SLX type model is constructed if ρ\rho is equal to zero, spatial_error is FALSE, and β1\beta_1, β2\beta_2 are supplied by the user.

  • An SEM type model is constructed if spatial_error is TRUE and either only β3\beta_3 or β1\beta_1, β2\beta_2, and β3\beta_3 are supplied by the user.

Value

A list with the generated XX, YY and WW and a list of parameters.

Examples

# SDM data generating process
dgp_dat = sim_dgp(n =20, tt = 10, rho = .5, beta1 = c(1,-1),
                  beta2 = c(0,.5),beta3 = c(.2),sigma2 = .5)

A Markov Chain Monte Carlo (MCMC) sampler for the panel spatial SLX model with unknown spatial weight matrix

Description

The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters. It is a wrapper around W_sampler.

Usage

slxw(
  Y,
  tt,
  X = matrix(0, nrow(Y), 0),
  Z = matrix(1, nrow(Y), 1),
  niter = 100,
  nretain = 50,
  W_prior = W_priors(n = nrow(Y)/tt),
  beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)),
  sigma_prior = sigma_priors()
)

Arguments

Y

numeric N×1N \times 1 matrix containing the dependent variables, where N=nTN = nT is the number of spatial (nn) times the number of time observations (TT, with tt=T). Note that the observations have organized such that Y=[Y1,...,YT]Y = [Y_1',...,Y_T']'.

tt

single number greater or equal to 1. Denotes the number of time observations. tt=Ttt = T.

X

numeric N×k1N \times k_1 design matrix of independent variables. These will be automatically spatially lagged. If no spatially lagged variable is included in the model a matrix with NN rows and zero columns should be supplied (the default value). Note: either XX or ZZ has to be a matrix with at least one column.

Z

numeric N×k3N \times k_3 design matrix of independent variables which are not spatially lagged. The default value is a N×1N \times 1 vector of ones (i.e. an intercept for the model). Note: either XX or ZZ has to be a matrix with at least one column.

niter

single number greater or equal to 1, indicating the total number of draws. Will be automatically coerced to integer. The default value is 100.

nretain

single number greater or equal to 0, indicating the number of draws kept after the burn-in. Will be automatically coerced to integer. The default value is 50.

W_prior

list containing prior settings for estimating the spatial weight matrix WW. Generated by the smart constructor W_priors.

beta_prior

list containing priors for the slope coefficients β\beta, generated by the smart constructor beta_priors. The ordering of the priors is: (1) priors of XX, (2) priors of spatially lagged XX, (3) priors of ZZ.

sigma_prior

list containing priors for the error variance σ2\sigma^2, generated by the smart constructor sigma_priors

Details

The considered spatial panel SLX model with unknown (nn by nn) spatial weight matrix WW takes the form:

Yt=Xtβ1+WXtβ2+Zβ3+εt,Y_t = X_t \beta_1 + W X_t \beta_2 + Z \beta_3 + \varepsilon_t,

with εtN(0,Inσ2)\varepsilon_t \sim N(0,I_n \sigma^2) and W=f(Ω)W = f(\Omega). The nn by nn matrix Ω\Omega is an unknown binary adjacency matrix with zeros on the main diagonal and f()f(\cdot) is the (optional) row-standardization function.

YtY_t (n×1n \times 1) collects the nn cross-sectional (spatial) observations for time t=1,...,Tt=1,...,T. XtX_t (n×k1n \times k_1) and ZtZ_t (n×k2n \times k_2) are matrices of explanatory variables, where the former will also be spatially lagged. β1\beta_1 (k1×1k_1 \times 1), β2\beta_2 (k1×1k_1 \times 1) and β3\beta_3 (k2×1k_2 \times 1) are unknown slope parameter vectors.

After vertically staking the TT cross-sections Y=[Y1,...,YT]Y=[Y_1',...,Y_T']' (N×1N \times 1), X=[X1,...,XT]X=[X_1',...,X_T']' (N×k1N \times k_1) and Z=[Z1,...,ZT]Z=[Z_1', ..., Z_T']' (N×k2N \times k_2), with N=nTN=nT. The final model can be expressed as:

Y=Xβ1+W~Xβ2+Zβ3+ε,Y = X \beta_1 + \tilde{W} X \beta_2 + Z \beta_3 + \varepsilon,

where W~=ITW\tilde{W}=I_T \otimes W and εN(0,INσ2)\varepsilon \sim N(0,I_N \sigma^2). Note that the input data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.

Estimation usually even works well in cases of n>>Tn >> T. However, note that for applications with n>200n > 200 the estimation process becomes computationally demanding and slow. Consider in this case reducing niter and nretain and carefully check whether the posterior chains have converged.

Value

List with posterior samples for the slope parameters, σ2\sigma^2, WW, and average direct, indirect, and total effects.

Examples

set.seed(123)
n = 20; tt = 10
dgp_dat = sim_dgp(n = 20, tt = 10, rho = 0, beta1 = c(1,-1),
                  beta2 = c(3,-2.5), beta3 = c(.2), sigma2 = .05,
                  n_neighbor = 3,intercept = TRUE)
res = slxw(Y = dgp_dat$Y, tt = tt, X = dgp_dat$X, Z = dgp_dat$Z,
                  niter = 20, nretain = 10)

Set prior specifications for the spatial weight matrix

Description

Set prior specifications for the nn by nn spatial weight matrix W=f(Ω)W=f(\Omega), where Ω\Omega is an nn by nn unknown binary adjacency matrix (with zeros on the main diagonal), and f()f() denotes the (optional) row-standardization function

Usage

W_priors(
  n,
  W_prior = matrix(0.5, n, n),
  symmetric_prior = FALSE,
  row_standardized_prior = TRUE,
  nr_neighbors_prior = bbinompdf(0:(n - 1), nsize = n - 1, a = 1, b = 1, min_k = 0, max_k
    = n - 1)
)

Arguments

n

The number of spatial observations

W_prior

An nn by nn matrix of prior inclusion probabilities for WW

symmetric_prior

Binary value. Should the estimated adjacency matrix Ω\Omega be symmetric (default: FALSE)? if TRUE: Ω\Omega is forced symmetric; if FALSE: Ω\Omega not necessarily symmetric.

row_standardized_prior

Binary value. Should the estimated WW matrix be row-standardized (default: TRUE)? if TRUE: row-stochastic WW; if FALSE: WW not row-standardized.

nr_neighbors_prior

An nn dimensional vector of prior weights on the number of neighbors (i.e. the row sums of the adjacency matrix Ω\Omega), where the first element denotes the prior probability of zero neighbors and the last those of n1n-1. A prior using only fixed inclusion probabilities for the entries in Ω\Omega would be an nn dimensional vector of 1/n1/n. Defaults to a bbinompdf prior, with prior parameters a=1a = 1, b=1b = 1.


An R6 class for sampling the elements of WW

Description

An R6 class for sampling the elements of WW

An R6 class for sampling the elements of WW

Format

An R6Class generator object

Details

This class samples the spatial weight matrix. Use the function W_priors class for setup.

The sampling procedure relies on conditional Bernoulli posteriors outlined in Krisztin and Piribauer (2022).

Public fields

W_prior

The current W_priors

curr_w

numeric, non-negative nn by nn spatial weight matrix with zeros on the main diagonal. Depending on the W_priors settings can be symmetric and/or row-standardized.

curr_W

binary nn by nn spatial connectivity matrix Ω\Omega

curr_A

The current spatial projection matrix IρWI - \rho W.

curr_AI

The inverse of curr_A

curr_logdet

The current log-determinant of curr_A

curr_rho

single number between -1 and 1 or NULL, depending on whether the sampler updates the spatial autoregressive parameter ρ\rho. Set while invoking initialize or using the function set_rho.

spatial_error

Should a spatial error model be constructed? Defaults to FALSE.

Methods

Public methods


Method new()

Usage
W_sampler$new(W_prior, curr_rho = NULL, spatial_error = FALSE)
Arguments
W_prior

The list returned by W_priors

curr_rho

Optional single number between -1 and 1. Value of the spatial autoregressive parameter ρ\rho. Defaults to NULL, in which case no updates of the log-determinant, the spatial projection matrix, and its inverse are carried out.

spatial_error

Optional binary, specifying whether the sampler is for a spatial error model (TRUE) or for a spatial autoregressive specification (FALSE). Defaults to FALSE. If spatial_error = TRUE then a value curr_rho has to be supplied at initialization.


Method set_rho()

If the spatial autoregressive parameter ρ\rho is updated during the sampling procedure the log determinant, the spatial projection matrix IρWI - \rho W and it's inverse must be updated. This function should be used for a consistent update. At least the new scalar value for ρ\rho must be supplied.

Usage
W_sampler$set_rho(new_rho, newLogdet = NULL, newA = NULL, newAI = NULL)
Arguments
new_rho

single, number; must be between -1 and 1.

newLogdet

An optional value for the log determinant corresponding to newW and curr_rho

newA

An optional value for the spatial projection matrix using newW and curr_rho

newAI

An optional value for the matrix inverse of newA


Method sample_fast()

Usage
W_sampler$sample_fast(
  Y,
  curr_sigma,
  mu,
  lag_mu = matrix(0, nrow(tY), ncol(tY))
)
Arguments
Y

The nn by tttt matrix of responses

curr_sigma

The variance parameter σ2\sigma^2

mu

The nn by tttt matrix of means.

lag_mu

nn by tttt matrix of means that will be spatially lagged with the estimated WW. Defaults to a matrix with zero elements.


Method sample()

Usage
W_sampler$sample(Y, curr_sigma, mu, lag_mu = matrix(0, nrow(tY), ncol(tY)))
Arguments
Y

The nn by tttt matrix of responses

curr_sigma

The variance parameter σ2\sigma^2

mu

The nn by tttt matrix of means.

lag_mu

nn by tttt matrix of means that will be spatially lagged with the estimated WW. Defaults to a matrix with zero elements.

References

Krisztin, T., and Piribauer, P. (2022) A Bayesian approach for the estimation of weight matrices in spatial autoregressive models. Spatial Economic Analysis, 1-20.