Skip to content

fit data to y~A+B*sin(C*x)

5 messages · Jonas Stein, Sarah Goslee, David L Carlson +2 more

#
I want to fit discrete data that was measured on a wavegenerator.
In this minimal example i generate some artificial data:

testsin <- 2+ 5 * sin(1:100) #generate sin data
testsin <-  testsin+ rnorm(length(testsin), sd = 0.01) #add noise

mydata <- list(X=1:100, Y=testsin) # generate mydata object

nlmod <- nls(X ~ A+B*sin(C* Y), data=mydata, start=list(A=2, B=4, C=1), trace=TRUE)

# this nls fit fails. 

Who can help me to fit this type of data?
Kind regards,
#
Hi Jonas,

I'm afraid we need more detail.
On Mon, Feb 13, 2012 at 5:54 PM, Jonas Stein <news at jonasstein.de> wrote:
Fails how? It runs when I copy and paste your code into a terminal, given:
R version 2.14.1 (2011-12-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

What happens when you run it? What do you expect to happen?
What's your sessionInfo()?

"fails" doesn't convey enough information here.

Sarah
#
Don't you want to predict testsin (aka Y) from X?
trace=TRUE)
50.42965 :  2 4 1 
0.007393982 :  1.9989006 5.0014997 0.9999902 
0.007377637 :  1.9989005 5.0015002 0.9999922 
0.007377637 :  1.9989005 5.0015002 0.9999922
Nonlinear regression model
  model:  Y ~ A + B * sin(C * X) 
   data:  mydata 
    A     B     C 
1.999 5.002 1.000 
 residual sum-of-squares: 0.007378

Number of iterations to convergence: 3 
Achieved convergence tolerance: 1.499e-08

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Sarah Goslee
Sent: Tuesday, February 14, 2012 9:13 AM
To: Jonas Stein
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] fit data to y~A+B*sin(C*x)

Hi Jonas,

I'm afraid we need more detail.
On Mon, Feb 13, 2012 at 5:54 PM, Jonas Stein <news at jonasstein.de> wrote:
trace=TRUE)
Fails how? It runs when I copy and paste your code into a terminal, given:
R version 2.14.1 (2011-12-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

What happens when you run it? What do you expect to happen?
What's your sessionInfo()?

"fails" doesn't convey enough information here.

Sarah
#
On 13-02-2012, at 23:54, Jonas Stein wrote:

            
What do you mean by "fail"
Nonlinear regression model
  model:  X ~ A + B * sin(C * Y) 
   data:  mydata 
      A       B       C 
50.7553  0.6308  0.8007 
 residual sum-of-squares: 83308

Number of iterations to convergence: 24 
Achieved convergence tolerance: 7.186e-06 

Results don't seem to look ok.
But I think you made a small mistake in the formula.
The argument to sin in testsin is 1:100 but that's not what you are giving nls.

Try this
50.30593 :  2 4 1 
0.01014092 :  2.0003732 5.0002681 0.9999979 
0.01014016 :  2.0003732 5.0002681 0.9999983
Nonlinear regression model
  model:  Y ~ A + B * sin(C * X) 
   data:  mydata 
A B C 
2 5 1 
 residual sum-of-squares: 0.01014

Number of iterations to convergence: 2 
Achieved convergence tolerance: 1.201e-07 

Looks better?

Berend
#
Note that, given C, A and B can be obtained by simple linear regression of y on sin(Cx). Hence you could avoid nls altogether by a simple search of the minimal ls solution(possibly robust) over a grid of C values. Or do this to find good starting values for nls.

Bert

Sent from my iPhone -- please excuse typos.
On Feb 14, 2012, at 7:24 AM, Berend Hasselman <bhh at xs4all.nl> wrote: