order of points in spline

View: New views
5 Messages — Rating Filter:   Alert me  

order of points in spline

by _nico_ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: order of points in spline

by Charles C. Berry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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: http://old.nabble.com/order-of-points-in-spline-tp26230875p26230875.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@... 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry@...            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
R-help@... 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.

Re: order of points in spline

by _nico_ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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@... 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.

Re: order of points in spline

by Greg Snow-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
801.408.8111


> -----Original Message-----
> From: r-help-bounces@... [mailto:r-help-bounces@r-
> project.org] On Behalf Of Nicola RomanĂ²
> Sent: Friday, November 06, 2009 2:07 PM
> To: r-help@...
> 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@... 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.

______________________________________________
R-help@... 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.

Re: order of points in spline

by _nico_ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At the end, although the xspline function suggested by Greg did what I needed, it suffers from the fact that you cannot specify the number of points of the output curve.
I then wrote my function for a generic nth grade Bezier curve interpolation.
You can find the code here if ever you're interested.
http://rosettacode.org/wiki/Cubic_bezier_curves#R

>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@imail.org
>801.408.8111


> -----Original Message-----
> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Nicola RomanĂ²
> Sent: Friday, November 06, 2009 2:07 PM
> To: r-help@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@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.

______________________________________________
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.