我正在尝试在r中复制Matlabs csaps函数。我已经通过this问题成功实现了这一目标。
但是,在两种情况下,当我尝试更改权重时都会出现问题。在MATLAB中,权重是基于误差度量的,但是我不确定r的pspline函数中的w值对应什么,这会给我带来不同的结果
https://uk.mathworks.com/help/curvefit/csaps.html
这是一些示例代码: ----------------在MATLAB中------------------
% Run spline with weights = 1
Axis = 1:100;
Yval = sin((1:100)/20 *(2*pi));
weights = ones(size(Yval));
p = 0.0005;
pp = csaps(Axis, Yval, p, [], weights);
smoothed = fnval(pp, Axis)';
% Run spline with some select weights = 0
weights([5,8,10,15,17,19,24,...
26,28,33,36,40,44,46,...
49,50,55,60,64,68,74,...
79,81,85,88,93,99,100])=0
pp2 = csaps(Axis, Yval, p, [], weights);
smoothed2 = fnval(pp2, Axis)';
plot(Axis,Yval)
hold on
plot(Axis,smoothed)
plot(Axis,smoothed2)
---------------- IN R ------------------
% Run spline with weights = 1
Axis <- 1:100
Yval <- sin((1:100)/20 *(2*pi))
weights <- rep(1,length(Yval))
p <- 0.0005
pp <- pspline::smooth.Pspline(
x = Axis, y =Yval, w = weights, spar = (1-p) / p )
smoothed <- c(predict(pp,Axis))
% Run spline with some select weights = 0
weights[c(5,8,10,15,17,19,24,
26,28,33,36,40,44,46,
49,50,55,60,64,68,74,
79,81,85,88,93,99,100)] <- 0.00000000000000001
pp2 <- pspline::smooth.Pspline(
x = Axis, y =Yval, w = weights, spar = (1-p) / p )
smoothed2 <- c(predict(pp2,Axis))
plot(Yval,t='l')
lines(smoothed,col = 2)
lines(smoothed2,col = 3)
R和Matlab中的结果是相同的(在四舍五入之内),没有加权,但是引入加权后,结果现在有所不同。两者都在后台运行FORTRAN样条函数。我无法弄清楚如何在R中运行pspline以获得这些权重相同的结果。
谢谢!