inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

user specify starting values for non-linear component parameters #44

Closed ASeatonSpatial closed 3 years ago

ASeatonSpatial commented 6 years ago

I'm running into the following error message when trying to fit a distance sampling model:

Error in numericDeriv(expr[[1]], effects, rho = myenv) : Missing value or an infinity produced when evaluating the model

I've traced this error to the nlinla() function and I think it comes from the default starting values for the parameters in the nonlinear part of my predictor.

Here is the non-linear part of my predictor:

hn = function(distance, lsig){ exp(-0.5*(distance/exp(lsig))^2) }

dsamp = function(distance, lsig){ distance*hn(distance, lsig) }

log(dsamp(distance, lsig)) appears in the predictor

It looks like the default starting value for lsig is 0. This is fine when small numbers are used (as in mexdolphin example): dsamp(3, 0) returns a number that I can take a log of.

I can fit a model on simulated data with distances up to around 30. However, my actual data has distances up to 57.

dsamp(57, 0) will return 0, which I think gives the error b/c log(0)

Is there anyway I can specify starting values for the parameters in the non-linear components of the model when I call lgcp()?

ASeatonSpatial commented 6 years ago

I think I have found the way:

starting_values <- data.frame(grf = 0, lsig = 4, Intercept = 0)

fit <- lgcp(components = cmp, data = obs, samplers = point_transects, formula = fml, options = list(result = starting_values))

I'm not sure if grf = 0 or Intercept = 0 will ever be likely to cause an issue.

finnlindgren commented 6 years ago

If the cause is numerical under/overflow, define the function to return the log directly, I.e. ‘log(distance) - 0.5*(...)^2’

This is similar to the issue where one should not use ‘log(dnorm(...))’ or similar for log-densities, but rather ‘dnorm(..., log=TRUE)’.

This isn’t something that can be automated, since once the ‘exp’ has been taken it’s too late to recover.

Have you traced the code to see what the result setting changes in what is sent to ‘inla’?

ASeatonSpatial commented 6 years ago

No I just traced what it did with result up to the nlinla() call but not after that. results are used to create the 'epunkt' argument sent to nlinla(). I'm not sure where else results is used.

I have defined the function to return the log directly, that seems to do better. The distance sampling example in the workshops should be updated because it also does log(half-normal) in the formula.

finnlindgren commented 6 years ago

Somewhere, I defined detection model functions with a “log” argument just as dnorm etc, but that seems to never have made it into tutorials.

Finn

On 15 Aug 2018, at 15:46, A Seaton notifications@github.com wrote:

No I just traced what it did with result up to the nlinla() call but not after that. results are used to create the 'epunkt' argument sent to nlinla(). I'm not sure where else results is used.

I have defined the function to return the log directly, that seems to do better. The distance sampling example in the workshops should be updated because it also does log(half-normal) in the formula.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

finnlindgren commented 3 years ago

Starting values can now be supplied with option bru_initial.