##################################################
#### 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)
n <- nrow(Grid)
#### Split the area into two groups between which there will be a boundary.
groups <-rep(1, n)
groups[Grid$Var1>5] <- 2
#### set up distance and neighbourhood (W, based on sharing a common border) matrices
distance <-array(0, c(n,n))
W <-array(0, c(n,n))
for(i in 1:n)
{
for(j in 1:n)
{
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
}
}
#### Generate the covariates and response data
phi <- mvrnorm(n=1, mu=groups, Sigma=0.2 * exp(-0.1 * distance))
fitted <- -0.2 + phi
Y <- rnorm(n=n, mean=fitted, sd=rep(1,n))
#### Generate a dissimilarity metric
dissimilarity <- cbind(groups) + rnorm(n, sd=0.2)
dissimilarity.matrix <- as.matrix(dist(cbind(dissimilarity, dissimilarity),
method="manhattan", diag=TRUE, upper=TRUE)) * W/2
Z <- list(dissimilarity.matrix=dissimilarity.matrix)
#### Run the localised CAR model with binary weights
#### Let the function randomly generate starting values for the parameters
#### Use the default priors specified by the function (for details see the help files)
formula <- Y ~ 1
model <- gaussian.dissimilarityCAR(formula=formula, rho=0.99,
W=W, Z=Z, burnin=5000, n.sample=10000)
model <- gaussian.dissimilarityCAR(formula=formula, rho=0.99,
W=W, Z=Z, burnin=20, n.sample=50)
Run the code above in your browser using DataLab