Tomas
Tomas

Reputation: 59545

Can I tell JAGS to re-start automatically after failure with initial values?

My model failed with the following error:

Compiling rjags model...
Error: The following error occured when compiling and adapting the model using rjags:
 Error in rjags::jags.model(model, data = dataenv, inits = inits, n.chains = length(runjags.object$end.state),  :
  Error in node Y[34,10]
Observed node inconsistent with unobserved parents at initialization.
Try setting appropriate initial values.

I have done some diagnosis and found that there was a problem with initial values in chain 3. However, this can happen from time to time. Is there any way to tell run.jags or JAGS itself to re-try and re-run the model in such cases? For example, to tell him to make another N attempts to initialize the model properly. That would be very logical thing to do instead of just failing. Or do I have to do it manually with some tryCatch thing?

P.S.: note that I am currently using run.jags to run JAGS from R.

Upvotes: 0

Views: 754

Answers (2)

Martyn Plummer
Martyn Plummer

Reputation: 341

Usually when this error occurs it is an indication of a serious underlying problem. I don't think a strategy of "try again" is useful in general (and especially because default initial values are deterministic).

The default initial values generated by JAGS are given by a "typical" value from the prior distribution (e.g. mean, median, or mode). If it turns out that this is inconsistent with the data then there are typically two possible causes:

  1. A posteriori constraints that need to be taken into account, such as when modelling censored survival data with the dinterval distribution
  2. Prior-data conflict, e.g. the prior mean is so far away from the value supported by the data that it has zero likelihood.

These problems remain the same when you are supplying your own initial values.

If you think you can generate good initial values most of the time, with occasional failures, then it might be worth repeated attempts inside a call to try() but I think this is an unusual case.

Upvotes: 1

Matt Denwood
Matt Denwood

Reputation: 2583

There is no facility for that provided within runjags, but it would be fairly simple to write yourself like so:

success <- FALSE
while(!success){
    s <- try(results <- run.jags(...))
    success <- class(s)!='try-error'
}
results

[Note that if this model NEVER works, the loop will never stop!]

A better idea might be to specify an initial values function/list that provides initial values that are guaranteed to work (if possible).

In runjags version 2, it will be possible to recover successful simulations when some simulations have crashed, so if you ran (say) 5 chains in parallel then if 1 or 2 crashed you would still have 3 or 4. That should be released within the next couple of weeks, and contains a large number of other improvements.

Upvotes: 2

Related Questions