Hello everyone. I am new to Stan and R.

I am trying to run a model for a 1 compartment model in pharmacokinetics.

I found an example on the web and adapted the model .

The model code is saved as stan_model_code in the working directory

```
data {
int<lower=0> nIV;
real<lower=0> doseIV;
vector<lower=0>[nIV] timeIV;
vector<lower=0>[nIV] concIV;
}
parameters {
real<lower=0> CL;
real<lower=0> V;
real<lower=0> kIV;
real<lower=0> cIV;
real<lower=0,upper=100> sigma;
}
transformed parameters {
real k;
real c0;
real AUCIV;
real t0_5;
vector[nIV] predIV;
k = CL / V;
c0 = doseIV / V;
AUCIV = doseIV / CL + cIV / kIV;
t0_5 = log(2) / k;
predIV = c0 * exp(-k * timeIV) + cIV * exp(-kIV * timeIV);
}
model {
kIV ~ normal(0.4, 1);
cIV ~ lognormal(3,10);
V ~ lognormal(2,10);
CL ~ lognormal(1,10);
concIV ~ normal(predIV, sigma);
}
}
```

The data file below is named data.R and saved in the working folder

```
nIV <- 12
doseIV <- 1000
timeIV <-
c(0.33, 0.5, 0.67, 1.5, 2, 4, 6, 10, 16, 24, 32, 48)
concIV <-
c(14.7, 12.6, 11, 9, 8.2, 7.9, 6.6, 6.2, 4.6, 3.2, 2.3, 1.2)
```

The main R script below

```
library(rstan)
source('data.R');
fit <- stan('stan_model_code.stan', data=c("nIV","doseIV",
"timeIV","concIV"),
chains=4, warmup=5000, iter=14000);
```

The errors I get:

Warning messages:

1: There were 31809 divergent transitions after warmup. See

to find out why this is a problem and how to eliminate them.

2: Examine the pairs() plot to diagnose sampling problems

Any ideas what is wrong?

I adapted the code from:

I eliminated the oral dosing stuff from the example because i don’t need it.

I also ran the full example from the link and got the same errors.

Thank you for any help and also a big thanks to the Stan developers. I was looking for weeks for this kind of software, now I just need to learn to use it