Search code examples
rcurve-fittingnls

What causes improper input parameters when using nlsLM from minpack.lm?


I'm trying to fit a nonlinear curve to three data points. Later on, I'll need to integrate this snippet into a larger software that would try fitting the curve to these three points automatically. As it can be seen below, I'm trying to estimate the curve in the form a*x^power1 + b*x^power2. I know that the following function satisfies the condition 0.666*x^(-0.18) - 0.016*x^0.36. However, I am, for some reason, not able at all to reproduce it using nlsLM() from minpack.lm. No matter what combination I try to add at the start parameter, I end up with the same warning message of Warning message: In nls.lm(par = start, fn = FCT, jac = jac, control = control, lower = lower, : lmdif: info = 0. Improper input parameters.

And even though it is "only" a warning message, it seems to entirely mess up my code. Due to the improper input parameters, my variable m which I pass the results to, gets corrupted and nothing works afterwards that include the variable m.

Here is the reproducible example:

library(ggplot2)
library(minpack.lm)

dataset <- read.table(text='
                      x   y
                      1   0.1 1
                      2   30 0.3
                      3  1000 0', header=T)

ds <- data.frame(dataset)
str(ds)
plot(ds, main = "bla")
nlmInitial <- c(a = 0.5, power1 = -0.2, b = -0.02, power2 = 0.3)
m <- nlsLM(y ~ a*I(x^power1) + b*I(x^power2), 
           data = ds, 
           start = nlmInitial, 
           trace = T)
summary(m)$coefficients

Solution

  • You want to estimate to many coefficients with too less observations. You say that 0.666*x^(-0.18) - 0.016*x^0.36 will be a solution. R comes to:

    m <- nlsLM(y ~ 0.666*I(x^power1) + b*I(x^power2), data = ds, trace = T
               , start = c(power1 = -0.2, b = -0.02, power2 = 0.3))
    

    0.666*x^(-0.18053) - 0.01975*x^0.32879. But also

    m <- nlsLM(y ~ 0.7*I(x^power1) + b*I(x^power2), data = ds, trace = T
               , start = c(power1 = -0.2, b = -0.02, power2 = 0.3))
    

    0.7*x^(-0.16599) - 0.04428*x^0.23363 will be a solution.

    So you either have to increase the number of observations or reduce the number of coefficients to estimate.