Search code examples
rnls

Error in nlsModel: singular gradient matrix at initial parameter estimates


I encountered this nls singular matrix problems in some real data test, also tried nlsLM, but I always get the same error. Some existing solutions in the stackoverflow says the initial parameters are not ideal enough. Then I created a test dataset with noise added. Then I entered the exact parameters for start, but still got the same error. Can some one take a look, what's the problem with this?

library(minpack.lm)
f <- function(x,a,b,m,n) {
  m + n* b/(a^b) * (x^(b-1))
}
# test dataset
x = seq(1,100)
y= f(x,a = 1,b = 2.5,m = 0.5, n= 50)
noise = runif(100,-1000,1000) 
y = y+ noise # add noise
plot(x, y, type="l")
data = as.data.frame(cbind(x,y))
mod <- nlsLM(y ~ f(x,a,b,m,n), data = data, start=list(a = 1,b = 2.5,m = 0.5, n= 50), control = list(maxiter = 500))

Thanks in advance!


Solution

  • The main problem is the model specification. For fixed b any combination of a and n for which n* b/(a^b) is the same yield the same model giving rise to the singularity. Fix either a or n. In the following we fix a to be 1.

    The other problem with the question is that the example is not reproducible because the random seed was not set.

    Using f from the question:

    set.seed(123)
    x <- 1:100
    y <- f(x, a = 1, b = 2.5, m = 0.5, n = 50) + runif(100, -1000, 1000) 
    
    a <- 1
    mod <- nlsLM(y ~ f(x, a, b, m, n), start = list(b = 2.5, m = 0.5, n= 50))
    

    giving:

    > mod
    Nonlinear regression model
      model: y ~ f(x, a, b, m, n)
       data: parent.frame()
          b       m       n 
      2.507 240.352  48.122 
     residual sum-of-squares: 31264921
    
    Number of iterations to convergence: 3 
    Achieved convergence tolerance: 1.49e-08