Hi all,
I hope I find you well.
I have been trying to fit a very simple BYM model in NIMBLE. The code is as follows:
BYMCode <- nimbleCode(
{
for (i in 1:N){
O[i] ~ dpois(mu[i]) # Poisson likelihood for observed counts
log(mu[i]) <- log(E[i]) + alpha + theta[i] + phi[i]
theta[i] ~ dnorm(0, tau = tau.theta) # area-specific RE
RR[i] <- exp(alpha + theta[i] + phi[i]) # area-specific RR
resRR[i] <- exp(theta[i] + phi[i]) # area-specific residual RR
e[i] <- (O[i]-mu[i])/sqrt(mu[i]) # residuals
proba.resRR[i]<-step(resRR[i]-1) # Posterior probability
}
# BYM prior
phi[1:N] ~ dcar_normal(adj = adj[1:L], weights = weights[1:L], num = num[1:N], tau = tau.phi)
# Priors
alpha ~ dflat() # vague prior (Unif(-inf, +inf))
overallRR <- exp(alpha) # overall RR across study region
tau.theta ~ dgamma(0.5, 0.05) # prior for the precision hyperparameter
sigma2.theta <- 1/tau.theta # variance of unstructured area random effects
tau.phi ~ dgamma(0.5, 0.0005) # prior on precison of spatial area random effects
sigma2.phi <- 1/tau.phi # conditional variance of spatial area random effects
sigma2.phi.marginal <- sd(phi[1:N])*sd(phi[1:N]) # empirical marginal variance of spatial effects
frac.spatial <- sigma2.phi.marginal/( sigma2.phi.marginal + sigma2.theta) # fraction of total variation in log RR
}
)
n.LTLA <- dim(COVID19Deaths)[1]
# Format the data for NIMBLE in a list
COVIDdata = list(
O = COVID19Deaths$deaths # observed nb of deaths
)
COVIDConsts <-list(
N = n.LTLA, # nb of LTLAs
L = length(nbWB_A$weights), # the number of neighboring areas
E = COVID19Deaths$expected, # expected number of deaths
adj = nbWB_A$adj, # the elements of the neighbouring matrix
num = nbWB_A$num,
weights = nbWB_A$weights
)
inits <- list(
list(alpha = 0.01,
tau.theta = 10,
tau.phi = 1,
theta = rep(0.01, times = 317),
phi = c(rep(0.5, times = 158), 0, rep(-0.5, times = 158))
),
list(alpha = 0.5,
tau.theta = 1,
tau.phi = 0.1,
theta = rep(0.05, times = 317),
phi = c(rep(0.05, times = 158), 0, rep(-0.05, times = 158))
)
)
params <- c("sigma2.theta", "sigma2.phi","overallRR", "RR", "theta",
"resRR", "frac.spatial", "sigma2.phi.marginal", "proba.resRR", "mu")
ni <- 50000 #nb iterations
nt <- 5 #thinning interval
nb <- 10000 #nb iterations as burning
nc <- 2 #nb chains
t0<- Sys.time()
modelBYM.sim <- nimbleMCMC(code = BYMCode,
data = COVIDdata,
constants = COVIDConsts,
inits = inits,
monitors = params,
niter = ni,
nburnin = nb,
thin = nt,
nchains = nc,
setSeed = 9,
progressBar = TRUE,
samplesAsCodaMCMC = TRUE,
summary = TRUE,
WAIC = TRUE
)
t1<- Sys.time()
t1 - t0
So the model runs smoothly if you exclude the phi (ie the ICAR component). I have tried several things, but imposing sum to zero constraints is important because the intercept is smth I need to have. I have tried several things:
1. If I remove the zero_mean = 1, it works but it never converges, I have tried a lot of different niter. I have read that it could be better if I remove the intercept under the zero_mean specification, but I want to keep the intercept.
2. I have played with the priors of the hyperparameters and intercept.
3. Notice that I imposed also the contraints as we used to do in winbugs (making the initial values to sum to zero) but this didnt work too
any ideas?
Best wishes,
Gary