interp1.RdOne-dimensional interpolation of points.
interp1(x, y, xi = x,
method = c("linear", "constant", "nearest", "spline", "cubic"))Numeric vector; points on the x-axis; at least two points require; will be sorted if necessary.
Numeric vector; values of the assumed underlying function;
x and y must be of the same length.
Numeric vector; points at which to compute the interpolation;
all points must lie between min(x) and max(x).
One of “constant", “linear", “nearest", “spline", or “cubic"; default is “linear"
Interpolation to find yi, the values of the underlying function
at the points in the vector xi.
Methods can be:
linear | linear interpolation (default) |
constant | constant between points |
nearest | nearest neighbor interpolation |
spline | cubic spline interpolation |
cubic | cubic Hermite interpolation |
Numeric vector representing values at points xi.
Method `spline' uses the spline approach by Moler et al., and is identical with the Matlab option of the same name, but slightly different from R's spline function.
The Matlab option “cubic” seems to have no direct correspondence in R.
Therefore, we simply use pchip here.
x <- c(0.8, 0.3, 0.1, 0.6, 0.9, 0.5, 0.2, 0.0, 0.7, 1.0, 0.4)
y <- x^2
xi <- seq(0, 1, len = 81)
yl <- interp1(x, y, xi, method = "linear")
#> Warning: Points in argument in 'x' unsorted; will be sorted.
yn <- interp1(x, y, xi, method = "nearest")
#> Warning: Points in argument in 'x' unsorted; will be sorted.
ys <- interp1(x, y, xi, method = "spline")
#> Warning: Points in argument in 'x' unsorted; will be sorted.
if (FALSE) { # \dontrun{
plot(x, y); grid()
lines(xi, yl, col="blue", lwd = 2)
lines(xi, yn, col="black", lty = 2)
lines(xi, ys, col="red")
} # }
## Difference between spline (Matlab) and spline (R).
x <- 1:6
y <- c(16, 18, 21, 17, 15, 12)
xs <- linspace(1, 6, 51)
ys <- interp1(x, y, xs, method = "spline")
sp <- spline(x, y, n = 51, method = "fmm")
if (FALSE) { # \dontrun{
plot(x, y, main = "Matlab and R splines")
grid()
lines(xs, ys, col = "red")
lines(sp$x, sp$y, col = "blue")
legend(4, 20, c("Matlab spline", "R spline"),
col = c("red", "blue"), lty = 1)
} # }