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

I’m trying to run a simple hierarchical logistic model (, which is presented as an example on p.139 in “Stan Modeling Language-User’s Guide and Reference Manual”) using rstan. I’m getting an error message as below.

Rejecting initial value:
  Log probability evaluates to log(0), i.e. negative infinity.
  Stan can't start sampling from this initial value.

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"

Do you have any idea about what’s wrong with the code or the data? My R codes are as below.



eatiming <- read.sas7bdat('C:/Users/Silver Chung/Dropbox/TempData/eatiming.sas7bdat')

eatiming_code <- '
int<lower=1> D;
int<lower=0> N;
int<lower=1> L;
int<lower=0,upper=1> y[N];
int<lower=1,upper=L> ll[N];
row_vector[D] x[N];
parameters {
real mu[D];
real<lower=0> sigma[D];
vector[D] beta[L];
model {
for (d in 1:D) {
mu[d] ~ normal(0, 100);
for (l in 1:L)
beta[l,d] ~ normal(mu[d], sigma[d]);
for (n in 1:N)
y[n] ~ bernoulli(inv_logit(x[n] * beta[ll[n]]));

x_data <-matrix(c(eatiming$io_perc, eatiming$sue,eatiming$negative_sue,eatiming$friday,
                nrow=nrow(eatiming), byrow=FALSE)
y_data <- c(eatiming$AMC)
id_data<- c(eatiming$id)

eatiming_data <- list(D=10, N=dim(eatiming)[1], L=length(unique(eatiming$id)), y=y_data, ll=id_data, x=x_data)

eatiming_fit <- stan(model_code=eatiming_code, 
                     data=eatiming_data, iter=2000, chains=1)

I’ve also attached the data to this post.
Any comments and feedback would help. Thank you so much!!!

rcode_041918.R (1.2 KB)

eatiming.csv (1.1 MB)

First try reducing the interval for the initial values by specifying init_r to some number less than 2.

1 Like

Changing bernoulli(inv_logit(...)) to bernoulli_logit(...) made it sample for me (they’re equivalent). Some of the outputs of inv_logit were 1.0, and I think that was giving the log1m functions inside bernoulli_lpmf a hard time.

1 Like

Thank you so much for such a prompt response! Specifying init_r worked! I also changed bernoulli(inv_logit(...)) to bernoulli_logit() I’m not really sure what "log1m functions inside bernoulli_lpmf though. Is it bad that some outputs of inv_logit were 1.0?

Thank you for your help again!

Internally bernoulli(theta) tries to evaluate log1m(theta). log1m(theta) = log(1 - theta). If theta == 1.0, then that means trying to evaluate log(0.0), which blows up.

Glad to hear things are working now!