Re: [R] Fit a sine to data

From: <apjaworski_at_mmm.com>
Date: Fri, 23 May 2008 10:19:27 -0500


Milan,

This is a fairly standard trick. Let us generalize your equation slightly:

y ~ a + c*sin(x+b)

so the amplitude of the sine wave is adjustable (otherwise, you assume (or know) that the amplitude is 1). Then

y ~ a + c*sin(b)*cos(x) + c*cos(b)*sin(x)

or

y ~ b0 + b1*x1 + b2*x2

which is a linear regression form that you can do using the lm function. After you get b0, b1 and b2 you do

a = b0
b1^2 + b2^2 = c^2*(sin^2(b) + cos^2(b)) = c^2 ====> c = sqrt(b1^2 + b2^2) b1/b2 = tan(b) ====> b = arctan(b1/b2)

Hope this helps,

Andy



Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory

E-mail: apjaworski_at_mmm.com
Tel: (651) 733-6092
Fax: (651) 736-3122
                                                                           
             "Zroutik Zroutik"                                             
             <zroutik_at_gmail.co                                             
             m>                                                         To 
             Sent by:                  r-help_at_r-project.org                
             r-help-bounces_at_r-                                          cc 
             project.org                                                   
                                                                   Subject 
                                       [R] Fit a sine to data              
             05/23/2008 08:49                                              
             AM                                                            
                                                                           
                                                                           
                                                                           
                                                                           




Dear R-users,

I'd like to fit a sine function to my data. The result should have a format (and thus the formula, too)

y ~ a + sin(x+b)

where y and x are vectors, and a and b are (yet) unknown values.

The data sets (vectors x and y) are OK, and I can do a simple lm fitting lm(y~x), or lm(y~I(sin(2*pi*x/360))), succesfully My issue is that I'm not able to do the optional linear shift in x (e.g. x+b). What other possibilities should I use instead of lm? I looked searched the R-seek for terms: fit, sine, lm, and combinations, but I could not find anything useful. Anybody would be so kind and point me to the right direction where I
can find other fitting possibilities?

Thank you for listening and kind answers upfront.

Regards,
Milan

             [[alternative HTML version deleted]]



R-help_at_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.

R-help_at_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. Received on Fri 23 May 2008 - 16:53:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 26 May 2008 - 12:30:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive