0

I acquired data (motor adaptation $y$ as a function of delays $t$ ) which I expect to look like a sine wave. I am trying (1) to fit a sine curve in my data and (2) to estimate the best model/parameters. I read several posts here but I am sill struggling.

I tried to use nls

using the equation $y(t) = A \sin(\Omega t + \Phi) + C$ where $A$ is the amplitude, $\Omega$ the period, $\Phi$ the phase shift and $C$ the midline.

R CODE

t<-c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
y<-c(0.310 ,0.630 ,0.430 ,0.245, 0.650 ,0.085 ,0.370, 0.560 ,0.250, 0.520)

A<- (max(y)-min(y)/2)
C<-((max(y)+min(y))/2)

res1<- nls(y ~ A*sin(omega*t+phi)+C, data=data.frame(t,y), start=list(A=A,omega=pi/2,phi=0,C=C))
summary(res1)
co <- coef(res1)
resid(res1)
sum(resid(res1)^2)
fit <- function(x, a, b, c, d) {a*sin(b*x+c)+d}
# Plot result
plot(x=t, y=y)
curve(fit(x, a=co["A"], b=co["omega"], c=co["phi"], d=co["C"]), add=TRUE ,lwd=2, col="steelblue")

OUTPUT

Formula: y ~ A * sin(omega * t + phi) + C

Parameters:
       Estimate Std. Error t value Pr(>|t|)    
A       0.21956    0.03982   5.513   0.0015 ** 
omega   2.28525    0.07410  30.841 7.72e-08 ***
phi   -32.57364    0.40375 -80.678 2.44e-10 ***
C       0.41146    0.02926  14.061 8.07e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.09145 on 6 degrees of freedom

Number of iterations to convergence: 18 
Achieved convergence tolerance: 9.705e-06

GRAPH

enter image description here

QUESTIONS

-How can I improve the fit using this method? I tried to change some parameters manually but for example to change phase shift $\Phi$ does not do much or lead to an error.

Error in nls(y ~ A * sin(omega * t + phi) + C, data = data.frame(t, y),  : 
  le pas 0.000488281 est devenu inférieur à 'minFactor' de 0.000976562
Nick Cox
  • 48,377
  • 8
  • 110
  • 156
SophieB
  • 11
  • 3
  • I find your post far too long to read, but based on the first page it looks like you have an aliasing problem: are you familiar with the [Nyquist rate?](https://en.wikipedia.org/wiki/Nyquist_rate) – whuber Jan 15 '20 at 14:13
  • 1
    I tried to be as clear and exhaustive as possible, providing code/output/graph in order to help people like me (who are moslty learning on such forum). But thanks for closing my post. I will look into the Nyquist rate... – SophieB Jan 16 '20 at 10:13
  • $\sin(\theta + \phi)$ expands to $\sin \theta\ \cos \phi + \cos \theta\ \sin \phi$. As $\phi$ is a constant to be estimated, it's a lot simpler to use plain regression on sine and cosine as predictors. Then you can add other terms as needed. https://www.stata-journal.com/article.html?article=st0116 is freely accessible. – Nick Cox Jan 16 '20 at 10:18
  • $\Omega$ isn't quite the period. – Nick Cox Jan 16 '20 at 10:21
  • If you have only 10 data points, be wary of overfitting. – Nick Cox Jan 16 '20 at 10:23
  • In problems where this makes sense, the time variable is something periodic, e.g. time of day. What is it in your case? – Nick Cox Jan 16 '20 at 10:25
  • Ok so the full story is [here](https://stackoverflow.com/questions/59685878/sine-curve-fit-using-lm-nls-and-nls2-in-r/59690641#59690641) a moderator said it was too long and closed my intital topic .. i tried lm but cannot change the amplitude ... – SophieB Jan 16 '20 at 10:27
  • why omega is not quite the period? The time is something periodic yes, it is a a time delay. – SophieB Jan 16 '20 at 10:29
  • The larger $\Omega$ is, the faster the curve repeats. It measures frequency, not period (length). – Nick Cox Jan 16 '20 at 11:11
  • Please don't post new questions, Sophie, in order to fix problems with existing questions: just edit the existing one. That makes the comment thread (and any answers) available for users, which advances the conversation. I have merged your old question with this one. – whuber Jan 16 '20 at 14:56
  • Why should I not post new questions? I posted 1 post that you closed because it was too long, on your advice I divided my posts. Now you merged my old post with your previous comments which do not make sense as the post is not too long anymore... Also, why would I edit my post as I do not have any new info/solution? – SophieB Jan 17 '20 at 13:29

0 Answers0