<- function(side)
dist.matrix
{<- rep(1:side, times=side)
row.coords <- rep(1:side, each=side)
col.coords <- data.frame(row.coords, col.coords)
row.col <- dist(row.col, method="euclidean", diag=TRUE, upper=TRUE)
D <- as.matrix(D)
D return(D)
}
11 Autoregressive models
In general, autoregressive models should not be be fit with Jags, but with a
11.1 Example with Jags
This is based on code posted originally by Petr Keil http://www.petrkeil.com/?p=1910, to illustrate some comments I made in response to this blog post
11.1.1 Creating the data
This helper function that makes distance matrix for a side*side 2D array
Here is the function that simulates the autocorrelated 2D array with a given side, and with exponential decay given by lambda (the mean mu is constant over the array, it equals to global.mu)
<- function(side, global.mu, lambda)
cor.surface
{<- dist.matrix(side)
D # scaling the distance matrix by the exponential decay
<- exp(-lambda*D)
SIGMA <- rep(global.mu, times=side*side)
mu # sampling from the multivariate normal distribution
<- matrix(nrow=side, ncol=side)
M <- rmvnorm(1, mu, SIGMA)
M[] return(M)
}
OK, finally simulating the data
# parameters (the truth) that I will want to recover by JAGS
= 10
side = 0
global.mu = 0.3 # let's try something new
lambda
# simulating the main raster that I will analyze as data
<- cor.surface(side = side, lambda = lambda, global.mu = global.mu)
M image(M)
mean(M)
[1] 0.4496296
# simulating the inherent uncertainty of the mean of M:
#test = replicate(1000, mean(cor.surface(side = side, lambda = lambda, global.mu = global.mu)))
#hist(test, breaks = 40)
11.1.2 Fitting the model in JAGS
preparing the data
<- as.vector(as.matrix(M))
y <- list(N = side * side, D = dist.matrix(side), y = y) my.data
defining the model
= textConnection("
modelCode model
{
# priors
lambda ~ dgamma(1, 0.1)
global.mu ~ dnorm(0, 0.01)
for(i in 1:N)
{
# vector of mvnorm means mu
mu[i] <- global.mu
}
# derived quantities
for(i in 1:N)
{
for(j in 1:N)
{
# turning the distance matrix to covariance matrix
D.covar[i,j] <- exp(-lambda*D[i,j])
}
}
# turning covariances into precisions (that's how I understand it)
D.tau[1:N,1:N] <- inverse(D.covar[1:N,1:N])
# likelihood
y[1:N] ~ dmnorm(mu[], D.tau[,])
}
")
Running the model
<- jags(data=my.data,
fit parameters.to.save=c("lambda", "global.mu"),
model.file=modelCode,
n.iter=10000,
n.chains=3,
n.burnin=5000,
n.thin=5,
DIC=FALSE)
module glm loaded
module dic loaded
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 1
Unobserved stochastic nodes: 2
Total graph size: 10114
Initializing model
plot(as.mcmc(fit))
pairs(as.matrix(as.mcmc(fit)))