rbchan / unmarked

R package for hierarchical models in ecological research
https://rbchan.github.io/unmarked/
36 stars 25 forks source link

Fix tryCatch error handling #59

Closed rbchan closed 4 years ago

rbchan commented 12 years ago

When hessian can't be inverted, it fails instead of returning NA.

kenkellner commented 4 years ago

I have been working on this a little bit. Assuming you still want to change this, is the desired behavior when the Hessian can't be inverted to throw a warning and return a matrix of NAs?

I was thinking of adding a common function that all fitting functions would use for this, for example:

invertHessian <- function(optimOut, nparam, SE){

  blankMat <- matrix(NA, nparam, nparam)
  if(!SE) return(blankMat)

  tryCatch(solve(optimOut$hessian),
    error=function(e){
      warning("Hessian is singular. Try providing starting values or using fewer covariates.", call.=FALSE)
      return(blankMat)
  })
}
rbchan commented 4 years ago

That looks great, thanks.