##################################################
#### 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 <- T * K
#### 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
Q.W <- 0.99 * (diag(apply(W, 2, sum)) - W) + 0.01 * diag(rep(1,K))
Q.W.inv <- solve(Q.W)
phi.temp <- mvrnorm(n=1, mu=rep(0,K), Sigma=(0.1 * Q.W.inv))
phi <- phi.temp
for(i in 2:T)
{
phi.temp2 <- mvrnorm(n=1, mu=(0.8 * phi.temp), Sigma=(0.1 * Q.W.inv))
phi.temp <- phi.temp2
phi <- c(phi, phi.temp)
}
jump <- rep(c(rep(3, 70), rep(4, 30)),T)
LP <- jump + phi
fitted <- exp(LP)
Y <- rpois(n=N.all, lambda=fitted)
#### Run the model
model1 <- ST.CARadaptive(formula=Y~1, family="poisson", W=W, burnin=10000,
n.sample=50000)Run the code above in your browser using DataLab