Rev 41508 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{splinefun}\alias{spline}\alias{splinefun}\title{Interpolating Splines}\description{Perform cubic spline interpolation of given data points, returningeither a list of points obtained by the interpolation or a functionperforming the interpolation.}\usage{splinefun(x, y = NULL, method = "fmm", ties = mean)spline(x, y = NULL, n = 3*length(x), method = "fmm",xmin = min(x), xmax = max(x), ties = mean)}\arguments{\item{x,y}{vectors giving the coordinates of the points to beinterpolated. Alternatively a single plotting structure can bespecified: see \code{\link{xy.coords}.}}\item{method}{specifies the type of spline to be used. Possiblevalues are \code{"fmm"}, \code{"natural"} and \code{"periodic"}.}\item{n}{interpolation takes place at \code{n} equally spaced pointsspanning the interval [\code{xmin}, \code{xmax}].}\item{xmin}{left-hand endpoint of the interpolation interval.}\item{xmax}{right-hand endpoint of the interpolation interval.}\item{ties}{Handling of tied \code{x} values. Either a functionwith a single vector argument returning a single number result orthe string \code{"ordered"}.}}\details{The inputs can contain missing values which are deleted, so at leastone complete \code{(x, y)} pair is required.If \code{method = "fmm"}, the spline used is that of Forsythe, Malcolmand Moler (an exact cubic is fitted through the four points at eachend of the data, and this is used to determine the end conditions).Natural splines are used when \code{method = "natural"}, and periodicsplines when \code{method = "periodic"}.These interpolation splines can also be used for extrapolation, that isprediction at points outside the range of \code{x}. Extrapolationmakes little sense for \code{method = "fmm"}; for natural splines itis linear using the slope of the interpolating curve at the nearestdata point.}\value{\code{spline} returns a list containing components \code{x} and\code{y} which give the ordinates where interpolation took place andthe interpolated values.\code{splinefun} returns a function with formal arguments \code{x} and\code{deriv}, the latter defaulting to zero. This functioncan be used to evaluate the interpolating cubic spline(\code{deriv}=0), or its derivatives (\code{deriv}=1,2,3) at thepoints \code{x}, where the spline function interpolates the datapoints originally specified. This is often more useful than\code{spline}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.Forsythe, G. E., Malcolm, M. A. and Moler, C. B. (1977)\emph{Computer Methods for Mathematical Computations}.}\seealso{\code{\link{approx}} and \code{\link{approxfun}} for constant andlinear interpolation.Package \pkg{splines}, especially \code{\link[splines]{interpSpline}}and \code{\link[splines]{periodicSpline}} for interpolation splines.That package also generates spline bases that can be used forregression splines.\code{\link{smooth.spline}} for smoothing splines.}\examples{require(graphics)op <- par(mfrow = c(2,1), mgp = c(2,.8,0), mar = .1+c(3,3,3,1))n <- 9x <- 1:ny <- 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)^2plot(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)## An example with ties (non-unique x values):set.seed(1); x <- round(rnorm(30), 1); y <- sin(pi * x) + rnorm(30)/10plot(x,y, main="spline(x,y) when x has ties")lines(spline(x,y, n= 201), col = 2)## visualizes the non-unique ones:tx <- table(x); mx <- as.numeric(names(tx[tx > 1]))ry <- matrix(unlist(tapply(y, match(x,mx), range, simplify=FALSE)),ncol=2, byrow=TRUE)segments(mx, ry[,1], mx, ry[,2], col = "blue", lwd = 2)}\keyword{math}\keyword{dplot}