Skip to content

fitting periodic 'sine wave' model

4 messages · Jon Loehrke, Gabor Grothendieck, Charilaos Skiadas +1 more

#
I have been attempting to estimate the periodic contribution of an  
effect to some data but have not been able to fit a sine wave within R.
It would be nice to start by being able to fit a sine wave with an  
amplitude and frequency.

x<-seq(0,20,by=0.5)
y<-2*sin(2*pi*.5*x) #amplitude =2, frequency=0.5

# This failed to converge
r<-nls(y ~ A*sin(2*pi*F*x), start=list(A = 1, F = 1), trace=T)


# even this gave a max iteration error
r<-nls(y ~ A*sin(2*pi*F*x), start=list(A = 1, F = .5), trace=T)

I have a feeling I am approaching this incorrectly.  Thank you all  
very much for the guidance.

Jon
R 2.7.0
mac os 10.5


Jon Loehrke
Graduate Research Assistant
Department of Fisheries Oceanography
School for Marine Science and Technology
University of Massachusetts
200 Mill Road, Suite 325
Fairhaven, MA 02719
jloehrke at umassd.edu
T 508-910-6393
F 509-910-6396
#
Try

RSiteSearch("fit a sine")
On Tue, Jun 10, 2008 at 10:25 AM, Jon Loehrke <jloehrke at umassd.edu> wrote:
#
In addition to Gabor's suggestion, note the following warning from ?nls

Warning

Do not use nls on artificial "zero-residual" data.

The nls function uses a relative-offset convergence criterion that  
compares the numerical imprecision at the current parameter estimates  
to the residual sum-of-squares. This performs well on data of the form

y = f(x, theta) + eps

(with var(eps) > 0). It fails to indicate convergence on data of the  
form

y = f(x, theta)

because the criterion amounts to comparing two components of the  
round-off error. If you wish to test nls on artificial data please  
add a noise component, as shown in the example below.




So for instance if you try with:

r<-nls(y ~ A*sin(2*pi*F*x), start=list(A = 1, F = .5), trace=T)

You will get convergence.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College
On Jun 10, 2008, at 10:25 AM, Jon Loehrke wrote:

            
#
If you make the x-values more dense, e.g. with x<-seq(0,20,0.1) then your example works. Plotting your data might give a hint to why your example fails.... Btw: It is generally not a good idea to use F for a parameter because F usually means FALSE. 
Regards
S?ren

________________________________

Fra: r-help-bounces at r-project.org p? vegne af Jon Loehrke
Sendt: ti 10-06-2008 16:25
Til: r-help at r-project.org
Emne: [R] fitting periodic 'sine wave' model



I have been attempting to estimate the periodic contribution of an 
effect to some data but have not been able to fit a sine wave within R.
It would be nice to start by being able to fit a sine wave with an 
amplitude and frequency.

x<-seq(0,20,by=0.5)
y<-2*sin(2*pi*.5*x) #amplitude =2, frequency=0.5

# This failed to converge
r<-nls(y ~ A*sin(2*pi*F*x), start=list(A = 1, F = 1), trace=T)


# even this gave a max iteration error
r<-nls(y ~ A*sin(2*pi*F*x), start=list(A = 1, F = .5), trace=T)

I have a feeling I am approaching this incorrectly.  Thank you all 
very much for the guidance.

Jon
R 2.7.0
mac os 10.5


Jon Loehrke
Graduate Research Assistant
Department of Fisheries Oceanography
School for Marine Science and Technology
University of Massachusetts
200 Mill Road, Suite 325
Fairhaven, MA 02719
jloehrke at umassd.edu
T 508-910-6393
F 509-910-6396

______________________________________________
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.