Initialization failure in rstan

Hi all,

I encountered the following error when trying to fit my model :

Rejecting initial value:
Error evaluating the log probability at the initial value.[RFEPEATED 100 TIMES]

Initialization between (-2, 2) failed after 100 attempts.
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”
Chain 1: Exception: validate transformed params: B[i_0__] is nan, but must be greater than or equal to 0.001 (in ‘model2c544ce95040_ddiff4’ at line 23)


I attached part of the stan code where I believe the problem is

parameters{
  real<lower=50,upper=10000> B0;
  real<lower=0.4,upper=0.8> InitDep;
  real<lower=10^(-3),upper=2> R_sigma;
  real<lower=10^(-3),upper=1> Obs_sigma;
  real<lower=10^(-6), upper=1> q;
  real<lower=10^(-3)> R[TT];
}
transformed parameters{
  real<lower=(10^(-3)> B[TT];
  real<lower=10^(-3)> SSB[TT];
  real<lower=10^(-2)> tmpR[TT];
  real<lower=10^(-2),upper=1> H[TT];
  real<lower=10^(-3),upper=1> s[TT];
  real<lower=10^(-2)> R0;
  real<lower=10^(-1)> qB[TT];
  
  R0=(B0*(1-(1+rho)*exp(-M)+rho*exp(-2*M)))/(w4-rho*w3*exp(-M));
  B[1]=B0*InitDep;
  B[2]=(1+rho)*exp(-M)*(1-H[1])*B[1]-rho*exp(-2*M)*(1-H[1])*B[1]-rho*exp(-M)*(1-H[1])*w3*R[1]+w4*R[1];
  H[1]=Catch[1]/B[1];
  H[2]=Catch[2]/B[2];
  s[1]=exp(-M)*(1-H[1]);
  s[2]=exp(-M)*(1-H[2]);
  tmpR[1]=(4*h*R0*B[1])/((B0*(1-h))+(B[1]*(5*(h-1)));
  tmpR[2]=(4*h*R0*B[2])/((B0*(1-h))+(B[2]*(5*(h-1)));
   SSB[1]= B[1]-Catch[1];
   SSB[2]= B[2]-Catch[2];
  
  for(t in 1:(TT-2)){
    tmpR[t+2]=(4*h*R0*B[t+2])/((B0*(1-h))+(B[t+2]*(5*(h-1)))); 
    B[t+2]=(1+rho)*s[t+1]*B[t+1]-rho*s[t+1]*s[t]*B[t]-rho*s[t+1]*w3*R[t+1]+w4*R[t+2];
    H[t+2]=Catch[t+2]/B[t+2];
    s[t+2]=exp(-M)*(1-H[t+2]);
    SSB[t+2]=B[t+2]-Catch[t+2];
  }
  
  for(t in 1:TT){
    qB[t]=q*(B[t]);
    
  }
}


model{
  for(t in 1:TTCPUE){
    target+=lognormal_lpdf(CPUE[t] | log(qB[t+5]),Obs_sigma);
  }
  
  for(t in 1:TT){
    target+=lognormal_lpdf(R[t] | log(tmpR[t]),R_sigma);
  }
}


For the MCMC sampling:
I specified initial values as shown below …

fit.DDM1<- sampling(Model1, data=stanData,
                  init=function(){
                    list(
                      B0 = runif(1,900,1500),
                      InitDep = runif(1,0.5,0.7),
                      Obs_sigma = runif(1,0.1,0.9),
                      R_sigma=runif(1,0.5,1.5))
                  }
                  ,control = list(adapt_delta = 0.99, max_treedepth = 15),
                  iter=20000, warmup=7000, thin=15, chains=1
)

Any ideas on How I can solve this problem ?

Thanks a lot

Hi,
I would start by solving this issue, which states that at line 23 (I can’t tell which it is exactly), B is NaN, rather than a number above or equal to 10^-3.

You should look at line 23, and figure out why your parameter ends up with a not-a-number value. You can use print(); inside your Stan script to display parameter values in the R console, it should help.

The error message is repeated 100 times because Stan tried initializing 100 times (and failed as many!) That’s just its default behaviour.

Best,
D