[R] question about nls

Prof J C Nash (U30A) nashjc at uottawa.ca
Fri Mar 15 14:45:15 CET 2013


Actually, it likely won't matter where you start. The Gauss-Newton 
direction is nearly always close to 90 degrees from the gradient, as 
seen by turning trace=TRUE in the package nlmrt function nlxb(), which 
does a safeguarded Marquardt calculation. This can be used in place of 
nls(), except you need to put your data in a data frame. It finds a 
solution pretty straightforwardly, though with quite a few iterations 
and function evaluations.

Of course, one may not really want to do any statistics with 4 
observations and 3 parameters, but the problem illustrates the GN vs. 
Marquardt directions.

JN


 > sol<-nlxb(y ~ exp(a + b*x)+d,start=list(a=0,b=0,d=1), data=mydata, 
trace=T)
formula: y ~ exp(a + b * x) + d
lower:[1] -Inf -Inf -Inf
upper:[1] Inf Inf Inf
...snip...
Data variable  y :[1]  0.8  6.5 20.5 45.9
Data variable  x :[1]  60  80 100 120
Start:lamda: 1e-04  SS= 2291.15  at  a = 0  b = 0  d = 1  1 / 0
gradient projection =  -2191.093  g-delta-angle= 90.47372
Stepsize= 1
lamda: 0.001  SS= 4.408283e+55  at  a = -25.29517  b = 0.74465  d = 
-24.29517  2 / 1
gradient projection =  -2168.709  g-delta-angle= 90.48307
Stepsize= 1
lamda: 0.01  SS= 3.986892e+54  at  a = -24.55223  b = 0.7284461  d = 
-23.55223  3 / 1
gradient projection =  -1991.804  g-delta-angle= 90.58199
Stepsize= 1
lamda: 0.1  SS= 2.439544e+46  at  a = -18.71606  b = 0.6010118  d = 
-17.71606  4 / 1
gradient projection =  -1476.935  g-delta-angle= 92.79733
Stepsize= 1
lamda: 1  SS= 4.114152e+23  at  a = -2.883776  b = 0.2505892  d = 
-1.883776  5 / 1
gradient projection =  -954.5234  g-delta-angle= 91.78881
Stepsize= 1
lamda: 10  SS= 39033042903  at  a = 2.918809  b = 0.07709855  d = 
3.918809  6 / 1
gradient projection =  -264.9953  g-delta-angle= 91.41647
Stepsize= 1
<<lamda: 4  SS= 571.451  at  a = 1.023367  b = 0.01762421  d = 2.023367 
  7 / 1
gradient projection =  -60.46016  g-delta-angle= 90.96421
Stepsize= 1
<<lamda: 1.6  SS= 462.3257  at  a = 1.080764  b = 0.0184132  d = 
1.981399  8 / 2
gradient projection =  -56.91866  g-delta-angle= 90.08103
Stepsize= 1
<<lamda: 0.64  SS= 359.6233  at  a = 1.135265  b = 0.01942354  d = 
0.9995471  9 / 3
gradient projection =  -65.90027  g-delta-angle= 90.04527
Stepsize= 1

... snip ...

lamda: 0.2748779  SS= 0.5742948  at  a = -0.1491842  b = 0.03419761  d = 
-6.196575  31 / 20
gradient projection =  -6.998402e-25  g-delta-angle= 90.07554
Stepsize= 1
lamda: 2.748779  SS= 0.5742948  at  a = -0.1491842  b = 0.03419761  d = 
-6.196575  32 / 20
gradient projection =  -2.76834e-25  g-delta-angle= 90.16973
Stepsize= 1
lamda: 27.48779  SS= 0.5742948  at  a = -0.1491842  b = 0.03419761  d = 
-6.196575  33 / 20
gradient projection =  -4.632864e-26  g-delta-angle= 90.08759
Stepsize= 1
No parameter change



On 13-03-15 07:00 AM, r-help-request at r-project.org wrote:
> Message: 36
> Date: Thu, 14 Mar 2013 11:04:27 -0400
> From: Gabor Grothendieck<ggrothendieck at gmail.com>
> To: meng<laomeng_3 at 163.com>
> Cc: R help<r-help at r-project.org>
> Subject: Re: [R] question about nls
> Message-ID:
> 	<CAP01uRmoDFN87QQvTWMaTuid0FX0d7LqMFqh4ChoFm5B2C9-tQ at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Thu, Mar 14, 2013 at 5:07 AM, meng<laomeng_3 at 163.com>  wrote:
>> >Hi,all:
>> >I met a problem of nls.
>> >
>> >My data:
>> >x    y
>> >60 0.8
>> >80 6.5
>> >100 20.5
>> >120 45.9
>> >
>> >I want to fit exp curve of data.
>> >
>> >My code:
>>> >>nls(y ~ exp(a + b*x)+d,start=list(a=0,b=0,d=1))
>> >Error in nlsModel(formula, mf, start, wts) :
>> >   singular gradient matrix at initial parameter estimates
>> >
>> >I can't find out the reason for the error.
>> >Any suggesions are welcome.
>> >
> The gradient is singular at your starting value so you will have to
> use a better starting value.  If d = 0 then its linear in log(y) so
> you can compute a starting value using lm like this:
>
> lm1 <- lm(log(y) ~ x, DF)
> st <- setNames(c(coef(lm1), 0), c("a", "b", "d"))
>
> Also note that you are trying to fit a model with 3 parameters to only
> 4 data points.
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com



More information about the R-help mailing list