##################################################
#### Run the model on simulated data on a lattice
##################################################
#### set up the regular lattice
x.easting <- 1:10
x.northing <- 1:10
Grid <- expand.grid(x.easting, x.northing)
K <- nrow(Grid)
T <- 10
N.all <- K * T
#### set up spatial neighbourhood matrix W
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
if(temp==1) W[i,j] <- 1
}
}
#### Simulate the elements in the linear predictor and the data
x <- rnorm(n=N.all, mean=0, sd=1)
beta <- 0.1
Q.W <- 0.99 * (diag(apply(W, 2, sum)) - W) + 0.01 * diag(rep(1,K))
Q.W.inv <- solve(Q.W)
phi <- mvrnorm(n=1, mu=rep(0,K), Sigma=(0.1 * Q.W.inv))
delta <- mvrnorm(n=1, mu=rep(0,K), Sigma=(0.1 * Q.W.inv))
trend <- array(NA, c(K, T))
time <-(1:T - mean(1:T))/T
for(i in 1:K)
{
trend[i, ] <- phi[i] + delta[i] * time
}
trend.vec <- as.numeric(trend)
LP <- 4 + x * beta + trend.vec
mean <- exp(LP)
Y <- rpois(n=N.all, lambda=mean)
#### Run the model
model <- ST.CARlinear(formula=Y~x, family="poisson", W=W, burnin=10000,
n.sample=50000)Run the code above in your browser using DataLab