require(spatialprobit)
# number of observations
n <- 10
# true parameters
beta <- c(0, 1, -1)
rho <- 0.75
# design matrix with two standard normal variates as "covariates"
X <- cbind(intercept=1, x=rnorm(n), y=rnorm(n))
# sparse identity matrix
I_n <- sparseMatrix(i=1:n, j=1:n, x=1)
# number of nearest neighbors in spatial weight matrix W
m <- 6
# spatial weight matrix with m=6 nearest neighbors
W <- sparseMatrix(i=rep(1:n, each=m),
j=replicate(n, sample(x=1:n, size=m, replace=FALSE)), x=1/m, dims=c(n, n))
# innovations
eps <- rnorm(n=n, mean=0, sd=1)
# generate data from model
S <- I_n - rho * W
z <- solve(qr(S), X %*% beta + eps)
y <- as.vector(z >= 0) # 0 or 1, FALSE or TRUE
# estimate SAR probit model
set.seed(12345)
sarprobit.fit1 <- sar_probit_mcmc(y, X, W, ndraw=1000, burn.in=50,
thinning=1, prior=NULL, computeMarginalEffects=TRUE)
summary(sarprobit.fit1)
# average direct effects
colMeans(sarprobit.fit1$direct)
me <- marginal.effects(sarprobit.fit1)
colMeans(me$direct)
plot(density(me$direct[,1]))
lines(density(sarprobit.fit1$direct[,1]), col="red")
plot(me$direct[,1],sarprobit.fit1$direct[,1])
abline(a=0, b=1, lty=1, col="red")
# problem: both should be the same, wrong:
# tr(W^i) is determined with simulation, so there will be different realisations in sarprobit.fit1
# and marginal.effects.sarprobit()
quantile(me$direct[,1], prob=c(0.025, 0.975)) # 95% confidence interval
quantile(sarprobit.fit1$direct[,1], prob=c(0.025, 0.975))Run the code above in your browser using DataLab