I am fitting the following hierarchical model with Stan:

With priors that you will see in my Stan code. Note that I model \gamma_{i1} and \gamma_{i1} based on the marginal and conditional distributions, which means putting a prior on the correlation.The code is as follows:

```
data {
int ID[108];
vector[108] Age;
vector[108] dist;
vector[108] Male;
}
parameters {
vector[3] beta;
matrix[27, 2] gam;
vector<lower=0, upper = 10>[2] sigg;
real<lower=0,upper=1> rhog;
real<lower=0, upper = 10> sig;
}
transformed parameters {
vector[108] mu;
vector[27] cmg;
vector<lower=0>[27] csd;
for(i in 1:27) {
cmg[i] = rhog * (sigg[1] / sigg[2]) * (gam[i,1] - beta[1]);
csd[i] = sqrt((sigg[2] ^ 2) * (1 - rhog^2));
}
for(i in 1:108) {
mu[i] = gam[ID[i],1] + beta[2]*Age[i] + gam[ID[i],2]*Age[i] + beta[3]*Male[i];
}
}
model {
for(i in 1:08) {
dist[i] ~ normal(mu[i], sig);
}
for(i in 1:27) {
gam[i, 1] ~ normal(beta[1], sigg[1]);
gam[i, 2] ~ normal(cmg[i], csd[i]);
}
for(i in 1:3) {
beta[i] ~ normal(0, 10);
}
for(j in 1:2) {
sigg[j] ~ uniform(0, 10);
}
sig ~ uniform(0, 10);
rhog ~ uniform(0, 1);
}
```

To obtain the data and fit the model in R, I use the following code:

```
library(rstan)
library(coda)
library(ggmcmc)
df <- read.table('http://people.oregonstate.edu/~calverta/BIDA/Chapter10/DentalData.txt',
header = TRUE)
data <- list(dist = df$Distance,
Age = df$Age,
ID = df$ID1,
Male = df$Male)
stan_fit <- stan(file = 'Dental.stan', data = data, chains = 3,
control=list(adapt_delta=.99, max_treedepth = 15),
iter = 5000)
### Show that trace plots for sigg do not look good
### Need ggmcmc package to do this
# install.packages('ggmcmc')
stan_df <- ggs(stan_fit, family = 'sigg')
ggs_traceplot(stan_df)
```

Fitting this model in Stan results in divergent transition warnings. When I fit it using JAGS, I don’t have any issues. Does anyone have suggestions to help fit this model?