Cubic Spline Approximation

DESCRIPTION:

Interpolates/extrapolates through data points using a cubic spline.

USAGE:

spline(x, y, n = 3*length(x), periodic = F, boundary = 0, 
       xmin = min(x), xmax = max(x), newx = NULL,
       derivative = 0, period = NULL, 
       constraint = "ratio2", extrapolationDegree = <<see below>>) 

REQUIRED ARGUMENTS:

x,y
coordinates of points. The coordinates can be given by two vector arguments or by a single arguments x which is a times series, a two-column matrix, or a list containing components named x and y. The values of x do not have to be ordered, but should be distinct (though the function eliminates redundant points, with the same x and y values). Lengths of x and y must be the same and be at least 4 after eliminating redundant values. Missing values are not accepted in x or y.

OPTIONAL ARGUMENTS:

n
integer, approximate number of output points. Ignored if newx is supplied. The actual number of output points may be different than n (see VALUE below).
periodic
logical, is the function periodic? If TRUE, by default the period is the range of x values, the y values at min(x) and max(x) must agree, and the output has derivatives matched at these end points. Ignored (assumed TRUE) if period is supplied.
boundary
vector of length 1 or 2, constants used in boundary value computations; see the constraint argument.
xmin, xmax
range of output x values. These are ignored if newx is supplied.
newx
output values of x. Does not have to be ordered and may contain missing values. If not provided, then output x values are determined by x, n, xmin and xmax (see VALUE below).
derivative
integer, one of 0, 1, 2, or 3. If non-zero, return the corresponding derivative of the spline curve.
period
If supplied, x values which differ by period are equivalent and the corresponding y values must be equal. The spline constructed is periodic with this period.
constraint
character vector of length 1 or 2, specifying the type of boundary condition for non-periodic splines. Both boundary and constraint are ignored if periodic=TRUE or period is supplied. If either is of length 2 the second value is used for the right boundary condition. Current options are:
"ratio2": the boundary constraint gives the ratio between the second derivative at the end point and the adjacent point. With boundary=0 this gives natural splines, which have second derivative equal to zero at the endpoints.
"ratio1": the boundary constraint gives the ratio between the first derivative at the end point and the adjacent point.
"fixed1": the boundary constraint gives the value of the first derivative at the end point.
"fixed2": the boundary constraint gives the value of the second derivative at the end point. With boundary=0 this gives natural splines.
"linear2": the second derivative at the endpoint and two adjacent points changes linearly, corresponding to a constant third derivative over the two intervals nearest the end. This option matches cubic polynomials exactly.
extrapolationDegree
integer, either 2 or 3, indicating the degree of polynomials for extrapolation. If 2, quadratic extrapolation is used; this gives linear extrapolation for natural splines. If 3, then extrapolation is cubic; this is the extension of the interpolating curve inside the boundary point. The default is 3 if constraint="linear2", 2 otherwise.

VALUE:

list containing components x and y, the coordinates of the (derivative of the) spline curve. If newx is provided it is the x component of the output. The ordering of newx is maintained in x, as are any missing values. y is set to NA for any corresponding NA in x. If newx is not provided, then x contains all values of input x between xmin and xmax, plus additional values equally spaced between each pair of neighboring input x values. In this case, approximately n values are returned, though it may be as few as n/2 or as many as n+length(input x).

BACKGROUND:

Splines approximate a function with a set of polynomials defined on subintervals. A cubic spline is a collection of polynomials of degree less than or equal to 3 such that the second derivatives agree at the "knots" (the input points), i.e., the spline has a continuous second derivative. When interpolating a number of points, a spline can be a much better solution than a polynomial interpolant, since the polynomial can oscillate wildly in order to hit all of the points (polynomials fit the data globally while splines fit the data locally).

REFERENCES:

Stoer, J. and Bulirsch, R. (1992). Introduction to Numerical Analysis, 2nd ed. Springer-Verlag, New York.

Blum, E. K. (1972). Numerical Analysis and Computation Theory and Practice. Addison-Wesley, Reading, Mass.

Hamming, R. W. (1973). Numerical Methods for Scientists and Engineers. 2nd ed. McGraw-Hill, New York.

SEE ALSO:

, , , , , .

EXAMPLES:

# Periodic example 
x <- 0:20 
y <- sin(x) 
plot(x,y) 
lines(spline(x,y))       # Non-periodic 
lines(spline(x,y,periodic=T, period=2*pi)) 
 
# Reproduce a cubic curve 
xx <- seq(0,1,length=41) 
plot(xx, xx^3, type="l") # true curve 
x <- ppoints(5) 
y <- x^3                  # 5 data points 
points(x, y) 
lines(spline(x, y, newx=xx), col=2) # natural spline 
lines(spline(x, y, newx=xx, constraint="linear2"), col=3)