Dear all,
expcov <- nimbleFunction(
run = function(dists = double(2), rho = double(0), sigma = double(0)) {
returnType(double(2))
n <- dim(dists)[1]
result <- matrix(nrow = n, ncol = n, init = FALSE)
sigma2 <- sigma*sigma
for(i in 1:n)
for(j in 1:n)
result[i, j] <- sigma2*exp(-dists[i,j]/rho)
return(result)
})
cExpcov <- compileNimble(expcov)
# model
model_exposure <- nimbleCode(
{
for(k in 1:N){
O[k] ~ dpois(lambda[k])
lambda[k] <- exp(log(E[k]) + beta_0 + b[X[k]])
}
# The Gaussian process for temperature
cov[1:Jb, 1:Jb] <- expcov(dists = dist_cov[1:Jb, 1:Jb],
rho = rho,
sigma = sigma)
b[1:Jb] ~ dmnorm(zeros[1:Jb], cov = cov[1:Jb, 1:Jb])
# sum-to-zero contraints
sum.constr <- sum(b[1:Jb])
one ~ dconstraint(sum.constr == 0)
# set the priors of the fixed effects
# intercept
beta_0 ~ dnorm(0, sd = 10)
# set the priors of the hyperparameters
sigma ~ dunif(0, 100)
rho ~ dunif(0, 20)
}
)
I have a couple of questions:
1. I would like to add sum to zero constraints on the temperature effect b but the moment I add the following lines:
sum.constr <- sum(b[1:Jb])
one ~ dconstraint(sum.constr == 0)
the b doesnt move. Am I approaching it in a wrong way?
2. I tried modifying the
expcov to make it a squared exponential covariance so it is kind of smoother but i get an error when taking the chol that the matrix is not positive definite. I also tried to use the absolute difference between the bins in the temperature domain and again is not pos def. Any ideas?
See attached the code and the data.
Best wishes,