John C Nash nashjc at uottawa.ca
Thu Jul 12 16:01:33 CEST 2012

```Duncan has given a indication of why nls() has troubles, and you have found a way to work
around the problem partially. However, you may like to try nlmrt (from R-forge project

R-forge.r-project.org/R/?group_id=395

It is intended to be very aggressive in finding a solution, and also to deal with small
residual problems that are really not statistical in nature i.e., nonlinear least squares
but not nonlinear regression. Note that there is a function wrapnls() if you want the
nls() output structure which is very useful for modeling. nlmrt::nlxb is closer to an
optimization method.

I'd be interested to know if you find the solution found by nlmrt is useful in your context.

----------------------------------------------
require(nlmrt)

nlsfit2<- nlxb(data=dd,  y ~  1/2 * ( 1- tanh((x - ttt)/smallc) * exp(-x / tau2) ),
start=list(ttt=0.4, tau2=0.1) ,
trace=TRUE)
----------------------------------------------

Best, JN

On 07/12/2012 06:00 AM, r-help-request at r-project.org wrote:
> From: Jonas Stein <news at jonasstein.de>
> To: <r-help at stat.math.ethz.ch>
> Subject: [R] nls problem: singular gradient
> Message-ID: <e8h0d9-ao4.ln1 at news.jonasstein.de>
> Content-Type: text/plain
>
> 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.
> 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)),  :