What is the R equivalent of the simple Matlab cubic spline interpolation in 2D space shown here, i.e.
n = 7;
x = rand(n,1);
y = rand(n,1);
plot(x,y,'.')
axis([0 1 0 1])
t = 1:n;
ts = 1:1/10:n;
xs = spline(t,x,ts);
ys = spline(t,y,ts);
hold on
plot(xs,ys,'r');
hold off
I've tried variations in R but they seem to require ordering of the x vector, and digging through the related questions didn't get me any further. Thanks...
Probably this is R version:
n <- 7
x <- runif(n)
y <- runif(n)
t <- 1:n
ts <- seq(1, n, by = 1/10)
xs <- splinefun(t, x)(ts)
ys <- splinefun(t, y)(ts)
plot(x, y, xlim = c(0, 1), ylim = c(0, 1))
lines(xs, ys)
Note that I'm not sure if the algorithms of spline is completely identical with matlab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With