# Initialization between (-2, 2) failed after 100 attempts

Hello, I’m encountering some difficulties in constructing a Bayesian model and I would like to seek your advice.
When I was performing regular Bayesian estimation, my code ran successfully and produced results. However, when I added an additional layer of hyperprior distribution, I encountered an error during model fitting (possibly related to initial values？).
Your insights on this matter would be greatly appreciated, as it is of great importance to me. If possible, could you please provide your suggestions? Thank you very much in advance!

``````data_bys_richards <- list(N = 298, t1 = m1\$t1, t0 = m1\$t0, h0 = m1\$h0, h1 = m1\$h1)

model_code <- "
data {
int<lower=0> N;
vector[N] t0;
vector[N] t1;
vector[N] h0;
vector[N] h1;
}

parameters {
real b;
real c1;
real c2;
}

transformed parameters {
vector[N] h1_pred;

for (i in 1:N) {
h1_pred[i] = exp((log(h0[i]) - c1 * log(1 - exp(-b * t0[i]))) / (1 + c2 * log(1 - exp(-b * t0[i])))) * (1 - exp(-b * t1[i])) ^ (c1 + c2 * ((log(h0[i]) - c1 * log(1 - exp(-b * t0[i]))) / (1 + c2 * log(1 - exp(-b * t0[i])))));
}
}

model {
// Hierarchical prior distribution
real b_mean;
real b_sd;
real c1_mean;
real c1_sd;
real c2_mean;
real c2_sd;

// Hyperprior distribution parameters
b_mean ~ normal(0, 10000);
b_sd ~ exponential(1);
c1_mean ~ normal(0, 10000);
c1_sd ~ exponential(1);
c2_mean ~ normal(0, 10000);
c2_sd ~ exponential(1);

// prior distribution
b ~ normal(b_mean, b_sd);
c1 ~ normal(c1_mean, c1_sd);
c2 ~ normal(c2_mean, c2_sd);

h1 ~ normal(h1_pred, 1);
}
"

stan_model <- stan_model(model_code = model_code)
fit <- sampling(stan_model, data = data_bys_richards, iter = 100, warmup = 10, control = list(max_treedepth = 15),chains=1)
``````

here is error：

``````Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Random variable is nan, but must be not nan!  (in 'modelb3746bf74b5_f9796e2ea206593b426d0116db44035b' at line 36)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Random variable is nan, but must be not nan!  (in 'modelb3746bf74b5_f9796e2ea206593b426d0116db44035b' at line 36)

Chain 1:
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.
 "Error : Initialization failed."
 "error occurred during calling the sampler; sampling not done"
``````

line36 is “vector[N] h1_pred;”

These quantities

are parameters in the model and need to be declared in the parameters block. When declaring local variables in the model block, you need to also populate their values in the model block.

As an aside, as written the hierarchical layer will be hard to estimate and won’t yield any special inference. (You’re asking the model to estimate the standard deviation of a normal distribution based on a single observation from that normal–e.g. estimate `b_sd` where `b` is the only realization from that normal distribution). If you’re building towards something more complex then this could be a useful intermediate step for debugging, but I wouldn’t recommend estimating exactly this model for your final inference.

As a second aside, the implied prior on `b`, etc is massively vague, which is probably ill advised unless it is actually reasonable for these parameters to take on values on the order of 10^5 and -10^5.

1 Like

It works,and what you said is very reasonable.
Thank you sincerely!

You will also want to constrain the standard deviations to be positive. Otherwise, Stan will sample negative values and throw an error when trying to estimate the normally-distributed variables.

``````parameters{
...
real b_mean;
real<lower = 0> b_sd;
real c1_mean;
real<lower = 0> c1_sd;
real c2_mean;
real<lower = 0> c2_sd;
}
``````
1 Like