问题描述
我打算将钳制的b样条曲线拟合到R中的一组控制点,但是难以理解bs中结参数的使用。给定一组控制点:
I intend to fit a clamped b-spline to a set of control points in R, but have trouble understanding the use of the knots parameter in bs. Given a set of control points:
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) )
我独立地拟合x和y沿路径的距离:
I fit x and y independently against the distance along the path:
path$distance <- 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
,但我想提供一个开放的统一结向量来锚定适合第一个和最后一个点-使用 df 不支持此功能。
but I want to provide an open uniform knot vector in order to anchor the fit to the first and last point - using df does not support this.
据我了解,对于我给定一组点,样条曲线的度数为3,则必须有(12-1)+ 3 + 2 = 16 节(每m = n + p + 1,对于#knots = m + 1,#control = n + 1,度= p),因此对于受限制的样条曲线,这应该是一个不错的结向量:
As I understand it, for my given set of points, and a degree of 3 for the spline, there must be (12-1)+3+2 = 16 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:
knots <- seq(path$distance[1], path$distance[12], length.out = 10) knots <- 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
尽管如此,使用它会产生一些疯狂的数字,以及关于等级不足的警告,因此很明显我必须以某种方式弄错了:
using this gives some crazy numbers though, as well as a warning about rank-deficiency, so clearly I must have it wrong somehow:
pred_df <- data.frame(x=0,y=0,distance=seq(min(path$distance), max(path$distance), length.out=100)) xPath <- 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
指定的正确方法是什么
推荐答案
根据您的参数和结数据判断,的确违反了Schoenberg-Whitney条件。我对R中的语法不熟悉,但似乎您正在path $ distance [1]和path $ distance [12]之间统一生成结值。这个不对。您应该通过以下方式生成结值:
Judging from your 'parameter' and 'knots' data, indeed they violate the Schoenberg-Whitney condition. I am not familiar with the syntax in R, but it seems that you are generating the knot values uniformly between path$distance[1] and path$distance[12]. This is not right. You should generate your knot values in the following way:
0)将参数表示为p [i],其中i = 0至(n-1),p [ 0] = 0.0,n是点数。对于您的情况,n = 12
1)创建结值
0) Denote the parameters as p[i], where i = 0 to (n-1), p[0]=0.0 and n is number of points. For your case, n=12
1) Create the knot values as
knot [0] =(p [1] + p [2 ] + p [3])/ 3
结[1] =(p [2] + p [3] + p [4])/ 3
结[2] =(p [3] + p [4] + p [5])/ 3
......
这些是内部结点值。您应注意,在此步骤中将不使用p [0]和p [n-1]。您应该为您的案例获得8个内部结点值。
2)现在,将p [0]添加到结点值的前面4次(对于度= 3)并添加p [n-1]到结点值的末尾4次,您就完成了。对于您的情况,您应该总共获得16个结点值。
knot[0] = (p[1]+p[2]+p[3])/3
knot[1] = (p[2]+p[3]+p[4])/3
knot[2] = (p[3]+p[4]+p[5])/3
......
These are the interior knot values. You should notice that p[0] and p[n-1] will not be used in this step. You should obtain 8 interior knot values for your case.
2) Now, add p[0] to the front of the knot values 4 times (for degree=3) and add p[n-1] to the end of the knot values 4 times and you are done. For your case, you should get 16 knot values in total.
请注意,这不是生成有效结点矢量的 only 方法。但是以这种方式生成的结向量将始终满足Shoenberg-Whitney条件。
Please note that this is not the only way to generate a valid knot vector. But knot vector generated in this way will always satisfy the Shoenberg-Whitney condition.
这篇关于在bs-call中指定钳位结向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!