I've tried to approach my general problem through two separate questions here on SO: Specify clamped knot vector in bs-call and Fit a B spline to a control path. The responses to these have led me to a reformulation of the bigger problem, which I'm now posting.
What I'm trying to accomplish is (in R), given a set of (non-monotonous) control points, how do I draw a b-spline along the path given by the control points. The b-spline must be clamped in both ends. This is purely for graphical purpose.
NOTE: I'm not trying to predict values based on the control points. Using a combination of predict
, lm
and bs
as proposed in Fit a B spline to a control path results in a b spline that tries to pass through the control points (which is not the point) - especially if setting df
or knots
so that the spline is clamped, this approach leads to overfitting and the spline will pass through every control point.
In the end the approach will be applied to 100s or 1000s of separate sets of control points so algorithmic efficiency is a factor, though currently I'm just looking for a way to get it done.
toy data (same as in the linked questions):
path <- data.frame(
x = c(3, 3.5, 4.6875, 9.625, 5.5625, 19.62109375, 33.6796875, 40.546875, 36.59375, 34.5, 33.5, 33),
y = c(0, 1, 4, 5, 6, 8, 7, 6, 5, 2, 1, 0)
)
All of this is for the purpose of calculating Hierarchical Edge Bundles in R.
EDIT:
Okay, so it seems that I'm unable to access the underlying b-spline implementation that is used under the hood (Or maybe I didn't try hard enough). Anyway I ended up crafting my own Rcpp based implementation inspired by Jason Yu-Tseh Chi's de Boor algorithm. The upside of this was that I could make it vectorised so multiple paths could be constructed in one go. This will be part of an upcoming CRAN package (hierarchicalSets), but I'll be happy to share it with anyone expressing interest in the meantime (too long to post here)...
UPDATE:
For anyone interested in this, spline drawing will be implemented as a geom/stat extension to ggplot2 in the ggforce extension package