Hello everyone, I was trying to fit a spline to some points and I was quite surprised to find out that the function spline does not take into account the order of the points themselves, but orders them by x. For instance, I have: x <- c(262, 275, 264, 250, 247, 242, 238, 233) y <- c(422, 389, 359, 308, 269, 229, 191, 176) plot(x, y, xlim=c(0, 500), ylim=c(0,500)) s <- spline(x,y) points(s, type="l", lwd=2) As you can see from the above examplethe spline is fitted not using the points in the order that was passed, but ordering them by x. Is there a way to prevent this? I also tried loess and other packages that fit curves to points but didn't have any luck either thanks nico -- View this message in context: old.nabble.com/order-of-points-in-spline-tp26230875p26230875.html Sent from the R help mailing list archive at Nabble.com.
On Fri, 6 Nov 2009, _nico_ wrote:> > Hello everyone, > > I was trying to fit a spline to some points and I was quite surprised to > find out that the function spline does not take into account the order of > the points themselves, but orders them by x. > > For instance, I have: > x <- c(262, 275, 264, 250, 247, 242, 238, 233) > y <- c(422, 389, 359, 308, 269, 229, 191, 176) > plot(x, y, xlim=c(0, 500), ylim=c(0,500)) > s <- spline(x,y) > points(s, type="l", lwd=2) > > As you can see from the above examplethe spline is fitted not using the > points in the order that was passed, but ordering them by x. > > Is there a way to prevent this?Huh? Prevent spline from using (x[n],y[n]) as the coordinates of the n^{th} data point? Why on earth would you want to do that? Do you think that this:> all.equal( spline(x,y) , spline( rev(x) , rev(y)))[1] TRUE>should have returned FALSE?? Chuck p.s. Hint: Respond by citing a reference in the peer reviewed math/stats literature that describes a function that does what you want and folks may be able to help you.> I also tried loess and other packages that fit curves to points but didn't > have any luck either > > thanks > nico > -- > View this message in context: old.nabble.com/order-of-points-in-spline-tp26230875p26230875.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego famprevmed.ucsd.edu/faculty/cberry La Jolla, San Diego 92093-0901
> Huh? > > Prevent spline from using (x[n],y[n]) as the coordinates of the n^{th} data point? > > Why on earth would you want to do that? >Maybe I did not explain myself correctly. My first three points have x: 262, 275, 264 I want the spline to go from point 1 (x=262) to point 2 (x=275) and then go back on the x axis to point 3 (x=264). The function goes from 1 (x=262) to 3 (x=264) to 2 (x=275) Please, run my example and you should see for yourself.
The spline function (and loess and others) fit regression splines (or approximations) meaning that each value of x maps to only 1 value of y. If you want a curve that goes through points in the specified order and is able to wrap back then you probably want the xspline function. See its help page for details. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Nicola Roman? > Sent: Friday, November 06, 2009 2:07 PM > To: r-help at r-project.org > Subject: Re: [R] order of points in spline > > > Huh? > > > > Prevent spline from using (x[n],y[n]) as the coordinates of the > n^{th} data point? > > > > Why on earth would you want to do that? > > > > Maybe I did not explain myself correctly. > > My first three points have x: 262, 275, 264 > > I want the spline to go from point 1 (x=262) to point 2 (x=275) and > then go back on the x axis to point 3 (x=264). > The function goes from 1 (x=262) to 3 (x=264) to 2 (x=275) > > Please, run my example and you should see for yourself. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.