splinefun
Interpolating Splines

Description

Interpolates or extrapolates through data points using a cubic spline.

Usage

spline(x, y = NULL, n = 3 * length(x), method = "fmm", xmin = min(x), xmax = max(x), xout, ties = mean)
splinefun(x, y = NULL, method = c("fmm", "periodic", "natural", "monoH.FC", "hyman"), ties = mean)
splinefunH(x, y, m)

Arguments

x, y vectors specifying the coordinates of points.
  • For spline and splinefun the x and y arguments are handled by the function xy.coords, so coordinates can be given by one of the following:
    • two vector arguments (of equal length).
    • a single argument x (which is a single plotting structure like times series).
    • a two-column matrix.
    • a list containing components named x and y.
    • a formula with the y variable on the left side of the tilde and the x variable on the right.
    Missing values (NAs) in x or y are removed before interpolating. Points with identical x values are combined. (See the ties argument.)
  • For splinefunH, the x and y arguments must be vectors,
n an integer. Specifies the number of output points, equally spaced in the interval[xmin, xmax]. Ignored if xout is supplied.
method a character string. Gives the method to interpolate. Must be one of the following:
  • "fmm"
  • "periodic"
  • "natural"
  • "monoH.FC"
  • "hyman"
Abbreviations are allowed. See Details for more information.
xmin, xmax The range of the default value of the xout argument.
xout The x values at which to evaluate the interpolating spline. Its default value is seq(xmin, xmax, length=n). If none of these are provided, the spline is evaluated at 3*length(x) equally spaced values covering the range of x.
ties a function or the name of a function specifying how to handle duplicate x values.
  • Vectors of y values corresponding to the same x value are passed to the function, which should return a single number.
  • The ties function can be passed a one-long vector of values, in which case it should return its input.
  • The special value "ordered" specifies that x is already a vector of strictly-increasing values.

    Note This function performs no check and does not correct problems if x does not meet this requirement. If you specify "ordered", and x does not meet its required standard, you will encounter unexpected and unwanted results.

m a vector of slopes corresponding to the point (x, y). Used only for splinefunH.

Details

The following rules apply for the methods called to interpolate:
Method Description
"fmm" The spline used is that of Forsythe, Malcolm, and Moler.
"periodic" Periodic splines are used. The period is taken to be diff(range(x)), and it is expected that the y values corresponding to the extrema of x are equal.
"natural" Natural splines are used. The second derivative is zero at the endpoints.
"monoH.FC" The monotone piecewise cubic interpolation method of F.N. Fritsch and R.E. Carlson (with modifications by Butland and Brodlie) is used. (This is the Fortran routine DPCHIP from the slatec library.)
"hyman" Also gives a monotone spline: the "fmm" spline is initially calculated, and then the method of J.M. Hyman is used to force it to be monotone.
Value
splinereturns a list containing components x and y, the coordinates of the interpolated spline curve.
splinefunreturns a function with arguments x and deriv(with the default value zero). This function can be used to evaluate the interpolating cubic spline (deriv=0), or its derivatives (deriv=1,2,3) at the points x.
splinefunH, splinefun(method="monoH.FC")return a function similar to that returned by splinefun, but it includes an extra argument, extrapol, giving you options for how the function behaves outside of the range of the x used to create the spline.

extrapol="linear", the default, specifies linear extrapolation; extrapol="cubic" specifies cubic extrapolation. (Cubic extrapolation usually gives very poor results.)

Note:
All are piecewise cubic functions.
References
Blum, E. K. 1972. Numerical Analysis and Computation Theory and Practice. Reading, MA: Addison-Wesley Pub.
Forsythe, G. E., Malcolm, M. A., and Moler, C. B. 1977. Computer Methods for Mathematical Computations. Englewood Cliffs, NJ: Prentice-Hall.
Fritsch, F. N. and Carlson, R. E. 1980. Monotone piecewise cubic interpolation. SIAM Journal on Numerical Analysis. Volume 17. 238-246.
Hamming, R. W. 1973. Numerical Methods for Scientists and Engineers. Second Edition. New York, NY: McGraw-Hill.
Hyman, J. M. 1983. Accurate monotonicisty preserviing cubic interpolation. SIAM Journal on Scientific and Statistical Computing. Volume 4. 645-654.
Stoer, J. and Bulirsch, R. 1992. Introduction to Numerical Analysis. Second Edition. New York, NY: Springer.
See Also
approx, smooth.spline.
Examples
spline(1:10, sin(1:10))
f <- splinefun(1:10, sin(1:10))
f(seq(1, 10, len=30)) # same result as y component in first call to spline

h <- splinefun(log2(1:20), sort(sin(1:20)), method="hyman") h(seq(0, 4, by=1/8), deriv=1) # all slopes positive

p <- splinefun(log2(1:16), sin(c(1:15,1)), method="periodic") p(seq(0, 5, by=1/4)) # period=4

# define a piecewise cubic in Hermite format that approximates sin(2*x) on [11,14] hm <- splinefunH(x=11:14, y=sin(2*(11:14)), m=2*cos(2*(11:14))) hm(seq(10,15,by=1/2), extrapol="linear")

Package stats version 6.1.4-13
Package Index