# [R] suggestions for nls error: false convergence

Gabor Grothendieck ggrothendieck at gmail.com
Mon Dec 19 17:12:27 CET 2005

```Sometimes its just one parameter that's the culprit so just use
a grid to get the starting value.  Since its known
that in logistic growth the saturation level is a problem we conjecture
that in this one m is the culprit and grid over it.  Note that with this
approach we did not need to extend the allowable iterations at all
so the control arg has been removed simplifying the call:

> for(m in seq(50, 150, 25)) {
+   model <- try(nls(y ~ a * (1+m*exp(-x/tau)) / (1+n*exp(-x/tau)),
+                  data=d, start = list(a = 277, m = m, n = 101, tau = 10),
+                  algorithm='port'))
+    if (!inherits(model, "try-error")) print(model)
+    }
Nonlinear regression model
model:  y ~ a * (1 + m * exp(-x/tau))/(1 + n * exp(-x/tau))
data:  d
a            m            n          tau
2.757817e+02 1.594652e+03 2.312661e+05 5.617307e+00
residual sum-of-squares:  808.7282
Nonlinear regression model
model:  y ~ a * (1 + m * exp(-x/tau))/(1 + n * exp(-x/tau))
data:  d
a            m            n          tau
2.757817e+02 1.594652e+03 2.312661e+05 5.617307e+00
residual sum-of-squares:  808.7282
Error in nls(y ~ a * (1 + m * exp(-x/tau))/(1 + n * exp(-x/tau)), data = d,  :
Convergence failure: iteration limit reached without convergence (9)
Error in nls(y ~ a * (1 + m * exp(-x/tau))/(1 + n * exp(-x/tau)), data = d,  :
Convergence failure: singular convergence (7)
Nonlinear regression model
model:  y ~ a * (1 + m * exp(-x/tau))/(1 + n * exp(-x/tau))
data:  d
a            m            n          tau
2.757817e+02 1.594652e+03 2.312661e+05 5.617307e+00
residual sum-of-squares:  808.7282

On 12/19/05, Christian Ritz <ritz at bioassay.dk> wrote:
> Hi Spencer.
>
> When using 'optim' and the first try fails you could:
>
>
> 1) try some other methods: Nelder-Mead, BFGS, ...
>
> 2) increase the maximum number of iterations (argument maxit in the control list)
>
> 3) specify the argument parscale in the control list, in order to have all parameters of same magnitude during
> optimisation (this is useful if the parameters are suspected to be of different magnitudes).
>
>
> Using the default method (Nelder-Mead) with maxit=1000 results in convergence, and essentially the same estimates are
> obtained if you use the method BFGS and set maxit=1000 and parscale=c(277, 100, 101, 10) (the initial starting values):
>
>
> x <- 1:100
>
> y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> 0,0,0,0,0,1,1,1,2,2,2,2,2,3,4,4,4,5,
> 5,5,5,6,6,6,6,6,8,8,9,9,10,13,14,16,19,21,
> 24,28,33,40,42,44,50,54,69,70,93,96,110,127,127,141,157,169,
> 178,187,206,216,227,236,238,244,246,250,255,255,257,260,261,262,266,268,
> 268,270,272,272,272,273,275,275,275,276)
>
> func2 <- function( par,y, x, rescale ) {
> par <- rescale*par
> a = par
> m = par
> n = par
> tau = par
> y. <- a * (1+m*exp(-x/tau)) / (1+n*exp(-x/tau))
> sum((y-y.)2)
> }
>
> est.no2 <- optim(c(277, 100, 101, 10), func2,  hessian=TRUE, y=y, x=x, rescale=1, control=list(maxit=1000))
>
> est.no3 <- optim(c(277, 100, 101, 10), func2,  hessian=TRUE, method="BFGS", y=y, x=x, rescale=1,
> control=list(maxit=1000, parscale=c(277, 100, 101, 10)))
>
>
> The optimisation in the package 'drc' uses BFGS with the maxit and parscale arguments specified.
>
> Best wishes
>
> Christian
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help