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>>)
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
.
newx
is
supplied. The actual number of output
points may be different than
n
(see VALUE below).
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.
constraint
argument.
x
values.
These are ignored if
newx
is supplied.
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).
x
values which differ by
period
are equivalent
and the corresponding
y
values must be equal.
The spline constructed is periodic with this period.
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.
constraint="linear2"
, 2 otherwise.
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)
.
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).
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.
# 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)