##################################################
#### Run the model on simulated data on a lattice
##################################################
#### Set up a square lattice region
x.easting <- 1:10
x.northing <- 1:10
Grid <- expand.grid(x.easting, x.northing)
K <- nrow(Grid)
#### set up distance and neighbourhood (W, based on sharing a common border) matrices
distance <-array(0, c(K,K))
W <-array(0, c(K,K))
for(i in 1:K)
{
for(j in 1:K)
{
temp <- (Grid[i,1] - Grid[j,1])^2 + (Grid[i,2] - Grid[j,2])^2
distance[i,j] <- sqrt(temp)
if(temp==1) W[i,j] <- 1
}
}
K <- nrow(W)
#### Generate the correlation structures
Q.W <- 0.99 * (diag(apply(W, 2, sum)) - W) + 0.01 * diag(rep(1,K))
Q.W.inv <- solve(Q.W)
Sigma <- matrix(c(1,0.5,0, 0.5,1,0.3, 0, 0.3, 1), nrow=3)
Sigma.inv <- solve(Sigma)
J <- nrow(Sigma)
precision.phi <- kronecker(Q.W, Sigma.inv)
var.phi <- solve(precision.phi)
#### Generate data
N.all <- K * J
x1 <- rnorm(N.all)
x2 <- rnorm(N.all)
phi <- mvrnorm(n=1, mu=rep(0,N.all), Sigma=var.phi)
lp <- 0.1 * x1 - 0.1 * x2 + phi
prob <- exp(lp) / (1 + exp(lp))
trials <- rep(100,N.all)
Y <- rbinom(n=N.all, size=trials, prob=prob)
#### Run the Leroux model
formula <- Y ~ x1 + x2
model <- MVS.CARleroux(formula=formula, family="binomial",
trials=trials, W=W, burnin=20000, n.sample=100000)Run the code above in your browser using DataLab