[R] cubic spline

Martin Maechler maechler at stat.math.ethz.ch
Sat Dec 1 20:53:52 CET 2012


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




More information about the R-help mailing list