[R] Non linear Regression: "singular gradient matrix at initial parameter estimates"

2,164 views
Skip to first unread message

Felix Nensa

unread,
Apr 11, 2011, 4:29:22 PM4/11/11
to r-h...@r-project.org
Hi,

I am using nls to fit a non linear function to some data but R keeps giving
me "singular gradient matrix at initial parameter estimates" errors.
For testing purposes I am doing this:

### R code ###

x <- 0:140
y <- 200 / (1 + exp(17 - x)/2) * exp(-0.02*x) # creating 'perfect' samples
with fitting model
yeps <- y + rnorm(length(y), sd = 2) # adding noise

# results in above error
fit = nls(yeps ~ p1 / (1 + exp(p2 - x) / p3) * exp(p4 * x))

###

>From what I've found in this list I think that my model is over-parameterized.
How can I work around that?
Thanks,

Felix

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Peter Ehlers

unread,
Apr 12, 2011, 5:10:05 AM4/12/11
to Felix Nensa, r-h...@r-project.org
On 2011-04-11 13:29, Felix Nensa wrote:
> Hi,
>
> I am using nls to fit a non linear function to some data but R keeps giving
> me "singular gradient matrix at initial parameter estimates" errors.
> For testing purposes I am doing this:
>
> ### R code ###
>
> x<- 0:140
> y<- 200 / (1 + exp(17 - x)/2) * exp(-0.02*x) # creating 'perfect' samples
> with fitting model
> yeps<- y + rnorm(length(y), sd = 2) # adding noise
>
> # results in above error
> fit = nls(yeps ~ p1 / (1 + exp(p2 - x) / p3) * exp(p4 * x))
>
> ###
>
>> From what I've found in this list I think that my model is over-parameterized.
> How can I work around that?

Take out p3; it's redundant.

Peter Ehlers

Felix Nensa

unread,
Apr 12, 2011, 12:01:33 PM4/12/11
to Peter Ehlers, r-h...@r-project.org
Hi Peter,

thank you for your reply. Now I see, that P3 is indeed redundand.
But with the simplified model...

fit = nls(yeps ~ p1 / (1 + exp(p2 - x)) * exp(p4 * x))

...nls still produces the same error.
Any ideas?

Felix

2011/4/12 Peter Ehlers <ehl...@ucalgary.ca>

Mario Valle

unread,
Apr 12, 2011, 12:53:08 PM4/12/11
to Felix Nensa, R help
Use a more realistic starting point instead of the default one:

fit <- nls(yeps ~ p1 / (1 + exp(p2 - x)) * exp(p4 * x),
start=list(p1=410,p2=18,p4=-.03))

This works for me:
> fit
Nonlinear regression model
model: yeps ~ p1/(1 + exp(p2 - x)) * exp(p4 * x)
data: parent.frame()
p1 p2 p4
199.48276 16.28664 -0.01987
residual sum-of-squares: 560.6

Number of iterations to convergence: 5
Achieved convergence tolerance: 5.637e-07

Ciao!
mario

On 12-Apr-11 18:01, Felix Nensa wrote:
> fit = nls(yeps ~ p1 / (1 + exp(p2 - x)) * exp(p4 * x))
>

--
Ing. Mario Valle
Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82

Felix Nensa

unread,
Apr 12, 2011, 12:54:51 PM4/12/11
to Mario Valle, R help
Hi Mario,
yes works great. Thanks!

2011/4/12 Mario Valle <mva...@cscs.ch>

> Use a more realistic starting point instead of the default one:
>
> fit <- nls(yeps ~ p1 / (1 + exp(p2 - x)) * exp(p4 * x),
> start=list(p1=410,p2=18,p4=-.03))
>
> This works for me:
> > fit
> Nonlinear regression model
> model: yeps ~ p1/(1 + exp(p2 - x)) * exp(p4 * x)
> data: parent.frame()
> p1 p2 p4
> 199.48276 16.28664 -0.01987
> residual sum-of-squares: 560.6
>
> Number of iterations to convergence: 5
> Achieved convergence tolerance: 5.637e-07
>
> Ciao!
> mario
>
>
> On 12-Apr-11 18:01, Felix Nensa wrote:
>
>> fit = nls(yeps ~ p1 / (1 + exp(p2 - x)) * exp(p4 * x))
>>
>>
> --
> Ing. Mario Valle
> Data Analysis and Visualization Group |
> http://www.cscs.ch/~mvalle
> Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60
> v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82
>
>


--
Felix Nensa

Luisenstr. 15-17
44787 Bochum
Germany

mail: felix...@googlemail.com
mobile: +49 171 958 51 40

[[alternative HTML version deleted]]

Reply all
Reply to author
Forward
0 new messages