[R] error using nls with logistic derivative
John C Nash
nashjc at uottawa.ca
Tue Apr 17 16:07:13 CEST 2012
Peter Dalgaard has already given some indications. However, nls() is pretty fragile as a
solver in my experience. I'm in the process of putting some more robust (in the computing
and not statistical sense) solvers in the nlmrt package on the R-forge project at
https://r-forge.r-project.org/R/?group_id=395
See output below. Best, JN
> source("ardila.R", echo=T)
> rm(list=ls())
> require(nlmrt)
> logis<- expression(a/(1+exp((b-x)/c)))
> D(logis, "x")
a * (exp((b - x)/c) * (1/c))/(1 + exp((b - x)/c))^2
> myY<-c(5.5199668, 1.5234525, 3.3557000, 6.7211704, 7.4237955, 1.9703127, 4.3939336,
+ -1.4380091, 3.2650180, 3.5760906, 0.2947972, 1.0569417)
> myX<-c(1, 0, 0, 4, 3, 5, 12, 10, 12, 100, 100, 100)
> mydata<-data.frame(X=myX, Y=myY)
> ratelogis <- try(nls(Y ~ a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2,
+ start=list(a = 21.16322, b = 8.83669, c = 2.957765),trace=TRUE, data=myda ....
[TRUNCATED]
151.098 : 21.163220 8.836690 2.957765
127.1149 : 103.49326 11.43274 20.29663
111.344 : 833.02390 -45.86244 140.32986
111.3375 : 978.97105 -76.20547 159.90818
111.3374 : 1097.1336 -101.6763 174.2032
111.3227 : 1179.8406 -119.7406 183.3788
Error in nls(Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2, :
step factor 0.000488281 reduced below 'minFactor' of 0.000976562
> print(ratelogis)
[1] "Error in nls(Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2, : \n step
factor 0.000488281 reduced below 'minFactor' of 0.000976562\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in nls(Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2, start =
list(a = 21.16322, b = 8.83669, c = 2.957765), trace = TRUE, data = mydata): step
factor 0.000488281 reduced below 'minFactor' of 0.000976562>
> ratelogisn <- nlxb(Y ~ a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2,
+ start=list(a = 21.16322, b = 8.83669, c = 2.957765),trace=TRUE, data=mydata ....
[TRUNCATED]
'data.frame': 12 obs. of 2 variables:
$ X: num 1 0 0 4 3 5 12 10 12 100 ...
$ Y: num 5.52 1.52 3.36 6.72 7.42 ...
NULL
formula: Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2
lower:[1] -Inf -Inf -Inf
upper:[1] Inf Inf Inf
$watch
[1] FALSE
$phi
[1] 1
$lamda
[1] 1e-04
$offset
[1] 100
$laminc
[1] 10
$lamdec
[1] 4
$femax
[1] 10000
$jemax
[1] 5000
Data variable Y : [1] 5.5199668 1.5234525 3.3557000 6.7211704 7.4237955 1.9703127
[7] 4.3939336 -1.4380091 3.2650180 3.5760906 0.2947972 1.0569417
Data variable X : [1] 1 0 0 4 3 5 12 10 12 100 100 100
Start:lamda: 1e-04 SS= 151.098 at a = 21.16322 b = 8.83669 c = 2.957765 1 / 0
gradient projection0 = -113.7506 gangle= -0.2949267
Stepsize= 1
<<lamda: 4e-05 SS= 127.1308 at a = 102.7381 b = 11.32418 c = 20.15418 2 / 1
gradient projection0 = -55.55629 gangle= -0.1594696
Stepsize= 1
lamda: 4e-04 SS= 129.9286 at a = 378.4733 b = -71.39815 c = 50.21802 3 / 2
gradient projection0 = -49.05021 gangle= -0.505497
[snip]
lamda: 2814.75 SS= 50.50144 at a = 36.13314 b = 2.572373 c = 1.079811 42 / 25
gradient projection0 = -5.685471e-20 gangle= -0.9954213
Stepsize= 1
lamda: 28147.5 SS= 50.50144 at a = 36.13314 b = 2.572373 c = 1.079811 43 / 25
gradient projection0 = -5.68707e-21 gangle= -0.9954364
Stepsize= 1
lamda: 281475 SS= 50.50144 at a = 36.13314 b = 2.572373 c = 1.079811 44 / 25
gradient projection0 = -5.687227e-22 gangle= -0.995437
Stepsize= 1
No parameter change
> print(ratelogisn)
$resid
[1] -0.3897067 1.0662193 -0.7660282 -1.1606765 0.6222073 0.9203074
[7] -4.3885301 1.4723895 -3.2596145 -3.5760906 -0.2947972 -1.0569417
$jacobian
a b c
[1,] 1.419821e-01 -2.954633e+00 -4.486669e-01
[2,] 7.167026e-02 -1.992780e+00 2.349023e+00
[3,] 7.167026e-02 -1.992780e+00 2.349023e+00
[4,] 1.538890e-01 2.981895e+00 -1.207117e+00
[5,] 2.226765e-01 1.456449e+00 -6.874521e+00
[6,] 7.999913e-02 2.165639e+00 2.191813e+00
[7,] 1.495441e-04 5.002498e-03 3.867174e-02
[8,] 9.514926e-04 3.177379e-02 1.867210e-01
[9,] 1.495441e-04 5.002498e-03 3.867174e-02
[10,] 6.050186e-40 2.024541e-38 1.806428e-36
[11,] 6.050186e-40 2.024541e-38 1.806428e-36
[12,] 6.050186e-40 2.024541e-38 1.806428e-36
$feval
[1] 44
$jeval
[1] 25
$coeffs
[1] 36.133144 2.572373 1.079811
$ssquares
[1] 50.50144
> ratelogis <- try(nls(Y ~ a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2,
+ start=list(a = 36.133144, b= 2.572373, c=1.079811),trace=TRUE, data=mydat ....
[TRUNCATED]
50.50144 : 36.133144 2.572373 1.079811
> print(ratelogis)
Nonlinear regression model
model: Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2
data: mydata
a b c
36.133 2.572 1.080
residual sum-of-squares: 50.5
Number of iterations to convergence: 0
Achieved convergence tolerance: 4.818e-07
>
On 04/17/2012 06:00 AM, r-help-request at r-project.org wrote:
> Message: 112
> Date: Mon, 16 Apr 2012 23:23:07 -0500
> From: "Francisco Mora Ardila" <fmora at oikos.unam.mx>
> To: r-help at r-project.org
> Subject: [R] error using nls with logistic derivative
> Message-ID: <20120417035718.M33436 at oikos.unam.mx>
> Content-Type: text/plain; charset=utf-8
>
> Hi
>
> I?m trying to fit a nonlinear model to a derivative of the logistic function
>
> y = a/(1+exp((b-x)/c)) (this is the parametrization for the SSlogis function with nls)
>
> The derivative calculated with D function is:
>
>> > logis<- expression(a/(1+exp((b-x)/c)))
>> > D(logis, "x")
> a * (exp((b - x)/c) * (1/c))/(1 + exp((b - x)/c))^2
>
> So I enter this expression in the nls function:
>
> ratelogis <- nls(Y ~ a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2,
> start=list(a = 21.16322, b = 8.83669, c = 2.957765),
> )
>
> The data is:
>
>> > Y
> [1] 5.5199668 1.5234525 3.3557000 6.7211704 7.4237955 1.9703127
> [7] 4.3939336 -1.4380091 3.2650180 3.5760906 0.2947972 1.0569417
>> > X
> [1] 1 0 0 4 3 5 12 10 12 100 100 100
>
> The problem is that I got the next error:
>
> Error en nls(Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2, :
> step factor 0.000488281 reduced below 'minFactor' of 0.000976563
>
> I trien to change the minFactor using the control argument inside nls
>
> control=nls.control(maxiter=50, tol=1e-5, minFactor = 1/2048
>
> but got a new error message:
>
>
> Error en nls(Y ~ a * (exp((b - X)/c) * (1/c))/(1 + exp((b - X)/c))^2, :
> step factor 0.000244141 reduced below 'minFactor' of 0.000488281
>
> So it seems that as I modify minFactor, the step factor reduces also and I can never
> reach a solution.
>
> Does anybody Know what am I doing wrong? Is there a problem with the formula? How can I
> solve it? I tried some suggestions on R-help related topics but did not work.
>
> Thanks
>
> Francisco
More information about the R-help
mailing list