Hi, I want to use integrate_1d function in rstan in for loop. Even though it is working, it is not convergent.

Since it is to integrate directly such as \int_{0}^{ \mbox{observed_failure}[i]} \mbox{exp} (\rho * \beta_2 * treat[i] * x) dx, I also tried the code using the integrated value ( \mbox{exp}(\rho * \beta_2 * treat[i] * \mbox{observed_failure}[i] ) - 1) / (\rho * \beta_2 * treat[i]) and it was normally working and convergent. For this reason, I can say that there was a problem regarding integrate_1d codes which were the only parts that I changed.

```
functions {
real func(real x, real xc, real[] theta, real[] x_r, int[] x_i){
real beta2 = theta[1];
real rho = theta[2];
int treat = x_i[1];
return(exp(rho *beta2 * treat * x ));
}
}
data {
...
int treat[new_N];
...
}
transformed data {
real x_r[0]; // I do not have real data
int x_i[1]; // I only have treat data which is integer
}
parameters {
real beta0;
real beta1;
real beta2;
...
}
model {
...
for (i in 1:new_N) {
h[i] = h0 * exp(eta*age[i] + rho*( beta1*treat[i] + beta2 * treat[i] * observed_failure[i] + U[i]));
integ[i] = integrate_1d(func, 0, observed_failure[i], {beta2, rho}, x_r, {treat[i]}, 1e-8);
S[i] = exp(- h0 * exp(eta * age[i]) * exp(rho * (beta0 + beta1 * treat[i]) + U[i]) * integ[i]);
}
...
}
```

fit=stan(data=data1, model_code=stan_codes, iter=2000, warmup=1000,

thin=1, chains=chain, pars=pars, init=inits, control = list(adapt_delta = 0.99))

When I run fit above, it is working but not convergent which shows â€śThere were 1191 divergent transitions after warmupâ€ť.

Is my code about integrate_1d wrongâ€¦?

Here is my version of R:

R 4.0.2

x86_64-apple-darwin17.0

stan_version 2.21.0

Thank you for your help in advance!