frareb / devRate

R CRAN package devRate
4 stars 1 forks source link

Error with nonlinear models #9

Open mpmontanal opened 1 year ago

mpmontanal commented 1 year ago

Hi, I am trying to adjust my empirical data to the nonlinear models, I have tried to use the initial values for each model of close species and averages as indicated in the package guides. I have also used my data as vectors and as data frames. However, in all cases I have obtained the errors below:

bri1_egg <- devRateModel( eq = briere1_99, temp = temp, devRate = devRate, startValues = list(aa = 1.12, Tmin = 0.021, Tmax = 32.29)) Error in numericDeriv(form[[3L]], names(ind), env, central = nDcentral) : Missing value or an infinity produced when evaluating the model

bri1_egg <- devRateModel( eq = briere1_99, dfData = larvae, startValues = list(aa = 0.85, Tmin = 9, Tmax = 37)) Error in stats::nls(formula = eq[[1]], data = data.frame(rT = devRate, : parameters without starting value in 'data': rT

lactin1_95 <- devRateModel( eq = lactin2_95, temp = temp, devRate = devRate, startValues = list( aa = 0.03, Tmax = 30, deltaT = 5.0, bb = -1.5)) Error in stats::nls(formula = eq[[1]], data = data.frame(rT = devRate, : singular gradient

taylor_egg <- devRateModel(eq = taylor_81, temp = temp, devRate = devRate, startValues = list(Rm = 0.05, Tm = 30, To = 5)) Error in stats::nls(formula = eq[[1]], data = data.frame(rT = devRate, : singular gradient

I would appreciate if you could guide me in possible solutions

frareb commented 1 year ago

Hi and thank you for your interest in devRate.

"Missing value or an infinity produced when evaluating the model" and "singular gradient" are typical problems encountered by the nls() function (Gauss-Newton algorithm) which is used by default by the devRateModel() function.

This problem can (sometime) be circumvented by using another algorithm, such as the Levenberg-Marquardt algorithm (from the minpack.lm package).

In the devRateModel() function, the algo argument can be used to choose the "GN" or "LM" algorithm (make sure to install the minpack.lm package first):

algo: The abbreviated name of the algorithm used for model fitting ( "GN" for Gauss-Newton algorithm, "LM" for Levenberg-Marquardt algorithm; "GN" is the default value).

Example:

bri1_egg <- devRateModel( eq = briere1_99, algo = "LM", temp = temp, devRate = devRate, startValues = list(aa = 1.12, Tmin = 0.021, Tmax = 32.29) )

Let me know if I can help you further.

Best regards.

mpmontanal commented 1 year ago

Thanks for your quick response, I thought I had solved the problem completely with the solution you gave me. However, when I use it with lactin2_95 or davidson_44 with a certain dataset I get:

Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates

I have tried without modifying the algo but it still gives an error.

Do you know what it could be?

frareb commented 1 year ago

Maybe you do not have enough data to fit Lactin2_95 or davidson_44 models ? Send me your dataset if you want, and I'll have a look.