| interpolate | R Documentation |
Interpolates unevenly spaced points into a relatively smooth curve. If the points are evenly spaced but there are missing values and/or aliasing should be avoided, use resample instead.
interpolate(
x,
y,
xout,
method = c("splineFC", "spline", "constant", "linear", "approxLowPass", "sgolay",
"pchip", "cosine", "cardinal", "hermite", "loess"),
plot = FALSE,
...
)
interpol_approxLowPass(bandwidth = 0.1)
interpol_sgolay(p = 3, n = 15)
interpol_cardinal(tension = 0.5)
interpol_loess(span = 0.5)
x, y |
numeric vectors giving the coordinates of the points to be interpolated (no NAs) |
xout |
numeric vector of target x‑coordinates where interpolation is to take place |
method |
interpolation method to use. Accepts either a character string
naming an inbuilt method (see "Interpolation methods" below) or a
constructor function such as |
plot |
logical; if TRUE, a quick diagnostic plot is drawn |
... |
extra arguments specific to the chosen interpolation method, e.g.
|
bandwidth |
(interpol_approxLowPass) the amount of smoothing, a number
between 0 and 1: close to 0 = more smoothing, close to 1 = less smoothing.
Defaults to |
p |
(interpol_sgolay) polynomial order for Savitzky‑Golay smoothing (positive integer, defaults to 3). |
n |
(interpol_sgolay) filter length for Savitzky‑Golay smoothing (odd
positive integer, defaults to |
tension |
(interpol_cardinal) a number between 0 and 1 controlling the
tightness of the cardinal spline: |
span |
(interpol_loess) the amount of LOESS smoothing, a number between
0 and 1: a larger |
A numeric vector of interpolated y‑values at the requested xout
locations.
Constant interpolation via approx. Fast,
but no smoothing.
Linear interpolation via approx.
Fast, but not smooth.
Cubic spline interpolation (FMM method) via
spline. Fast, but overshoots.
Monotone cubic interpolation using the Fritsch‑Carlson
method (see splinefun). Moderately fast, less
overshooting than the FMM spline.
Linear interpolation followed by low‑pass filtering.
Fast, smooth, but reduced range compared to original y. Constructor:
interpol_approxLowPass(bandwidth).
Linear interpolation followed by Savitzky‑Golay smoothing
(see sgolayfilt). Fairly similar to approxLowPass,
but much slower. Constructor: interpol_sgolay(p, n).
Piecewise Cubic Hermite Interpolating Polynomial (preserves
monotonicity). Calls interp1 with method = "pchip".
Cosine‑eased (smoothstep) interpolation. Eases between anchor points using a cosine curve. Fast, passes exactly through input points.
Cardinal spline interpolation (a generalization of
Catmull‑Rom). Fast, can be forced to pass exactly through input points.
Constructor: interpol_cardinal(tension).
Hermite spline interpolation with forced zero slope at local extrema to prevent overshoot. Fast, passes exactly through input points.
Locally estimated scatterplot smoothing (LOESS) via
loess. Smooth, but slow; may overshoot. Constructor:
interpol_loess(span).
interpolateNA resample
x = c(0, .15, .2, .3, .7, 1)
y = c(360, 116, 550, 350, 700, 610)
xout = seq(0, 1, length.out = 100)
# Compare inbuilt interpolation methods
ms = c('constant', 'linear', 'spline', 'splineFC', 'approxLowPass',
'sgolay', 'cosine', 'cardinal', 'hermite', 'loess')
op = par(c('mfrow', 'mar')); par(mfrow = c(4, 3), mar = c(2, 2, 3, 1))
for (m in ms) {interpolate(x, y, xout, method = m, plot = TRUE); title(m)}
par(op)
# Passing method‑specific parameters via ...
interpolate(x, y, xout, method = 'cardinal', tension = 0, plot = TRUE)
interpolate(x, y, xout, method = 'loess', span = 0.2, plot = TRUE)
interpolate(x, y, xout, method = 'loess', span = 0.9, plot = TRUE)
# Equivalent: passing a constructor (useful when forwarding through
# higher‑level functions like resample())
interpolate(x, y, xout, method = interpol_cardinal(tension = 0), plot = TRUE)
interpolate(x, y, xout, method = interpol_loess(span = 0.2), plot = TRUE)
# Passing a fully custom function
interpolate(x, y, xout, plot = TRUE,
method = function(...) spline(x, y, xout = xout, method = 'natural')$y)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.