Search code examples
rstanrstan

Overcome the error: Initialization failed in rstan::sampling()


rstan::sampling() fails with the following error.

[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
[1] "error occurred during calling the sampler; sampling not done"
Stan model 'foo' does not contain samples.
Stan model 'foo' does not contain samples.
Stan model 'foo' does not contain samples.
Stan model 'foo' does not contain samples.

where, foo means a name of a stan file foo.stan.


Solution

  • Reproducible example that is inconsistent and thus impossible to initialize:

    model <- 
      stan_model(model_code = "parameters { real<lower = 0> y; }
                               transformed parameters { real<upper = -1> z = y; }")
    
    fit <- sampling(model)
    

    This produces the following output in RStan 2.18.1:

    > fit <- sampling(model)
    
    SAMPLING FOR MODEL '64719d6dccb64c32b0d897ef1f340d74' NOW (CHAIN 1).
    Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
    [1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
    error occurred during calling the sampler; sampling not done
    

    Here's the beginning of the resulting fit structure:

    > str(fit)
    Formal class 'stanfit' [package "rstan"] with 10 slots
      ..@ model_name: chr "64719d6dccb64c32b0d897ef1f340d74"
      ..@ model_pars: chr [1:3] "y" "z" "lp__"
      ..@ par_dims  :List of 3
      .. ..$ y   : num(0) 
      .. ..$ z   : num(0) 
      .. ..$ lp__: num(0) 
      ..@ mode      : int 2
      ..@ sim       : list()
    

    So you can use the test

    length(fit@sim) == 0
    

    If there are samples, they'll show up as a list in variable sim.

    WARNING: This problem often arises because constraints are not well formulated on parameters. Stan assumes every value of the parameters that satisfies the constraints has support in the model. If it doesn't, the model will typically fail to initialize. The best solution is to fix the constraints on the parameters and the scales of the parameters so that the model can be initialized randomly. If the constraints are well formulated, numerical issues can also cause problems. Scaling parameters to unit scale can help. Also, big interval uniform priors can be a problem because initialization will be around the halfway point. If the problem is well scaled and constraints are well formed, then you can reduce the interval for initialization, which can help. Otherwise, you really have to supply your own sensible inits for things to be robust. Retrying if 100 tries already failed is not usually going to work.