| 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 error (SEM), spatial Durbin (SDM), spatial error Durbin (SDEM) 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]
|
| Maintainer: | Tamas Krisztin <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.0 |
| Built: | 2026-05-15 08:33:04 UTC |
| Source: | https://github.com/tkrisztin/estimatew |
A hierarchical prior setup can be used in W_priors to anchor the prior
number of expected neighbors. Assuming a fixed prior inclusion probability
for the off-diagonal entries in the binary by adjacency matrix implies
that the number of neighbors (i.e. the row sums of ) follows a Binomial distribution
with a prior expected number of neighbors for the spatial observations of .
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 , the beta binomial prior accounts for the number of neighbors in each row of .
bbinompdf(x, nsize, a, b, min_k = 0, max_k = nsize)bbinompdf(x, nsize, a, b, min_k = 0, max_k = nsize)
x |
Number of neighbors (scalar) |
nsize |
Number of potential neighbors: |
a |
Scalar prior parameter |
b |
Scalar prior parameter |
min_k |
Minimum prior number of neighbors (defaults to 0) |
max_k |
Maximum prior number of neighbors (defaults to |
The beta-binomial distribution is the result of treating the prior inclusion probability
as random (rather than being fixed) by placing a hierarchical beta prior on it.
For the number of neighbors , the resulting prior on the elements of , ,
can be written as:
where is the Gamma function, and and
are hyperparameters from the beta prior. In the case of , the prior takes the
form of a discrete uniform distribution over the number of neighbors. By fixing
the prior can be anchored around the expected number of neighbors through
(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.
Prior density evaluated at x.
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.
This function allows the user to specify custom values for Gaussian priors on the slope parameters.
beta_priors( k, beta_mean_prior = matrix(0, k, 1), beta_var_prior = diag(k) * 100 )beta_priors( k, beta_mean_prior = matrix(0, k, 1), beta_var_prior = diag(k) * 100 )
k |
The total number of slope parameters in the model. |
beta_mean_prior |
numeric |
beta_var_prior |
A |
For the slope parameters the package uses common Normal
prior specifications. Specifically, .
This function allows the user to specify custom values for the prior hyperparameters
and . The default values correspond to weakly informative Gaussian priors with mean
zero and a diagonal prior variance-covariance matrix with on the main diagonal.
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).
This class samples slope parameters with a Gaussian prior from the conditional posterior. Use the beta_priors class for setup.
An R6Class generator object
beta_priorThe current beta_priors
curr_betaThe current value of
new()
beta_sampler$new(beta_prior)
beta_priorThe list returned by beta_priors
sample()
beta_sampler$sample(Y, X, curr_sigma)
YThe by matrix of responses
XThe by design matrix
curr_sigmaThe variance parameter
A four-parameter Beta specification as the prior for the spatial autoregressive parameter ,
as proposed by LeSage and Parent (2007) .
betapdf(rho, a = 1, b = 1, rmin = 0, rmax = 1)betapdf(rho, a = 1, b = 1, rmin = 0, rmax = 1)
rho |
The scalar value for |
a |
The first shape parameter of the Beta distribution |
b |
The second shape parameter of the Beta distribution |
rmin |
Scalar |
rmax |
Scalar |
The prior density is given by:
where () represents the Beta function,
.
Density value evaluated at rho.
LeSage, J. P., and Parent, O. (2007) Bayesian model averaging for spatial econometric models. Geographical Analysis, 39(3), 241-267.
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.
covidcovid
A data.frame object.
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.
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.
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.
logdetAinvUpdate(ch_ind, diff, AI, logdet)logdetAinvUpdate(ch_ind, diff, AI, logdet)
ch_ind |
vector of non-negative integers, between 1 and |
diff |
a numeric |
AI |
numeric |
logdet |
single number that is the log-determinant of the matrix |
Let be an invertible by matrix. is an by
column vector of real numbers and is a binary vector containing a single one and zeros otherwise.
Then the Matrix Determinant Lemma states that:
.
This provides an update to the determinant, but the inverse of has to be updated as well.
The Sherman-Morrison formula proves useful:
.
Using these two formulas, an efficient update of the spatial projection matrix determinant can be achieved.
A list containing the updated by matrix , as well as the
updated log determinant of
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.
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
, where is a by spatial weight matrix. However, direct computation
of the log-determinant is computationally expensive.
logdetPaceBarry(W, length.out = 200, rmin = -1, rmax = 1)logdetPaceBarry(W, length.out = 200, rmin = -1, rmax = 1)
W |
numeric |
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 |
rmax |
single number between -1 and 1. Sets the maximum value of the
spatial autoregressive parameter |
This function wraps the log-determinant approximation by Barry and Pace (1999), which
can be used to pre-compute the log-determinants over a grid of values.
numeric length.out by 2 matrix; the first column
contains the approximated log-determinants the second column the values
ranging between rmin and rmax.
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.
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
, where is a by spatial weight matrix. However, direct computation
of the log-determinant is computationally expensive.
logdetSpline(W, length.out = 200, rmin = -1, rmax = 1)logdetSpline(W, length.out = 200, rmin = -1, rmax = 1)
W |
numeric |
length.out |
single, integer number, has to be at least 11 (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 |
rmax |
single number between -1 and 1. Sets the maximum value of the
spatial autoregressive parameter |
This function uses a grid search and splines to pre-compute the log-determinants over a grid of values.
numeric length.out by 2 matrix; the first column
contains the approximated log-determinants the second column the values
ranging between rmin and rmax.
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.
normalgamma( Y, tt, X = matrix(1, nrow(Y), 1), niter = 200, nretain = 100, beta_prior = beta_priors(k = ncol(X)), sigma_prior = sigma_priors() )normalgamma( Y, tt, X = matrix(1, nrow(Y), 1), niter = 200, nretain = 100, beta_prior = beta_priors(k = ncol(X)), sigma_prior = sigma_priors() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
X |
numeric |
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 |
sigma_prior |
list containing priors for the error variance |
The considered model takes the form:
with .
() collects the cross-sectional observations for time
. () is a matrix of explanatory variables.
() is an unknown slope parameter matrix.
After vertically staking the cross-sections (),
(), with , the final model can be expressed as:
where . Note that the input
data matrices have to be ordered first by the cross-sectional (spatial) units and then stacked by time.
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)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)
Annual growth rates of GVA per worker (labor productivity) for 90 NUTS-1 regions, 2001-2019 (T=19). Explanatory variables are lagged by one year.
nuts1growthnuts1growth
A data frame with 1710 observations (90 regions × 19 years) and 6 variables:
NUTS1NUTS-1 region code
yearYear (2001-2019)
growth_gdp_pwAnnual growth rate of GVA per worker (log differences), for year year vs. year-1
init_gdp_pwLog initial GVA per worker (measured at year-1)
loweducShare of working-age population with low education (ISCED 0-2, at year-1)
higheducShare of working-age population with tertiary education (ISCED 5-8, at year-1)
A data.frame object.
The dataset contains annual regional data for 26 European Union countries, disaggregated at the NUTS-1 level. The countries covered are: Austria, Belgium, Bulgaria, Cyprus, Czechia, Denmark, Estonia, Finland, France, Germany, Hungary, Ireland, Italy, Lithuania, Luxembourg, Latvia, Malta, Netherlands, Poland, Portugal, Romania, Sweden, Slovenia, and Slovakia. The NUTS-1 codes identify the first-level administrative regions within these countries.
ARDECO (European Commission DG REGIO) and Eurostat regional database.
Piribauer, P., Glocker, C., & Krisztin, T. (2023). Beyond distance: The spatial relationships of European regional economic growth. Journal of Economic Dynamics and Control, 155, 104735.
Graphical plot of the posterior probabilities of the estimated adjacency matrix .
## S3 method for class 'estimateW' plot( x, cols = c("white", "lightgrey", "black"), breaks = c(0, 0.5, 0.75, 1), ... )## S3 method for class 'estimateW' plot( x, cols = c("white", "lightgrey", "black"), breaks = c(0, 0.5, 0.75, 1), ... )
x |
|
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
## S3 method for class 'sim_dgp' plot(x, ...)## S3 method for class 'sim_dgp' plot(x, ...)
x |
|
... |
further arguments are passed on to the invoked |
Specify prior for the spatial autoregressive parameter and sampling settings
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, use_pace_barry = TRUE, mh_tune_low = 0.4, mh_tune_high = 0.6, mh_tune_scale = 0.1 )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, use_pace_barry = TRUE, mh_tune_low = 0.4, mh_tune_high = 0.6, mh_tune_scale = 0.1 )
rho_a_prior |
Single number. Prior hyperparameter for the four-parameter beta distribution |
rho_b_prior |
Single number. Prior hyperparameter for the four-parameter beta distribution |
rho_min |
Minimum value for |
rho_max |
Maximum value for |
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 |
use_pace_barry |
Binary value. If griddy-Gibbs is used should this be done using
Pace and Barry or spline approximation |
mh_tune_low |
Lower bound of acceptance rate for Metropolis-Hastings tuning
(used if |
mh_tune_high |
Upper bound of acceptance rate for Metropolis-Hastings tuning
(used if |
mh_tune_scale |
Scaling factor for Metropolis-Hastings tuning
(used if |
An R6 class for sampling the spatial autoregressive parameter
An R6 class for sampling the spatial autoregressive parameter
An R6Class generator object
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).
rho_priorThe current rho_priors
curr_rhoThe current value of
curr_WThe current spatial weight matrix ; an by matrix.
curr_AThe current spatial filter matrix .
curr_AIThe inverse of curr_A
curr_logdetThe current log-determinant of curr_A
curr_logdetsA set of log-determinants for various values of . See the
rho_priors function for settings of step site and other parameters of the grid.
new()
rho_sampler$new(rho_prior, W = NULL)
rho_priorThe list returned by rho_priors
WAn optional starting value for the spatial weight matrix
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.
rho_sampler$stopMHtune()
setW()
rho_sampler$setW(newW, newLogdet = NULL, newA = NULL, newAI = NULL)
newWThe updated spatial weight matrix .
newLogdetAn optional value for the log determinant corresponding to newW and curr_rho.
newAAn optional value for the spatial projection matrix using newW and curr_rho.
newAIAn optional value for the matrix inverse of newA.
sample()
rho_sampler$sample(Y, mu, sigma)
YThe by matrix of responses.
muThe by matrix of means.
sigmaThe variance parameter .
sample_Griddy()
rho_sampler$sample_Griddy(Y, mu, sigma)
YThe by matrix of responses.
muThe by matrix of means.
sigmaThe variance parameter .
sample_MH()
rho_sampler$sample_MH(Y, mu, sigma)
YThe by matrix of responses.
muThe by matrix of means.
sigmaThe variance parameter .
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.
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 . The function is
used as an illustration on using the beta_sampler, sigma_sampler,
and rho_sampler classes.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
W |
numeric, non-negative and row-stochastic |
Z |
numeric |
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 |
beta_prior |
list containing priors for the slope coefficients,
generated by the smart constructor |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial autoregressive model (SAR) takes the form:
with . The row-stochastic by spatial weight
matrix is non-negative and has zeros on the main diagonal. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () is a matrix of explanatory variables.
() is an unknown slope parameter matrix.
After vertically staking the cross-sections (),
(), with , the final model can be expressed as:
where and . 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.
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)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)
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 .
This is a wrapper function calling sdmw with no spatially lagged exogenous variables.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
Z |
numeric |
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 |
rho_prior |
list of prior settings for estimating |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial autoregressive model (SAR) with unknown ( by ) spatial weight
matrix takes the form:
with and . The by
matrix is an unknown binary adjacency matrix with zeros on the main diagonal and
is the (optional) row-standardization function. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () is a matrix of explanatory variables.
() is an unknown slope parameter vector.
After vertically staking the cross-sections (),
and (), with . The final model can be expressed as:
where and . 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 . However, note that for applications with 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.
List with posterior samples for the slope parameters, , , ,
and average direct, indirect, and total effects.
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)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)
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 . The function is
used as an illustration on using the beta_sampler, sigma_sampler,
and rho_sampler classes.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
W |
numeric, non-negative and row-stochastic |
X |
numeric |
Z |
numeric |
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 |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial Durbin error model (SDEM) takes the form:
with . The row-stochastic by spatial weight
matrix is non-negative and has zeros on the main diagonal. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () and () are
matrices of explanatory variables, where the former will also be spatially lagged.
(), () and ()
are unknown slope parameter vectors.
After vertically staking the cross-sections (),
() and (),
with , the final model can be expressed as:
where . Note that the input
data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.
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)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)
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 . It is a wrapper around W_sampler.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
X |
numeric |
Z |
numeric |
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 |
rho_prior |
list of prior settings for estimating |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial Durbin error model (SDEM) with unknown ( by ) spatial weight
matrix takes the form:
with and . The by
matrix is an unknown binary adjacency matrix with zeros on the main diagonal and
is the (optional) row-standardization function. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () and () are
matrices of explanatory variables, where the former will also be spatially lagged.
(), () and ()
are unknown slope parameter vectors.
After vertically staking the cross-sections (),
() and (),
with . The final model can be expressed as:
where . 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 . However, note that for applications with 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.
List with posterior samples for the slope parameters, , , ,
and average direct, indirect, and total effects.
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)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)
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 . The function is
used as an illustration on using the beta_sampler, sigma_sampler,
and rho_sampler classes.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
W |
numeric, non-negative and row-stochastic |
X |
numeric |
Z |
numeric |
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 |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial Durbin model (SDM) takes the form:
with . The row-stochastic by spatial weight
matrix is non-negative and has zeros on the main diagonal. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () and () are
matrices of explanatory variables, where the former will also be spatially lagged.
(), () and ()
are unknown slope parameter vectors.
After vertically staking the cross-sections (),
() and (),
with , the final model can be expressed as:
where and . Note that the input
data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.
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)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)
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 . It is a wrapper around W_sampler.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
X |
numeric |
Z |
numeric |
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 |
rho_prior |
list of prior settings for estimating |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial Durbin model (SDM) with unknown ( by ) spatial weight
matrix takes the form:
with and . The by
matrix is an unknown binary adjacency matrix with zeros on the main diagonal and
is the (optional) row-standardization function. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () and () are
matrices of explanatory variables, where the former will also be spatially lagged.
(), () and ()
are unknown slope parameter vectors.
After vertically staking the cross-sections (),
() and (),
with . The final model can be expressed as:
where and . 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 . However, note that for applications with 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.
List with posterior samples for the slope parameters, , , ,
and average direct, indirect, and total effects.
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)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)
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 . The function is
used as an illustration on using the beta_sampler, sigma_sampler,
and rho_sampler classes.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
W |
numeric, non-negative and row-stochastic |
Z |
numeric |
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 |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial error model (SEM) takes the form:
with . The row-stochastic by spatial weight
matrix is non-negative and has zeros on the main diagonal. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () is a
matrix of explanatory variables, where the former will also be spatially lagged.
() is an unknown slope parameter vector.
After vertically staking the cross-sections ()
and (),
with , the final model can be expressed as:
where . Note that the input
data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.
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)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)
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 .
This is a wrapper function calling sdemw with no spatially lagged exogenous variables.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
Z |
numeric |
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 |
rho_prior |
list of prior settings for estimating |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered panel spatial error model (SEM) with unknown ( by ) spatial weight
matrix takes the form:
with and . The by
matrix is an unknown binary adjacency matrix with zeros on the main diagonal and
is the (optional) row-standardization function. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () is a matrix of explanatory variables.
() is an unknown slope parameter vector.
After vertically staking the cross-sections (),
and (), with . The final model can be expressed as:
where .
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 . However, note that for applications with 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.
List with posterior samples for the slope parameters, , , ,
and average direct, indirect, and total effects.
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)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
sigma_priors(sigma_rate_prior = 0.001, sigma_shape_prior = 0.001)sigma_priors(sigma_rate_prior = 0.001, sigma_shape_prior = 0.001)
sigma_rate_prior |
Sigma rate prior parameter (scalar), default: |
sigma_shape_prior |
Sigma shape prior parameter (scalar), default: This function allows the user to specify priors for the error variance |
This class samples nuisance parameter which an inverse Gamma prior from the conditional posterior. Use the sigma_priors class for setup.
An R6Class generator object
sigma_priorThe current sigma_priors
curr_sigmaThe current value of
new()
sigma_sampler$new(sigma_prior)
sigma_priorThe list returned by sigma_priors
sample()
sigma_sampler$sample(Y, mu)
YThe by matrix of responses
muThe by matrix of means
This function can be used to generate data from a data generating process for SDM, SAR, SEM, and SLX type models.
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 )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 )
n |
Number of spatial observations |
tt |
Number of time observations |
rho |
The true |
beta1 |
Vector of dimensions |
beta2 |
Vector of dimensions |
beta3 |
Vector of dimensions |
sigma2 |
The true |
n_neighbor |
Number of neighbors for the generated |
W |
Exogeneous spatial weight matrix for the data generating process. Defaults to
|
do_symmetric |
Should the generated spatial weight matrix be symmetric? (default: FALSE) |
intercept |
Should the first column of |
spatial_error |
Should a spatial error model be constructed? Defaults to |
For the SDM, SAR, and SLX models the generated spatial panel model takes the form
with .
For the SEM model the generated spatial panel model takes the form
with .
The function generates the vector . The elements of the explanatory variable matrices
() and () are randomly generated from a Gaussian
distribution with zero mean and unity variance ().
The non-negative, row-stochastic by matrix 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 , , and , as well as
and have to be provided by the user. The length of and
have to be equal.
A spatial Durbin model (SDM) is constructed if is not equal to zero,
spatial_error is FALSE, and , , and are all supplied by the user.
A spatial autoregressive model is constructed if is not equal to zero,
spatial_error is FALSE, and only is supplied by the user.
An SLX type model is constructed if is equal to zero, spatial_error is FALSE,
and , are supplied by the user.
An SEM type model is constructed if spatial_error is TRUE and either only
or , , and are supplied by the user.
A list with the generated , and and a list of parameters.
# 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)# 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)
The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters. The function is
used as an illustration on using the beta_sampler and sigma_sampler classes.
slx( Y, tt, W, X = matrix(0, nrow(Y), 0), Z = matrix(1, nrow(Y), 1), niter = 100, nretain = 50, beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)), sigma_prior = sigma_priors() )slx( Y, tt, W, X = matrix(0, nrow(Y), 0), Z = matrix(1, nrow(Y), 1), niter = 100, nretain = 50, beta_prior = beta_priors(k = ncol(X) * 2 + ncol(Z)), sigma_prior = sigma_priors() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
W |
numeric, non-negative and row-stochastic |
X |
numeric |
Z |
numeric |
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 |
sigma_prior |
list containing priors for the error variance |
The considered spatial panel SLX model takes the form:
with . The row-stochastic by spatial weight
matrix is non-negative and has zeros on the main diagonal. is a scalar spatial autoregressive parameter.
() collects the cross-sectional (spatial) observations for time
. () and () are
matrices of explanatory variables, where the former will also be spatially lagged.
(), () and ()
are unknown slope parameter vectors.
After vertically staking the cross-sections (),
() and (),
with . The final model can be expressed as:
where and . Note that the input
data matrices have to be ordered first by the cross-sectional spatial units and then stacked by time.
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 = slx(Y = dgp_dat$Y, tt = tt, W = dgp_dat$W, X = dgp_dat$X, Z = dgp_dat$Z, niter = 20, nretain = 10)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 = slx(Y = dgp_dat$Y, tt = tt, W = dgp_dat$W, X = dgp_dat$X, Z = dgp_dat$Z, niter = 20, nretain = 10)
The sampler uses independent Normal-inverse-Gamma priors for the slope and variance parameters.
It is a wrapper around W_sampler.
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() )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() )
Y |
numeric |
tt |
single number greater or equal to 1. Denotes the number of time observations. |
X |
numeric |
Z |
numeric |
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 |
beta_prior |
list containing priors for the slope coefficients |
sigma_prior |
list containing priors for the error variance |
The considered spatial panel SLX model with unknown ( by ) spatial weight
matrix takes the form:
with and . The by
matrix is an unknown binary adjacency matrix with zeros on the main diagonal and
is the (optional) row-standardization function.
() collects the cross-sectional (spatial) observations for time
. () and () are
matrices of explanatory variables, where the former will also be spatially lagged.
(), () and ()
are unknown slope parameter vectors.
After vertically staking the cross-sections (),
() and (),
with . The final model can be expressed as:
where and . 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 . However, note that for applications with 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.
List with posterior samples for the slope parameters, , ,
and average direct, indirect, and total effects.
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.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 by spatial weight matrix ,
where is an by unknown binary adjacency matrix (with zeros on the
main diagonal), and denotes the (optional) row-standardization function
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) )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) )
n |
The number of spatial observations |
W_prior |
An |
symmetric_prior |
Binary value. Should the estimated adjacency matrix |
row_standardized_prior |
Binary value. Should the estimated |
nr_neighbors_prior |
An |
An R6 class for sampling the elements of
An R6 class for sampling the elements of
An R6Class generator object
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).
W_priorThe current W_priors
curr_wnumeric, non-negative by spatial weight matrix with zeros
on the main diagonal. Depending on the W_priors settings can be symmetric and/or
row-standardized.
curr_Wbinary by spatial connectivity matrix
curr_AThe current spatial projection matrix .
curr_AIThe inverse of curr_A
curr_logdetThe current log-determinant of curr_A
curr_rhosingle number between -1 and 1 or NULL, depending on whether the sampler updates
the spatial autoregressive parameter . Set while invoking initialize
or using the function set_rho.
spatial_errorShould a spatial error model be constructed? Defaults to FALSE.
new()
W_sampler$new(W_prior, curr_rho = NULL, spatial_error = FALSE)
W_priorThe list returned by W_priors
curr_rhoOptional single number between -1 and 1. Value of the spatial autoregressive
parameter . Defaults to NULL, in which case no updates of the log-determinant, the spatial
projection matrix, and its inverse are carried out.
spatial_errorOptional 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.
set_rho()
If the spatial autoregressive parameter is updated during the sampling procedure the log determinant, the
spatial projection matrix and it's inverse must be updated. This function should be
used for a consistent update. At least the new scalar value for must be supplied.
W_sampler$set_rho(new_rho, newLogdet = NULL, newA = NULL, newAI = NULL)
new_rhosingle, number; must be between -1 and 1.
newLogdetAn optional value for the log determinant corresponding to newW and curr_rho
newAAn optional value for the spatial projection matrix using newW and curr_rho
newAIAn optional value for the matrix inverse of newA
sample()
W_sampler$sample(Y, curr_sigma, mu, lag_mu = matrix(0, nrow(tY), ncol(tY)))
YThe by matrix of responses
curr_sigmaThe variance parameter
muThe by matrix of means.
lag_mu by matrix of means that will be spatially lagged with
the estimated . Defaults to a matrix with zero elements.
Krisztin, T., and Piribauer, P. (2022) A Bayesian approach for the estimation of weight matrices in spatial autoregressive models. Spatial Economic Analysis, 1-20.