I’m working on a hierarchical beta-binomial model with order constraints for prevalence in different age groups. Here is the description of the model and the Stan code I’ve written so far.

### Model Description

The hierarchical model can be described as follows:

The number of cases y_i in each age group i follows a binomial distribution:

y_i \sim \text{Binomial}(n_i, \pi_i)

where n_i is the number of individuals in age group i and \pi_i is the prevalence.

The prevalence \pi_i follows a beta distribution with order constraints:

\pi_i \sim \text{Beta}(\alpha_i, \beta_i) I(\pi_{i-1}, \pi_{i+1})

where I(\pi_{i-1}, \pi_{i+1}) is an indicator function ensuring that \pi_{i-1} \leq \pi_i \leq \pi_{i+1} .

Hyperpriors for (\alpha) and (\beta) are specified as truncated normal distributions:

\alpha_i \sim \text{Normal}(0, 1000) T[0, \infty)

\beta_i \sim \text{Normal}(0, 1000) T[0, \infty)

### Stan Code

Below is the Stan code I have written. I would appreciate any feedback or suggestions for improvements.

stan

```
data {
int<lower=1> Nage; // Number of age groups
vector[Nage] age; // Age vector
int<lower=0> posi[Nage]; // Number of positive cases
int<lower=0> ni[Nage]; // Number of trials
}
parameters {
vector<lower=0>[Nage] alpha;
vector<lower=0>[Nage] beta;
vector<lower=0,upper = 1>[Nage] pi;
}
model {
for( i in 1:Nage){
alpha[i] ~ normal(0,sqrt(1/0.001));
beta[i] ~ normal(0,sqrt(1/0.00001));
}
pi[1] ~ beta(alpha[1],beta[1])T[,pi[2]];
for(k in 2:(Nage-1)){
pi[k] ~ beta(alpha[k],beta[k])T[pi[k-1],pi[k+1]];
}
pi[Nage] ~ beta(alpha[Nage],beta[Nage])T[pi[Nage-1],0.96];
for( i in 1:Nage){
posi[i] ~ binomial(ni[i], pi[i]);
}
}
```

When I run the model, I encounter the following error message:

Chain 1: Initialization between (-2, 2) failed after 100 attempts. Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model. [1] “Error : Initialization failed.” [1] “error occurred during calling the sampler; sampling not done”

Do you have any ideas to fix this problem? Thank you for your help!