This is probably something really simple but I cannot figure out the solution.

I’m getting the classic error: Initialization between (-2, 2) failed after 100 attempts.

Consider the following model:

```
modelEx = '
data {
int<lower=1> N; // sample size
vector<lower=0,upper=1>[N] phat; //apparent prevalence
}
parameters {
real<lower=0,upper=1> se; // sensitivity
real<lower=0,upper=1> sp; // specificity
}
transformed parameters{
vector<lower=0,upper=1>[N] p;
p = (phat - 1 + sp) / (se + sp - 1);
}
model {
// priors
se ~ beta(9,1);
sp ~ beta(9,1);
}
'
```

```
# toy example
set.seed(20190123)
n = 30
p <- runif(n)
se <- 0.8
sp <- 0.9
phat <- p * se + (1 - p) * (1 - sp)
dat <- list(phat = phat, N = n)
inits <- function(){list(se = 0.70, sp = 0.80)}
output <- stan(model_code = modelEx,
init=inits,
pars = c('se','sp', 'p'),
data = dat,
iter = 3000,
warmup=500,
thin=1,
chains = 1,
verbose = T)
```

I think the problem arises for small values of phat e.g. 0.01 when sp < 1 - phat, which yields a negative value for p (that is a probability).

Any idea how to solve this problem?