Perform cubic spline interpolation of given data points, returning either a list of points obtained by the interpolation or a function performing the interpolation.
interpolated. Alternatively a single plotting structure can be specified: see
spanning the interval [
|left-hand endpoint of the interpolation interval.|
|right-hand endpoint of the interpolation interval.|
The inputs can contain missing values which are deleted, so at least one complete
(x, y) pair is required. If
method = “fmm”, the spline used is that of Forsythe, Malcolm and Moler (an exact cubic is fitted through the four points at each end of the data, and this is used to determine the end conditions). Natural splines are used when
method = “natural”, and periodic splines when
method = “periodic”.
These interpolation splines can also be used for extrapolation, that is prediction at points outside the range of
x. Extrapolation makes little sense for
method = “fmm”; for natural splines it is linear using the slope of the interpolating curve at the nearest data point.
spline returns a list containing components
y which give the ordinates where interpolation took place and the interpolated values.
splinefun returns a function with formal arguments
deriv, the latter defaulting to 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, where the spline function interpolates the data points originally specified. This is often more useful than
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth \& Brooks/Cole.
Forsythe, G. E., Malcolm, M. A. and Moler, C. B. (1977) Computer Methods for Mathematical Computations.
smooth.spline for smoothing splines.
op <- par(mfrow = c(2,1), mgp = c(2,.8,0), mar = .1+c(3,3,3,1)) n <- 9 x <- 1:n y <- rnorm(n) plot(x, y, main = paste("spline[fun](.) through", n, "points")) lines(spline(x, y)) lines(spline(x, y, n = 201), col = 2) y <- (x-6)^2 plot(x, y, main = "spline(.) -- 3 methods") lines(spline(x, y, n = 201), col = 2) lines(spline(x, y, n = 201, method = "natural"), col = 3) lines(spline(x, y, n = 201, method = "periodic"), col = 4) legend(6,25, c("fmm","natural","periodic"), col=2:4, lty=1) y <- sin((x-0.5)*pi) f <- splinefun(x, y) ls(envir = environment(f)) splinecoef <- get("z", envir = environment(f)) curve(f(x), 1, 10, col = "green", lwd = 1.5) points(splinecoef, col = "purple", cex = 2) curve(f(x, deriv=1), 1, 10, col = 2, lwd = 1.5) curve(f(x, deriv=2), 1, 10, col = 2, lwd = 1.5, n = 401) curve(f(x, deriv=3), 1, 10, col = 2, lwd = 1.5, n = 401) par(op)