[R] fitting sine wave
rolf.turner at xtra.co.nz
Thu Jul 28 11:27:20 CEST 2011
On 28/07/11 02:36, Tonja Krueger wrote:
> Dear R-helpers
> I have 7 data points that I want to fit a continuous curve to, that should look similar to a sine wave
> My data points would mark the local minima and maxima respectively.
> This is what I’ve got so far. And I would keep doing so, but sadly nls() then says that it has reached the maximum number of Iterations…
> x<- c(1,2,3,4,5,6,7)
> p<- nls(m~k1*x+k2*cos(x)+k3*sin(x)+k4*cos(2*x)+k5*sin(2*x)+k6*cos(3*x),start = list(k1=0,k2=0,k3=0.1,k4=0.1,k5=0,k6=0))
> par<- c(pk1=summary(p)$parameters[1,1],pk2=summary(p)$parameters[2,1],pk3=summary(p)$parameters[3,1],pk4=summary(p)$parameters[4,1],pk5=summary(p)$parameters[5,1],pk6=summary(p)$parameters[6,1])
> xx<- seq(1,7,length.out=500)
> mm<- par*xx+par*cos(xx)+par*sin(xx)+par*cos(2*xx)+par*sin(2*xx)+par*cos(3*xx)
> I was also thinking of using fft(), but when I use the inverse function I only get my 7 original points back, but no smooth sine function.
> Thank you for your suggestions.
(1) You are fitting 6 parameters to 7 data points. That can only be
(2) Why on earth are you using nls()? Your model is linear. You could
p <- lm(m ~ 0 + x + I(cos(x)) + I(sin(x)) + I(cos(2*x)) + I(sin(2*x)) +
(3) Even with the ridiculously large number of parameters relative to
of data, you still won't get anything like a good fit.
(4) Think about what you really need to do. Don't just hammer the
More information about the R-help