cubic spline

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

cubic spline

Steve Stephenson
Hallo,
I'm facing a problem and I would really appreciate your support.
I have to translate some Matalb code in R that I don't know very well but I would like to.
I have to interpolate 5 point with a cubic spline function and then I expect my function returns the Y value as output a specific X value inside the evaluation range.  Let's suppose that:
  1-  X = [-10, -5, 0, 5, 10] 
  2 - Y = [12, 10, 8, 7, 6]
  3 - I have to interpolate with a cubic spline assuming x=11

In Matlab I used this function:

y = interp1(X, Y, x, "cubic");

How can I do the same in R?
Many thanks in advance for your reply and support!
Kindly

Steve
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

Jorge I Velez
Hi Steve,

Check the "splint" function in the "fields" package.

HTH,
Jorge.-


On Sat, Dec 1, 2012 at 11:09 PM, Steve Stephenson <> wrote:

> Hallo,
> I'm facing a problem and I would really appreciate your support.
> I have to translate some Matalb code in R that I don't know very well but I
> would like to.
> I have to interpolate 5 point with a cubic spline function and then I
> expect
> my function returns the Y value as output a specific X value inside the
> evaluation range.  Let's suppose that:
>   1-  *X = [-10, -5, 0, 5, 10]*
>   2 - *Y = [12, 10, 8, 7, 6]*
>   3 - *I have to interpolate with a cubic spline assuming x=11*
>
> In Matlab I used this function:
>
> *y = interp1(X, Y, x, "cubic"); *
>
> How can I do the same in R?
> Many thanks in advance for your reply and support!
> Kindly
>
> Steve
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/cubic-spline-tp4651537.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

Steve Stephenson
Hi Jorge,

many thanks for your suggestion!
I tried the function you mentioned and it looks working!!
Now I have just to check that splint (R) and interp1 (Matlab) functions provide me the same results.
Kind regards!

Steve

 
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

David Winsemius
In reply to this post by Steve Stephenson

On Dec 1, 2012, at 5:09 AM, Steve Stephenson wrote:

> Hallo,
> I'm facing a problem and I would really appreciate your support.
> I have to translate some Matalb code in R that I don't know very  
> well but I
> would like to.
> I have to interpolate 5 point with a cubic spline function and then  
> I expect
> my function returns the Y value as output a specific X value inside  
> the
> evaluation range.  Let's suppose that:
>  1-  *X = [-10, -5, 0, 5, 10]*
>  2 - *Y = [12, 10, 8, 7, 6]*
>  3 - *I have to interpolate with a cubic spline assuming x=11*
>
> In Matlab I used this function:
>
> *y = interp1(X, Y, x, "cubic"); *
>
> How can I do the same in R?
> Many thanks in advance for your reply and support!

splinefun( x = c(-10, -5, 0, 5, 10),
            y = c(12, 10, 8, 7, 6),
          method="natural")(11)
[1] 5.785714

--
David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

Martin Maechler
>>>>> David Winsemius <[hidden email]>
>>>>>     on Sat, 1 Dec 2012 09:25:42 -0700 writes:

    > On Dec 1, 2012, at 5:09 AM, Steve Stephenson wrote:

    >> Hallo, I'm facing a problem and I would really appreciate
    >> your support.  I have to translate some Matalb code in R
    >> that I don't know very well but I would like to.  I have
    >> to interpolate 5 point with a cubic spline function and
    >> then I expect my function returns the Y value as output a
    >> specific X value inside the evaluation range.  Let's
    >> suppose that: 1- *X = [-10, -5, 0, 5, 10]* 2 - *Y = [12,
    >> 10, 8, 7, 6]* 3 - *I have to interpolate with a cubic
    >> spline assuming x=11*
    >>
    >> In Matlab I used this function:
    >>
    >> *y = interp1(X, Y, x, "cubic"); *
    >>
    >> How can I do the same in R?  Many thanks in advance for
    >> your reply and support!

    > splinefun( x = c(-10, -5, 0, 5, 10), y = c(12, 10, 8, 7, 6),
                method="natural")(11) [1] 5.785714

Yes, indeed, or simple  spline(....)

but definitely *no* need to use a function from an extra CRAN
package .. as someone else ``erronously'' suggested.

Note that
  spline() and splinefun()
together with
  approx() and approxfun()
are among the several hundred functions that were already
part of "pre-alpha" R, i.e., before R had a version number or *any* packages ...
and yes, the README then started with the two lines

| R Source Code (Tue Jun 20 14:33:47 NZST 1995)
| Copyright 1993, 1994, 1995 by Robert Gentleman and Ross Ihaka

and it would be *really* *really* great
if people did not add stuff to their packages that has
been part of R for longer than they have even heard of R.

Martin Maechler, ETH Zurich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

bbolker
Martin Maechler <maechler <at> stat.math.ethz.ch> writes:

 [snip]

> but definitely *no* need to use a function from an extra CRAN
> package .. as someone else ``erronously'' suggested.
>
> Note that
>   spline() and splinefun()
> together with
>   approx() and approxfun()
> are among the several hundred functions that were already
> part of "pre-alpha" R, i.e., before R had a version number or *any*
> packages ...
> and yes, the README then started with the two lines
>
> | R Source Code (Tue Jun 20 14:33:47 NZST 1995)
> | Copyright 1993, 1994, 1995 by Robert Gentleman and Ross Ihaka
>
> and it would be *really* *really* great
> if people did not add stuff to their packages that has
> been part of R for longer than they have even heard of R.
>
> Martin Maechler, ETH Zurich

  To be fair, the 'fields' package has a pretty long history too --
I think it may have been ported from an S-PLUS 'package' (or whatever
the correct terminology is) that existed quite a while ago.

 I think it was the FUNFITS module. From http://lib.stat.cmu.edu/S/:

funfits

    FUNFITS is a comprehensive S-Plus module for fitting functions and
nonlinear time series, including multivariate splines, Kriging and
neural networks.  Contributed by Doug Nychka
([hidden email]). [25/Apr/96] [24/Mar/97][24/Sep/99] (3 kbytes). The
actual compressed tar file is available as funfits23.tar.gz in the S
collection. Access this file via FTP, or the WWW, but not
e-mail. (596k).  Older version avaulable at funfits.tar.Z

  A quick look at funfits.tar.Z suggests that 'splint' existed in that
version, in 1996 -- so respectably old.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

Hans W Borchers
In reply to this post by Martin Maechler
> but definitely *no* need to use a function from an extra CRAN
> package .. as someone else ``erronously'' suggested.

Except that Matlab's interp1() 'cubic' method does not use cubic spline
interpolation, but Moler's 'pchip' approach, a piecewise cubic Hermite
interpolation. Thus the results are different:

    % Matlab:
    interp1(X, Y, 11, 'cubic')              %=> 5.8000
    interp1(X, Y, 15, 'cubic')              %=> 5.0000

    # R:
    spfun <- splinefun( X, Y, "natural")
    spfun(11)                                    #=> 5.785714
    spfun(15)                                    #=> 4.928571

    spfun <- splinefun( X, Y, "monoH.FC")
    spfun(11)                                    #=> 5.8
    spfun(15)                                    #=> 5.0

Unfortunately, if the points are not monotonic, the 'monoH.FC' method does
not exactly agree with the 'pchip' approach, i.e. does not in general return
the same results.

By the way, pchip() in package 'pracma' implements Moler's approach and does
return the same (interpolation and extrapolation) results as interp1() with
the 'cubic' option in Matlab.

Hans Werner


Martin Maechler <maechler <at> stat.math.ethz.ch> writes:

>
> >>>>> David Winsemius <dwinsemius <at> comcast.net>
> >>>>>     on Sat, 1 Dec 2012 09:25:42 -0700 writes:
>
>     > On Dec 1, 2012, at 5:09 AM, Steve Stephenson wrote:
>
>     >> Hallo, I'm facing a problem and I would really appreciate
>     >> your support.  I have to translate some Matalb code in R
>     >> that I don't know very well but I would like to.  I have
>     >> to interpolate 5 point with a cubic spline function and
>     >> then I expect my function returns the Y value as output a
>     >> specific X value inside the evaluation range.  Let's
>     >> suppose that: 1- *X = [-10, -5, 0, 5, 10]* 2 - *Y = [12,
>     >> 10, 8, 7, 6]* 3 - *I have to interpolate with a cubic
>     >> spline assuming x=11*
>     >>
>     >> In Matlab I used this function:
>     >>
>     >> *y = interp1(X, Y, x, "cubic"); *
>     >>
>     >> How can I do the same in R?  Many thanks in advance for
>     >> your reply and support!
>
>     > splinefun( x = c(-10, -5, 0, 5, 10), y = c(12, 10, 8, 7, 6),
>                 method="natural")(11) [1] 5.785714
>
> Yes, indeed, or simple  spline(....)
>
> but definitely *no* need to use a function from an extra CRAN
> package .. as someone else ``erronously'' suggested.
>
> Note that
>   spline() and splinefun()
> together with
>   approx() and approxfun()
> are among the several hundred functions that were already
> part of "pre-alpha" R, i.e., before R had a version number
> or *any* packages ...
> and yes, the README then started with the two lines
>
> | R Source Code (Tue Jun 20 14:33:47 NZST 1995)
> | Copyright 1993, 1994, 1995 by Robert Gentleman and Ross Ihaka
>
> and it would be *really* *really* great
> if people did not add stuff to their packages that has
> been part of R for longer than they have even heard of R.
>
> Martin Maechler, ETH Zurich
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

Steve Stephenson
Dear all,
many thanks for your answer and support!!
Steve
Reply | Threaded
Open this post in threaded view
|

Re: cubic spline

Martin Maechler
In reply to this post by bbolker
>>>>> Ben Bolker <[hidden email]>
>>>>>     on Sat, 1 Dec 2012 21:49:47 +0000 writes:

    > Martin Maechler <maechler <at> stat.math.ethz.ch> writes:
    > [snip]

    >> but definitely *no* need to use a function from an extra
    >> CRAN package .. as someone else ``erronously'' suggested.
    >>
    >> Note that spline() and splinefun() together with approx()
    >> and approxfun() are among the several hundred functions
    >> that were already part of "pre-alpha" R, i.e., before R
    >> had a version number or *any* packages ...  and yes, the
    >> README then started with the two lines
    >>
    >> | R Source Code (Tue Jun 20 14:33:47 NZST 1995) |
    >> Copyright 1993, 1994, 1995 by Robert Gentleman and Ross
    >> Ihaka
    >>
    >> and it would be *really* *really* great if people did not
    >> add stuff to their packages that has been part of R for
    >> longer than they have even heard of R.
    >>
    >> Martin Maechler, ETH Zurich

    >   To be fair, the 'fields' package has a pretty long
    > history too -- I think it may have been ported from an
    > S-PLUS 'package' (or whatever the correct terminology is)
    > that existed quite a while ago.

    >  I think it was the FUNFITS module. From
    > http://lib.stat.cmu.edu/S/:

    > funfits

    >     FUNFITS is a comprehensive S-Plus module for fitting
    > functions and nonlinear time series, including
    > multivariate splines, Kriging and neural networks.
    > Contributed by Doug Nychka ([hidden email]). [25/Apr/96]
    > [24/Mar/97][24/Sep/99] (3 kbytes). The actual compressed
    > tar file is available as funfits23.tar.gz in the S
    > collection. Access this file via FTP, or the WWW, but not
    > e-mail. (596k).  Older version avaulable at funfits.tar.Z

    >   A quick look at funfits.tar.Z suggests that 'splint'
    > existed in that version, in 1996 -- so respectably old.

Good point, Ben, thank you!

and of course Hans Borcher's one is even more relevant to the
original question.

Martin

______________________________________________
[hidden email] 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.