# Error using nls function

4 messages
Open this post in threaded view
|

## Error using nls function

 I'm trying to fit a harmonic equation to my data, but when I'm applying the nls function, R gives me the following error: Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates. All posts I've seen, related to this error, are of exponential functions, where a linearization is used to fix this error, but in this case, I'm not able to solve it in this way. I tried to use other starting points but it still not working. y <- c(20.91676, 20.65219, 20.39272, 20.58692, 21.64712, 23.30965, 23.35657, 24.22724, 24.83439, 24.34865, 23.13173, 21.96117) t <- c(1, 2, 3, 4 , 5 , 6, 7, 8, 9, 10, 11, 12) # Fitting function fit <- function(x, a, b, c) {a+b*sin(2*pi*x)+c*cos(2*pi*x)} res <- nls(y ~ fit(t, a, b, c), data=data.frame(t,y), start = list(a=1,b=0, c=1)) Can you help me? Thanks! David -- Sent from: https://r.789695.n4.nabble.com/R-help-f789696.html______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Error using nls function

 Use nlsr::nlxb() to get analytic derivatives. Though your problem is pretty rubbishy -- look at the singular values. (You'll need to learn some details of nlxb() results to interpret.) Note to change the x to t in the formula. JN > f1 <- y ~  a+b*sin(2*pi*t)+c*cos(2*pi*t) > res1 <- nls(f1, data=data.frame(t,y), start=list(a=1,b=0, c=1)) Error in nlsModel(formula, mf, start, wts) :   singular gradient matrix at initial parameter estimates > library(nlsr) > res1n <- nlxb(f1, data=data.frame(t,y), start=list(a=1,b=0, c=1)) > res1n nlsr object: x residual sumsquares =  28.644  on  12 observations     after  3    Jacobian and  4 function evaluations   name            coeff          SE       tstat      pval      gradient    JSingval a                11.2235            NA         NA         NA  -2.473e-12       4.899 b           -1.55541e-09            NA         NA         NA   1.595e-14   8.399e-15 c                11.2235            NA         NA         NA  -2.473e-12   5.053e-16 > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.