我在Matlab中寻找(一个理想的内置)函数,它以与R中相同的方式计算B样条基矩阵,例如对于具有20个等间距3度结的样条基础,我会在R中做
require(splines)
B = bs(x = seq(0,1,length.out=100),
knots = seq(0, 1, length.out=20), # 20 knots
degree = 3,
intercept = FALSE)
matplot(B,type="l")
为了在Matlab中得到相同的结果,我想我可以使用它
B = spcol(linspace(0,1,20),3,linspace(0,1,100));
plot(B);
但是可以看出边界结然后丢失了.
有任何想法,Matlab中的等效语法与R中的相同吗?
PS R用于bs()的代码有点简化:
basis
if(degree == 0){
B = knots[i]) & (x < knots[i+1]), 1, 0)
} else {
if((knots[degree+i] - knots[i]) == 0) {
alpha1
} else {
alpha1
}
if((knots[i+degree+1] - knots[i+1]) == 0) {
alpha2
} else {
alpha2
}
B
}
return(B)
}
bs
if(missing(x)) stop("You must provide x")
if(degree < 1) stop("The spline degree must be at least 1")
Boundary.knots
interior.knots.sorted
if(!is.null(interior.knots)) interior.knots.sorted
knots
K
B.mat
for(j in 1:K) B.mat[,j]
if(any(x == Boundary.knots[2])) B.mat[x == Boundary.knots[2], K]
if(intercept == FALSE) {
return(B.mat[,-1])
} else {
return(B.mat)
}
}