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…
m<-c(-0.2061826,0.5888406,0.2026079,1.0000000,0.2342754,0.6865078,-0.1265754)
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[1]*xx+par[2]*cos(xx)+par[3]*sin(xx)+par[4]*cos(2*xx)+par[5]*sin(2*xx)+par[6]*cos(3*xx)
plot(x,m)
points(xx,mm,type="l")
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
described
as silly.
(2) Why on earth are you using nls()? Your model is linear. You could
simply
do
p <- lm(m ~ 0 + x + I(cos(x)) + I(sin(x)) + I(cos(2*x)) + I(sin(2*x)) +
I(cos(3*x)))
(3) Even with the ridiculously large number of parameters relative to
the number
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
problem with
flashy tools.
cheers,
Rolf Turner
______________________________________________
R-help@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.