[R] Upper limit in nlsLM not working as expected
Martin.Hehn at postgrad.manchester.ac.uk
Fri Oct 19 12:18:53 CEST 2012
Thanks for the reply Berend,
I am aware that I do not have to define the limits to Inf or -Inf, I just did this
to make sure all other variables (besides 'w') in 'upper' have no limits.
I can tolerate no upper limit as my peak is fitted well, but my actual data is a time series where the peak disappears from time to time. Once disappeared, the algorithm is still fitting to the noise of the experimental dataset.
I thought that it would be handy to suppress this by simply specifying a limit for how wide I allow the peak to be as it becomes very broad when fitting to noise. Any ideas on how to avoid fitting to the noise?
From: Berend Hasselman [bhh at xs4all.nl]
Sent: 18 October 2012 17:45
To: Martin Hehn
Cc: r-help at R-project.org
Subject: Re: [R] Upper limit in nlsLM not working as expected
On 18-10-2012, at 14:16, Martin Hehn wrote:
> Dear all,
> I am using the nlsLM function to fit a Lorentzian function to my experimental data.
> The LM algorithm should allow to specify limits, but the upper limit appears not to work as expected in my code.
> The parameter 'w', which is peak width at half maximuim always hits the upper limit if the limit is specified. I would expect the value to be in-between the upper and lower limit with maybe hitting either limit occasionally.
> The code below calculates a lorentzian curve with some noise added that looks like a typical experimental spectrum. Then a fit is made to this data. Note that if 'w' in the upper limit is set to e.g 0.6, 0.7, or 0.8 the fit always hits this limit. If 'w'=Inf, the fit calculates to something around 0.06, which is correct.
> Why does the fit go wrong if 'w' is not Inf?
The upper limit for 'w' does not need to be set to Inf. It needs to be set high enough for LM to 'work'.
If you set the upper limit for 'w' to 10 the algorithm will also estimate 'w' at 0.06..
When you set the upper limit to low values as you have done, you are not giving LM enough room to manoeuvre.
If you set trace to TRUE (please don't use F and T) you'll see that 'w' hits the upper limit and that it stays there.
> #Create x axis
> #Simulate a function with noise
> eq<-y0 + 2*A/pi*w/(4*(x-xc)^2+w^2)
> #Evaluate function aka Measured data
> #Start values for fitting
> #Fit function to data
> fit<-nlsLM(y ~ y0 + 2*(A/pi)*w/(4*(x-xc)^2+w^2),
> ftol = .Machine$double.eps,
> ptol = .Machine$double.eps
> #Predict fitting values
> Best regard
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help