[R] Non-linear fit?

S Ellison S.Ellison at LGCGroup.com
Wed Nov 25 17:11:43 CET 2015



> -----Original Message-----
> On 11/24/2015 09:32 PM, Judson wrote:
> > I need to fit a sinusoidal curve to
> > x-y data that exhibits a sinusoidal
> > pattern.   The curve will be:
> >   y = a*sin(w*x +p) ;
> > where I need to get the best
> > fit choice for the parameters
> > a, w, and p.   Could anyone
> > suggest which package and
> > routine I should use?   I have
> > less than 1000 data points.
> > Can this problem be somehow
> > coerced into a linear fit?
> > ....... judson blake
> 
> You may take a look at the nlme library.
> --
> Ulises M. Alvarez

nlme includes a nonlinear _mixed effects_ model, but non-linear least squares fitting is well catered for already. nlm, nls and optim in the core distribution all cover non-linear fitting.

But you'll need good starting values.

Life could be easier with a reformulation expanding sin(w*x + p) to 
y = alpha sin(w*x) + beta * cos(w*x) 

where alpha=a*cos(p) and beta = a * sin(p)
(if my mental trig is working)

Given a good starting value for w (eg from an FFT) that would allow an initial linear (ie lm() )  fit to cos(w*x) + sin(w*x) to get alpha and beta, and hence a and p. Those values could then be used as starting values for optim or similar.

S Ellison


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list