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 ---------------------- Francisco Mora Ardila Estudiante de Doctorado Centro de Investigaciones en Ecosistemas Universidad Nacional Aut?noma de M?xico
On Apr 17, 2012, at 06:23 , Francisco Mora Ardila wrote:> 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.(Please don't send private messages. I don't do free consulting outside of the mailing lists.) With nls(), there's really no substitute for good data and good starting values. Did you actually try plotting those data? At best, they are extremely noisy. How did you obtain the starting values? They seem remarkably accurate for something that fits data so poorly. What is happening is that the algorithm shoots off into a region of the parameter space where it can't make any progress:> 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),+ trace=TRUE)151.098 : 21.163220 8.836690 2.957765 127.1149 : 103.49326 11.43274 20.29663 111.344 : 833.02386 -45.86244 140.32985 111.3375 : 978.97214 -76.20571 159.90833 111.3374 : 1097.1376 -101.6771 174.2037 111.3228 : 1179.8451 -119.7416 183.3794 Notice that the "b" parameter which is supposed to be the maximum point starts off well to the right of the position of the largest Y values, then shoots into large negative values. With a little eyeballing, I can do better:> ratelogis <- nls(Y ~ a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2,+ start=list(a = 40, b = 3.5, c = 10),trace=T) 139.5173 : 40.0 3.5 10.0 97.28614 : 26.513424 4.052639 2.267709 81.53127 : 32.384910 3.473411 2.307504 65.11387 : 39.53542 3.01097 2.07678 50.66328 : 36.223529 2.590102 1.133965 50.50921 : 35.984466 2.565698 1.067731 50.50162 : 36.149993 2.573420 1.081673 50.50145 : 36.129962 2.572195 1.079504 50.50144 : 36.133656 2.572402 1.079862 50.50144 : 36.133061 2.572368 1.079803 However, the fit isn't exactly stellar. Try this: X <- c(1, 0, 0, 4, 3, 5, 12, 10, 12, 100, 100, 100) Y <- c(5.5199668, 1.5234525, 3.3557, 6.7211704, 7.4237955, 1.9703127, 4.3939336, -1.4380091, 3.265018, 3.5760906, 0.2947972, 1.0569417) plot(X,Y) ratelogis <- nls(Y ~ a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2, start=list(a = 40, b = 3.5, c = 10),trace=T) x0 <- seq(0,100,,1000) lines(x0,predict(ratelogis,newdata=data.frame(X=x0))) lines(x0,evalq(a*(exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2,envir=list(a = 21.16322, b = 8.83669, c = 2.957765, X=x0)), lty="dashed") -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On Tue, Apr 17, 2012 at 12:23 AM, Francisco Mora Ardila <fmora at oikos.unam.mx> wrote:> 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 >Try alg = "plinear" noting that we must drop the linear coefficient a from the formula and starting values and .lin in the output represents a: ratelogis <- nls(Y ~ (exp((b-X)/c)*(1/c))/(1 + exp((b-X)/c))^2, start=list(b = 8.83669, c = 2.957765), alg = "plinear" ) ratelogis plot(X,Y) o <- order(X) lines(X[o], fitted(ratelogis)[o], col = "red") -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
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