[R] nls problem: singular gradient
Duncan Murdoch
murdoch.duncan at gmail.com
Wed Jul 11 17:56:25 CEST 2012
On 11/07/2012 11:04 AM, Jonas Stein wrote:
> Why fails nls with "singular gradient" here?
> I post a minimal example on the bottom and would be very
> happy if someone could help me.
Take a look at the predicted values at your starting fit: there's a
discontinuity at 0.4, which sure makes it look as though overflow is
occurring. I'd recommend expanding tanh() in terms of exponentials and
rewrite the prediction in a way that won't overflow.
Duncan Murdoch
> Kind regards,
>
> ###########
>
> # define some constants
> smallc <- 0.0001
> t <- seq(0,1,0.001)
> t0 <- 0.5
> tau1 <- 0.02
>
> # generate yy(t)
>
> yy <- 1/2 * ( 1- tanh((t - t0)/smallc) * exp(-t / tau1) ) + rnorm(length(t))*0.01
>
> # show the curve
>
> plot(x=t, y=yy, pch=18)
>
> # prepare data
>
> dd <- data.frame(y=yy, x=t)
>
> nlsfit <- nls(data=dd, y ~ 1/2 * ( 1- tanh((x - ttt)/smallc) * exp(-x / tau2) ), start=list(ttt=0.4, tau2=0.1) , trace=TRUE)
>
> # get error:
> # Error in nls(data = dd, y ~ 1/2 * (1 - tanh((x - ttt)/smallc) * exp(-x/tau2)), :
> # singular gradient
>
More information about the R-help
mailing list