x=seq(0, 1, length.out=11)
y=sin(pi*x)+rnorm(x, sd=0.1)
# constraint B-spline to be 0 at the interval ends
fsm=smbsp(x, y, nki=1, lieq=list(rbind(c(0, 0), c(1, 0))))
# check parameters of found B-splines
bsppar(fsm)
plot(x, y) # original "measurements"
# fine grained x
xfine=seq(0, 1, length.out=101)
lines(xfine, fsm(xfine)) # fitted B-splines
lines(xfine, sin(pi*xfine), col="blue") # original function
# visualize knot positions
xk=bsppar(fsm)$xk
points(xk, fsm(xk), pch="x", col="red")
# fit broken line with linear B-splines
x1=seq(0, 1, length.out=11)
x2=seq(1, 3, length.out=21)
x3=seq(3, 4, length.out=11)
y1=x1+rnorm(x1, sd=0.1)
y2=-2+3*x2+rnorm(x2, sd=0.1)
y3=4+x3+rnorm(x3, sd=0.1)
x=c(x1, x2, x3)
y=c(y1, y2, y3)
plot(x, y)
f=fitsmbsp(x, y, n=1, nki=2)
lines(x, f(x))
Run the code above in your browser using DataLab