title: Specify clamped knot vector in bs-call
body: <p>I intend to fit a clamped b-spline to a set of control points in R, but have trouble understanding the use of the <code>knots</code> parameter in bs. Given a set of control points:</p>
  
  
  
  <pre><code>path &lt;- 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)
  )
  </code></pre>
  
  <p>I fit x and y independently against the distance along the path:</p>
  
  <pre><code>path$distance &lt;- c(0, cumsum(sqrt(diff(path[,1])^2 + diff(path[,2])^2)))
  path$distance
  ##  [1]  0.000000  1.118034  4.344511  9.382259 13.566026 27.766169 41.860284 48.799899 52.877545 56.535931 57.950145
  ## [12] 59.068179
  </code></pre>
  
  <p>but I want to provide an open uniform knot vector in order to anchor the fit to the first and last point - using <code>df</code> does not support this.</p>
  
  <p>As I understand it, for my given set of points, and a degree of 3 for the spline, there must be <code>(12-1)+3+2 = 16</code> knots (per m=n+p+1, for #knots=m+1, #control=n+1, degree=p), so for a clamped spline, this should be a nice knot vector:</p>
  
  <pre><code>knots &lt;- seq(path$distance[1], path$distance[12], length.out = 10)
  knots &lt;- c(rep(knots[1], 3), knots, rep(knots[10], 3))
  knots
  ##  [1]  0.000000  0.000000  0.000000  0.000000  6.563131 13.126262 19.689393 26.252524 32.815655
  ## [10] 39.378786 45.941917 52.505048 59.068179 59.068179 59.068179 59.068179
  </code></pre>
  
  <p>using this gives some crazy numbers though, as well as a warning about rank-deficiency, so clearly I must have it wrong somehow:</p>
  
  <pre><code>pred_df  &lt;-  data.frame(x=0,y=0,distance=seq(min(path$distance), max(path$distance), length.out=100))
  xPath &lt;- predict(lm(x~bs(distance, knots=knots, degree = 3), path), pred_df)
  ## Warning message:
  ## In predict.lm(lm(x ~ bs(distance, knots = knots, degree = degree),  :
  ##   prediction from a rank-deficient fit may be misleading
  summary(xPath)
  ##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
  ## -2133.000     3.468    16.700  -161.900    64.590   857.800 
  </code></pre>
  
  <p>What is the correct way to specify the knot-vector given a set of control points and a degree?</p>