# [R] nls convergence trouble

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 4 16:49:44 CEST 2008

```> # 1. Supplying the derivatives results in convergence:

> lgmg <- function(a, b, C0, m, V) {
+ e <- expression((V + b * m * a + C0 * V * b - ((C0 * V * b)^2 + 2 *
+ C0 * b * V^2 - 2 * C0 * V * m * a * b^2 + V^2 + 2 * V * m *
+ a * b + (b * m * a)^2))/(2 * b * m))
+ val <- eval(e)
+ attr(val, "gradient") <- cbind(a = eval(D(e, "a")), b = eval(D(e, "b")))
+ val
+ }
> nls(Qe ~ lgmg(a, b, C0, m, V), bois.DATA, start = c(a = 300, b = 1))
Nonlinear regression model
model:  Qe ~ lgmg(a, b, C0, m, V)
data:  bois.DATA
a         b
337.74912   0.03864
residual sum-of-squares: 15473

Number of iterations to convergence: 9
Achieved convergence tolerance: 3.16e-06

> # 2. As mentioned before squaring both sides results in convergence:
> # even without derivatives
> # though admittedly that is a slightly different objective.
>
> lgm <- function(a, b, C0, m, V) {
+ (V + b * m * a + C0 * V * b - ((C0 * V * b)^2 + 2 *
+ C0 * b * V^2 - 2 * C0 * V * m * a * b^2 + V^2 + 2 * V * m *
+ a * b + (b * m * a)^2))/(2 * b * m)
+ }
> nls(Qe^2 ~ lgm(a, b, C0, m, V)^2, bois.DATA, start = c(a = 300, b = 1))
Nonlinear regression model
model:  Qe^2 ~ lgm(a, b, C0, m, V)^2
data:  bois.DATA
a        b
225.6474   0.3568
residual sum-of-squares: 9.98e+10

Number of iterations to convergence: 16
Achieved convergence tolerance: 6.096e-06
>
> # 3. Also using the reciprocal it converges
> # without derivatives
>
> nls(Qe ~ lgm(a, 1/b, C0, m, V), bois.DATA, start = c(a = 300, b = 1))
Nonlinear regression model
model:  Qe ~ lgm(a, 1/b, C0, m, V)
data:  bois.DATA
a      b
337.75  25.88
residual sum-of-squares: 15473

Number of iterations to convergence: 12
Achieved convergence tolerance: 1.722e-06
> transform(as.list(coef(.Last.value)), b = 1/b)
a          b
1 337.7492 0.03863738
>

On Wed, Sep 3, 2008 at 10:36 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Try squaring both sides of the formula.
>
> On Wed, Sep 3, 2008 at 10:01 AM, Benoit Boulinguiez
> <benoit.boulinguiez at ensc-rennes.fr> wrote:
>> Hi,
>>
>> Parameters assessment in R with nls doesn't work, though it works fine with
>> MS Excel with the internal solver :(
>>
>>
>> I use nls in R to determine two parameters (a,b) from experimental data.
>>
>>        m         V        C0         Ce        Qe
>> 1  0.0911 0.0021740  3987.581   27.11637  94.51206
>> 2  0.0911 0.0021740  3987.581   27.41915  94.50484
>> 3  0.0911 0.0021740  3987.581   27.89362  94.49352
>> 4  0.0906 0.0021740  5981.370   82.98477 189.37739
>> 5  0.0906 0.0021740  5981.370   84.46435 189.34188
>> 6  0.0906 0.0021740  5981.370   85.33213 189.32106
>> 7  0.0911 0.0021740  7975.161  192.54276 233.30310
>> 8  0.0911 0.0021740  7975.161  196.52891 233.20797
>> 9  0.0911 0.0021740  7975.161  203.07467 233.05176
>> 10 0.0906 0.0021872  9968.951  357.49157 328.29824
>> 11 0.0906 0.0021872  9968.951  368.47609 328.03306
>> 12 0.0906 0.0021872  9968.951  379.18904 327.77444
>> 13 0.0904 0.0021740 13956.532 1382.61955 350.33391
>> 14 0.0904 0.0021740 13956.532 1389.64915 350.16485
>> 15 0.0904 0.0021740 13956.532 1411.87726 349.63030
>> 16 0.0902 0.0021740 15950.322 2592.90486 367.38460
>> 17 0.0902 0.0021740 15950.322 2606.34599 367.06064
>> 18 0.0902 0.0021740 15950.322 2639.54301 366.26053
>> 19 0.0906 0.0021872 17835.817 3894.12224 336.57036
>> 20 0.0906 0.0021872 17835.817 3950.35273 335.21289
>> 21 0.0906 0.0021872 17835.817 3972.29367 334.68320
>>
>> the model "LgmAltformula" is
>>
>> Qe ~ (V + b * m * a + C0 * V * b - ((C0 * V * b)^2 + 2 * C0 *
>>    b * V^2 - 2 * C0 * V * m * a * b^2 + V^2 + 2 * V * m * a *
>>    b + (b * m * a)^2)^(1/2))/(2 * b * m)
>>
>> the command in R is
>>
>>
>> nls(formula=LgmAltFormula,data=bois.DATA,start=list(a=300,b=0.01),trace=TRUE
>> ,control=nls.control(minFactor=0.000000009))
>>
>> R has difficulties to converge and stops after the maximum of iterations
>>
>> 64650.47 :  2.945876e+02 3.837609e+08
>> 64650.45 :  2.945876e+02 4.022722e+09
>> 64650.45 :  2.945876e+02 1.695669e+09
>> 64650.45 :  2.945876e+02 5.103971e+08
>> 64650.44 :  2.945876e+02 8.497431e+08
>> 64650.41 :  2.945876e+02 1.515243e+09
>> 64650.36 :  2.945877e+02 5.482744e+09
>> 64650.36 :  2.945877e+02 2.152294e+09
>> 64650.36 :  2.945877e+02 7.953167e+08
>> 64650.35 :  2.945877e+02 7.625555e+07
>> Erreur dans nls(formula = LgmAltFormula, data = bois.DATA, start = list(a =
>> 300,  :
>>  le nombre d'itérations a dépassé le maximum de 50
>>
>>
>> The parameters "a" and "b" are estimated to be 364 and 0.0126 with Excel
>> with the same data set.
>> I tried with the algorithm="port" with under and upper limits. One of the
>> parameter reaches the limit and the regression stops.
>>
>> How can I succeed with R to make this regression?
>>
>>
>> Regards/Cordialement
>>
>> -------------
>> Benoit Boulinguiez
>> Ph.D
>> Ecole de Chimie de Rennes (ENSCR) Bureau 1.20
>> Equipe CIP UMR CNRS 6226 "Sciences Chimiques de Rennes"
>> Campus de Beaulieu, 263 Avenue du Général Leclerc
>> 35700 Rennes, France
>> Tel 33 (0)2 23 23 80 83
>> Fax 33 (0)2 23 23 81 20
>> http://www.ensc-rennes.fr/
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>

```