Why is Stan complaining here?

1,930 views
Skip to first unread message

Thao Tran

unread,
Nov 21, 2016, 10:06:59 AM11/21/16
to stan-...@googlegroups.com

Hi Stan users and friendly Stan developers.
Could any one help to give a hint what should I do to improve my model?

I have a model built in Stan where there is a recursive relationship between latent quantities S, I, R.
So:
S[t] = S[t-1] - Inew[t]
      I
[t] = I[t-1] + Inew[t] - Rnew[t]
      R
[t] = R[t-1] + Rnew[t]


and
      Rnew[t] ~ sampling from a binomial distribution with total = I[t-1] and rate = 1-exp(-sigma) where sigma >0 to assume that 0 < rate <1.
      Inew[t]  ~ sampling from a binomial distribution with total = S[t-1] and rate[i] = 1-exp(-lambda[i]) where lambda[i] = p1+p2*age[i].
I can not sample from a binomial distribution in the transformed parameters block so I need to code them like:
 
    Inew[i]   <- rate[i]*S[i-1] ;                                
     
Rnew[i]   <- rateRecover*I[i-1];                            


 Then at each iteration, I calculate: p[i] <- (I[i] + R[i]) / (S[i] + I[i] + R[i]) but I only extract 83 of them based on the index ind <- 365*c(1:83) + 1: prob <- p[ind]. This will be used later in the model block.
Since there is only 83 values of age, but it is more of interest to run the above recursing process longer so I create an index ind with length = 83 but the loop for running the above procedure is much longer.
I have the data on Pos and Tot so in the model block, I have:
model {
 
for (k in 1:n){
   
Pos[k] ~ binomial(Tot[k], prob[k]);
 
}


However, when running the program, I got some errors like:

[505] "Rejecting initial value:"                                                                                                              
[506] "  Log probability evaluates to log(0), i.e. negative infinity."                                                                        
[507] "  Stan can't start sampling from this initial value."                                                                                  
[508] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                        
[509] "validate transformed params: lambda[k0__] is -0.00015359, but must be greater than or equal to 0"                                      
[510] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[511] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                              
[512] "Rejecting initial value:"                                                                                                              
[513] "  Error evaluating the log probability at the initial value."                                                                          
[514] "Rejecting initial value:"                                                                                                              
[515] "  Log probability evaluates to log(0), i.e. negative infinity."                                                                        
[516] "  Stan can't start sampling from this initial value."                                                                                  
[517] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                        
[518] "validate transformed params: lambda[k0__] is -0.614633, but must be greater than or equal to 0"                                        
[519] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[520] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                              
[521] "Rejecting initial value:"                                                                                                              
[522] "  Error evaluating the log probability at the initial value."                                                                          
[523] "Initialization between (-2, 2) failed after 100 attempts. "                                                                            
[524] " Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model."                                 
[525] "Error : "                                                                                                                              
[1] "error occurred during calling the sampler; sampling not done"


In the attachment is my complete program and data.

Merci :D

Bob Carpenter

unread,
Nov 22, 2016, 4:47:52 PM11/22/16
to stan-...@googlegroups.com
What the ereror message is saying is that there's a bug
in your model evaluating the log density. Maybe a variable
isn't being defined or a parameter isn't being declared with
the right constraints.

Also, not clear why you're doing 1 - exp(-sigma) regression
rather than the more standard logistic regression, which
doesn't require that p1 + p2 * age[i] > 0 for it to make sense.
If you really want to keep doing what you're doing, then you need
to constraint p1 and p2 appropriately so that p1 + p2 * age[i] > 0
(e.g., p1 declared with <lower = -fmin(p2 * age)> --- I think that's
right).

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <BP19_Run_Nov16.txt>

Thao Tran

unread,
Nov 23, 2016, 2:54:16 AM11/23/16
to Stan users mailing list
Hi Bob,
Thank you for your reply.
The way I formulate the model like in my model is related to a stochastic SIR model.
Where lambda is the force of infection, then it should be positive.
I will try to put the constraint you propose into the model to see if everything goes on the right track.

Thao.

Thao Tran

unread,
Nov 23, 2016, 3:41:06 AM11/23/16
to Stan users mailing list
Hi Bob,
I tried to implement the constraint but it did not help.
I think there is something going wrong with the model but sill not figured out where.

But thanks for your help.

Thao.

Thao Tran

unread,
Nov 23, 2016, 5:18:33 AM11/23/16
to Stan users mailing list
Oh,
I figured out what is going on.
The model run fine when I specifed a reasonable starting values.

Thank you Bob.

Bob Carpenter

unread,
Nov 23, 2016, 3:59:32 PM11/23/16
to stan-...@googlegroups.com
It's even better to build in the constraint you want.
Then you can do a diffuse initialization and monitor
convergence.

- Bob
Reply all
Reply to author
Forward
0 new messages