Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic two-dimensional cubic spline fitting in R

Tags:

r

matlab

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...

like image 750
Christian Avatar asked Dec 27 '22 08:12

Christian


1 Answers

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.

enter image description here

like image 111
kohske Avatar answered Jan 31 '23 13:01

kohske