Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in nls singular gradient matrix at initial parameter estimates

I'm trying to fit a rectangular hyperbola using the nls in R.

curve.nlslrc = nls(photolrc ~ (1/(2*theta))*(AQY*PARlrc+Am-sqrt((AQY*PARlrc+Am)^2-4*AQY*theta*Am*PARlrc))-Rd, start=list(Am=(max(photolrc)-min(photolrc)),AQY=0.05,Rd=-min(photolrc),theta=1))

And a wild message appears:

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

Any ideas on how to fix this?

Data:

PARlrc  photolrc
    50     -0.04
   100  1.130000
   150  0.580000
   200  0.850000
   250  1.370000
   300  1.370000
   350  1.230000
   400  2.040000
   450  1.670000
   500  1.790000
   550  1.820000
   600  1.768494
   650  2.083641
   700  1.998950
   750  2.399018
   800  2.289517
   850  2.223104
   900  2.329006
   950  2.700987
  1000  2.694792
  1050  2.684530
  1100  2.594925
  1150  2.662429
  1200  2.590890
  1250  3.043056
  1300  3.795076
  1350  4.003595
  1400  4.401325
  1450  4.786757
  1500  4.338971
  1550  4.701821
  1600  4.431703
  1650  4.392877
  1700  4.642945
  1750  4.429018
  1800  3.638166
  1850  2.879107
like image 267
skcela Avatar asked Nov 19 '25 00:11

skcela


1 Answers

Try nlsLM:

library(minpack.lm)

curve.nlslrc = with(DF, 
  nlsLM(photolrc ~ 
          (1/(2*theta))*(AQY*PARlrc+Am-sqrt((AQY*PARlrc+Am)^2-4*AQY*theta*Am*PARlrc))-Rd, 
     start = list(Am=(max(photolrc)-min(photolrc)), AQY=0.05,  Rd=-min(photolrc), theta=1))
)

giving:

> curve.nlslrc
Nonlinear regression model
  model: photolrc ~ (1/(2 * theta)) * (AQY * PARlrc + Am - sqrt((AQY *     PARlrc + Am)^2 - 4 * AQY * theta * Am * PARlrc)) - Rd
   data: parent.frame()
       Am       AQY        Rd     theta 
 3.957527  0.002529 -0.340865  1.000022 
 residual sum-of-squares: 6.94

Number of iterations to convergence: 35 
Achieved convergence tolerance: 1.49e-08

(continued after chart)

screenshot

Note 1: Note that an even simpler model with fewer parameters (3 vs. 4) has a lower residual sum of squares (6.7 vs. 6.9):

fm.lm <- lm(photolrc ~ PARlrc, DF)
fm2 <- nls(photolrc ~ pmin(a, b * PARlrc + c), DF,
  start = list(a = mean(DF$photolrc), b = coef(fm.lm)[2], c = 0))

giuving:

> fm2
Nonlinear regression model
  model: photolrc ~ pmin(a, b * PARlrc + c)
   data: DF
       a        b        c 
4.159377 0.002434 0.420329 
 residual sum-of-squares: 6.739

Number of iterations to convergence: 5 
Achieved convergence tolerance: 9.197e-09

Note 2: This was used as DF:

Lines <- "PARlrc photolrc
50 -0.04
100 1.130000
150 0.580000
200 0.850000
250 1.370000
300 1.370000
350 1.230000
400 2.040000
450 1.670000
500 1.790000
550 1.820000
600 1.768494
650 2.083641
700 1.998950
750 2.399018
800 2.289517
850 2.223104
900 2.329006
950 2.700987
1000 2.694792
1050 2.684530
1100 2.594925
1150 2.662429
1200 2.590890
1250 3.043056
1300 3.795076
1350 4.003595
1400 4.401325
1450 4.786757
1500 4.338971
1550 4.701821
1600 4.431703
1650 4.392877
1700 4.642945
1750 4.429018
1800 3.638166
1850 2.879107"
DF <- read.table(text = Lines, header = TRUE)
like image 122
G. Grothendieck Avatar answered Nov 21 '25 13:11

G. Grothendieck