[R] nls problem: singular gradient

Jonas Stein news at jonasstein.de
Wed Jul 11 20:34:40 CEST 2012


> 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

Hi Duncan,
Thank you for your suggestion. I wrote a function "mytanh" and 
nls terminates a bit later with another error message:

Error in nls(data = dd, y ~ 1/2 * (1 - mytanh((x - ttt)/1e-04) * exp(-x/tau2)),  : 
  number of iterations exceeded maximum of 50

How can i fix that?
Kind regards,
Jonas

============================ R CODE STARTS HERE =======

mytanh <- function(x){
  return(x - x^3/3 + 2*x^5 /15 - 17 * x^7/315)
}

t <- seq(0,1,0.001)
t0 <- 0.5
tau1 <- 0.02

yy <- 1/2 * ( 1- tanh((t - t0)/0.0001) * exp(-t / tau1) ) + rnorm(length(t))*0.001

plot(x=t, y=yy, pch=18)

dd <- data.frame(y=yy, x=t)

nlsfit <- nls(data=dd,  y ~  1/2 * ( 1- mytanh((x - ttt)/0.0001) * exp(-x / tau2) ), start=list(ttt=0.5, tau2=0.02) , trace=TRUE)

============================ R CODE ENDS HERE =======

-- 
Jonas Stein <news at jonasstein.de>



More information about the R-help mailing list