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.
[1] "Error : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"
line36 is “vector[N] h1_pred;”