Error in rejecting initial values in Rstan

@bbbales2 @jonah @Bob_Carpenter @mike-lawrence

Hi all, Thank you very much helped me on my previous errors. But, as I am not much familiar with stan, I often get compilation errors on my code. Below mention how I coded priors and initial values for my model. But it makes me nervous with these errors:

SAMPLING FOR MODEL ‘2078d01a6497d314c182be58730f1fc2’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: integrate: error estimate of integral 0.00441318 exceeds the given relative tolerance times norm of integral (in ‘model237c36431647_2078d01a6497d314c182be58730f1fc2’ at line 127)

Chain 1:
Chain 1: Initialization between (-2, 2) failed after 1 attempts.
Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”


parameters{
real<lower=0.01> b0;
real<lower=1> mu;
real<lower=0.01> sigma2;
real<lower=0.01> lambda;
real<lower=0.01> alpha; 
// 
}

// prior distributions 
b0 ~ normal(1.97,1);
mu ~ normal(4.19,1);
sigma2 ~ normal(0.04,1);
lambda ~ normal(0.28,1);
alpha ~ normal(1.55,1);

init_func <- function(){
list(b0=runif(1,0,4),
mu=runif(1,4,4.5),
sigma2=runif(1,0.01,0.05),
lambda=runif(1,0.01,0.5),
alpha=runif(1,1.5,4))}

fit <- stan(model_code = sample_code,
data = sample_data,
iter = 1000,
warmup = 500,
init = init_func,
chains = 2,
control = list(max_treedepth=10 , adapt_delta = 0.95)
)


  1. Is it possible to define custom iniitial values rather than (-2,2) range? Because, in my case the range for parameters b_{0} and \mu varies beyond 2 (Please see my random uniform parameter values)

  2. Is it possible to define argument “init_r” with my initial value function (init_func) in parallel ? If so what lower and upper bounds work for generating initial values ? Please kindly explain what is going?

Could you please help me here? It will be a big step for me.

Thanks in advance.

Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: integrate: error estimate of integral 0.00441318 exceeds the given relative tolerance times norm of integral (in ‘model237c36431647_2078d01a6497d314c182be58730f1fc2’ at line 127)

This error means that you have an integrate_1d call in your code that is failing to hit the tolerance requirements.

There’s probably either a bug in the integral or a numeric problem that is making it not work correctly.

You’ll need to debug this. Can you add print statements before your integrate_1d call and try to print out the parameters right before the call happens? If print doesn’t work don’t spend too much time on it – there’s some weird condition where print doesn’t work before initialization and I forget what it is.

Anyway if it does work that’s your ticket. Take those parameter values and try to figure out if the integral exists, etc.

The other thing you can do is put your integral in a function so you can do expose_stan_functions in R and try to figure out if it’s doing the right thing (again, compare against a known solution).

Hi Ben @bbbales2,
Thanks for understanding the mess in my code. This is the function block i have used there.

functions{
real Q_integral(real x,
real xc,
real[] theta,
real[] x_r,
int[] x_i){

real mu = theta[1];
real sigma2 = theta[2];
real lambda = theta[3];
real alpha = theta[4];
real tn = theta[5];
real Q;

Q= (1/x)*(exp(((-(log(x)-log(tn))^2)/(2*sigma2))+((mu-log(tn))*log(x)/sigma2)-(lambda*(tn-x)^alpha)));
return(Q);
}

real W_integral(real x,
real xc,
real[] theta,
real[] x_r,
int[] x_i){

real mu = theta[1];
real sigma2 = theta[2];
real W;
W = ((0.3 * exp(-square(log(x) - mu) / (2 * sigma2)))/(sqrt(2*pi()*sigma2)*x));
return(W);
}
}

Then, I want to do several integrals through out the my code as follows:
V[1] = beta_t[2]((0.3/sqrt(2pi()sigma2))(exp(((-(mu-log(t[2]))^2)/(2*sigma2))-(((mu-log(t[2]))log(t[2]))/(sigma2)))))(integrate_1d(Q_integral,t[1],t[2],{mu, sigma2,lambda, alpha, t[2]}, {0.0}, x_i, 1e-8));

I[1] = (1-beta_t[2])(((0.3/sqrt(2pi()sigma2))(exp(((-(mu-log(t[2]))^2)/(2*sigma2))-(((mu-log(t[2]))log(t[2]))/(sigma2)))))integrate_1d(Q_integral,t[1], t[2],{ mu, sigma2, alpha, lambda, t[2]}, {0.0}, {0}, 1e-8)-
((0.3/sqrt(2
pi()sigma2))(exp(((-(mu-log(t[3]))^2)/(2
sigma2))-(((mu-log(t[3]))log(t[3]))/(sigma2)))))integrate_1d(Q_integral,t[1], t[2],{ mu, sigma2, alpha, lambda, t[3]}, {0.0}, {0}, 1e-8))+
integrate_1d(W_integral,t[2], t[3],{ mu, sigma2}, {0.0}, {0}, 1e-8)-
((0.3/sqrt(2
pi()sigma2))(exp(((-(mu-log(t[3]))^2)/(2
sigma2))-(((mu-log(t[3]))*log(t[3]))/(sigma2)))))*integrate_1d(Q_integral,t[2], t[3],{ mu, sigma2, alpha, lambda, t[3]}, {0.0}, {0}, 1e-8);
.
.
.
.
init_func <- function(){list(b0=runif(1,0,4),mu=runif(1,4,4.5),sigma2=runif(1,0.01,0.05),lambda=runif(1,0.01,0.5),alpha=runif(1,1.5,4))}

fit<-stan(model_code = cancer_code,
data = cancer_data,
iter = 20,
warmup = 10,
init = init_func,
init_r = 0.5,
chains = 2,
control = list(max_treedepth=10 , adapt_delta = 0.95)
)

I think I could understand what you suggested me to do. So, I let the code to print parameter values and limits of integration in following order

print(lower_limit, “,” , upper_limit , “,” , mu , “,” , sigma2 , “,” , alpha , “,” , lambda);

Then stan gives me:

SAMPLING FOR MODEL ‘d5fc77a7c85e8fcd0e4880ff25844065’ NOW (CHAIN 1).
Chain 1:
15,50,4.21296,0.0231476,1.67584,0.309843
15,51,4.21296,0.0231476,1.67584,0.309843
15,52,4.21296,0.0231476,1.67584,0.309843
15,53,4.21296,0.0231476,1.67584,0.309843
15,54,4.21296,0.0231476,1.67584,0.309843
15,55,4.21296,0.0231476,1.67584,0.309843
15,56,4.21296,0.0231476,1.67584,0.309843
15,57,4.21296,0.0231476,1.67584,0.309843
15,58,4.21296,0.0231476,1.67584,0.309843
15,59,4.21296,0.0231476,1.67584,0.309843
15,60,4.21296,0.0231476,1.67584,0.309843
15,61,4.21296,0.0231476,1.67584,0.309843
15,62,4.21296,0.0231476,1.67584,0.309843
15,63,4.21296,0.0231476,1.67584,0.309843
15,64,4.21296,0.0231476,1.67584,0.309843
15,65,4.21296,0.0231476,1.67584,0.309843
15,66,4.21296,0.0231476,1.67584,0.309843
15,67,4.21296,0.0231476,1.67584,0.309843
15,68,4.21296,0.0231476,1.67584,0.309843
15,69,4.21296,0.0231476,1.67584,0.309843
15,70,4.21296,0.0231476,1.67584,0.309843
15,71,4.21296,0.0231476,1.67584,0.309843
15,72,4.21296,0.0231476,1.67584,0.309843
15,73,4.21296,0.0231476,1.67584,0.309843
15,74,4.21296,0.0231476,1.67584,0.309843

Chain 1: 15,50,4.21296,0.0231476,1.67584,0.309843

Chain 1: Exception: integrate: error estimate of integral 2.84703e+13 exceeds the given relative tolerance times norm of integral (in ‘modela682a643eba_d5fc77a7c85e8fcd0e4880ff25844065’ at line 90)

[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: integrate: error estimate of integral 2.84703e+13 exceeds the given relative tolerance times norm of integral (in ‘modela682a643eba_d5fc77a7c85e8fcd0e4880ff25844065’ at line 90)”
[1] “error occurred during calling the sampler; sampling not done”

SAMPLING FOR MODEL ‘d5fc77a7c85e8fcd0e4880ff25844065’ NOW (CHAIN 2).
Chain 2:
15,50,4.47482,0.0305653,1.52133,0.321217
15,51,4.47482,0.0305653,1.52133,0.321217
15,52,4.47482,0.0305653,1.52133,0.321217
15,53,4.47482,0.0305653,1.52133,0.321217
15,54,4.47482,0.0305653,1.52133,0.321217
15,55,4.47482,0.0305653,1.52133,0.321217
15,56,4.47482,0.0305653,1.52133,0.321217
15,57,4.47482,0.0305653,1.52133,0.321217
15,58,4.47482,0.0305653,1.52133,0.321217
15,59,4.47482,0.0305653,1.52133,0.321217
15,60,4.47482,0.0305653,1.52133,0.321217
15,61,4.47482,0.0305653,1.52133,0.321217
15,62,4.47482,0.0305653,1.52133,0.321217
15,63,4.47482,0.0305653,1.52133,0.321217
15,64,4.47482,0.0305653,1.52133,0.321217
15,65,4.47482,0.0305653,1.52133,0.321217
15,66,4.47482,0.0305653,1.52133,0.321217
15,67,4.47482,0.0305653,1.52133,0.321217
15,68,4.47482,0.0305653,1.52133,0.321217
15,69,4.47482,0.0305653,1.52133,0.321217
15,70,4.47482,0.0305653,1.52133,0.321217
15,71,4.47482,0.0305653,1.52133,0.321217
15,72,4.47482,0.0305653,1.52133,0.321217
15,73,4.47482,0.0305653,1.52133,0.321217
15,74,4.47482,0.0305653,1.52133,0.321217

Chain 2: 15,50,4.47482,0.0305653,1.52133,0.321217

Chain 2: Exception: integrate: error estimate of integral 7.21561e+22 exceeds the given relative tolerance times norm of integral (in ‘modela682a643eba_d5fc77a7c85e8fcd0e4880ff25844065’ at line 90)

[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: integrate: error estimate of integral 7.21561e+22 exceeds the given relative tolerance times norm of integral (in ‘modela682a643eba_d5fc77a7c85e8fcd0e4880ff25844065’ at line 90)”
[1] “error occurred during calling the sampler; sampling not done”

I have noticed that parameter values didn’t change over the data table which contains 25 rows with different ages.
I would be grateful if anyone can comments here.

Thank you all