# Translating a bayesian model from MCMCpack (hregress) to JAGS

I am trying to convert a hierarchical model that currently works with the R package MCMCpack, into a JAGS version. I have experience working with regular models in JAGS, but not with hierarchical models.

With the MCMChregress function, my model translates into:

mcmc_model <- MCMChregress(fixed= Y ~ X1 + X2 + X3 + X4+ X5, random=~X1+X2, group="L2", data=mydata, burnin=10000, mcmc=10000, thin=1,verbose=1, r=3, R=diag(c(1,0.1,0.1)), nu=0.001, delta=0.001)


However, I am finding it difficult to write it down in JAGS. So far, I have done the following:

jags_model <- function() {

for (i in 1:N){ #first level model
Y[i] ~ dnorm(mu[i], tau.y)
mu[i] <- b0[L2[i]]
+ b1[L2[i]] * X1[i]
+ b2[L2[i]] * X2[i]
}
for (j in 1:L){
b0[j] <- xi.b0*B.raw[j,1]
b1[j] <- xi.b1*B.raw[j,2]
b2[j] <- xi.b2*B.raw[j,3]
B.raw[j,1:3] ~ dmnorm(B.raw.hat[j,], Tau.B.raw[,])
B.raw.hat[j,1] <- g00 + g01*X3[j] + g02*X4[j] + g03*X5[j]
B.raw.hat[j,2] <- mu.b1.raw
B.raw.hat[j,3] <- mu.b2.raw
}

g00 <- xi.b0*mu.b0.raw
g01 <- xi.b0*mu.b0.raw
g02 <- xi.b0*mu.b0.raw
g03 <- xi.b0*mu.b0.raw
mu.b0.raw ~ dnorm(0,.0001)
mu.b1.raw ~ dnorm(0,.0001)
mu.b2.raw ~ dnorm(0,.0001)
xi.b0 ~ dunif(0,100)
xi.b1 ~ dunif(0,100)
xi.b2 ~ dunif(0,100)

Tau.B.raw[1:3,1:3] ~ dwish(W[,], df)
df <- 4
Sigma.B.raw[1:3,1:3] <- inverse(Tau.B.raw[,])
tau.y <- pow(sigma.y, -2)
sigma.y ~ dunif(0,100)

}


However, although the fixed effects end up being the same with MCMChregress function in R and with JAGS, the random effects are very dissimilar. Especially the group-varying slopes (b1_j and b2_j). Do someone know how could I achieve such an specification that mimics MCMChregress?

Any suggestions will be very helpful.

EDIT: I am editing the question to follow a request made by @Patrick Coulombe in a comment below (since I cannot comment myself yet). So, to better understand the precise specification that runs in the background of MCMChregress, one could look here: http://rgm3.lab.nig.ac.jp/RGM/R_rdfile?f=MCMCpack/man/MCMChregress.Rd&d=R_CC

My intention was to model the impact of X1 and X2 (variables of interest, both in the first level) on Y, and the extract the varying slopes for them in each of the L2 groups. The control variables X3, X4 and X5 are measured at the second level.

• Would love to try to help, but I don't know MCMCpack's syntax, so I cannot be certain what model you're fitting. Is there any chance you could also show your model with mathematical expressions? – Patrick Coulombe Apr 7 '15 at 3:34
• You should look at the WinBUGS examples. mrc-bsu.cam.ac.uk/wp-content/uploads/WinBUGS_Vol1.pdf. There are three volumes in total. JAGS syntax is identical in many ways to *BUGS code. Since you have not posted the model (only MCMCpack code), it is very difficult to help you. First off, I would say that you probably need your second for loop inside your first one. – stoched Apr 7 '15 at 10:53