什么是matlab的R等价物';s csaps()
matlab中的什么是matlab的R等价物';s csaps(),r,matlab,splines,R,Matlab,Splines,matlab中的csaps()根据平滑参数p的特定定义进行三次样条。以下是一些matlab代码及其结果: % x variable age = 75:99 % y variable diffs = [-39 -2 -167 -21 -13 32 -37 -132 -143 -91 -93 -88 -62 -112 -95 -28 -90 -40 -27 -23 -28 -11 -8 -6 1]
csaps()
根据平滑参数p
的特定定义进行三次样条。以下是一些matlab代码及其结果:
% x variable
age = 75:99
% y variable
diffs = [-39 -2 -167 -21 -13 32 -37 -132 -143 -91 -93 -88 -62 -112 -95 -28 -90 -40 -27 -23 -28 -11 -8 -6 1]
% 0.0005 is the parameter p, and the later specification of
% age are the desired x for prediction
csaps(age,diffs,0.0005,age)
% result (column headers removed):
-63.4604 -64.0474 -64.6171 -65.1397 -65.6111 -66.0165 -66.3114
-66.4123 -66.2229 -65.6726 -64.7244 -63.3582 -61.5676 -59.3568
-56.7364 -53.7382 -50.4086 -46.7922 -42.9439 -38.9183 -34.7629
-30.5180 -26.2186 -21.8912 -17.5532
我希望在R中得到相同的结果。我尝试了base::smooth.spline()
,但平滑参数spar
的指定方式与matlab的p
不同(可以吗?)。我能得到的最接近的结果是Pspline
包的smooth.Pspline()
函数。下面是一些代码,可以让事情在R中顺利进行:
age <- 75:99
diffs <- c(-39L, -2L, -167L, -21L, -13L, 32L, -37L, -132L, -143L, -91L,
-93L, -88L, -62L, -112L, -95L, -28L, -90L, -40L, -27L, -23L,
-28L, -11L, -8L, -6L, 1L)
predict(pspline::smooth.Pspline(
x = age,
y = diffs,
norder = 2,
method = 1,
spar = 1 / 0.0005 # p given in MP and matlab as 0.0005
),age)
# which gives something close, but not exactly the same:
[1] -63.46487 -64.05103 -64.61978 -65.14158 -65.61214 -66.01662 -66.31079
[8] -66.41092 -66.22081 -65.67009 -64.72153 -63.35514 -61.56447 -59.35372
[15] -56.73367 -53.73584 -50.40680 -46.79098 -42.94333 -38.91850 -34.76393
[22] -30.51985 -26.22131 -21.89474 -17.55757
age以下是我在p。大卫·希贝勒的第16页。[但是,我不使用Matlab]
将自然三次样条曲线(S′(x)=0在两个端点)拟合到坐标在向量x和y中的点(xi,yi);在x坐标在矢量xx中的点处求值,存储
yy中对应的y
Matlab:
pp=csape(x,y,’variational’);
yy=ppval(pp,xx) but note that
csape is in Matlab’s Spline
Toolbox
R
tmp=spline(x,y,method=’natural’,
xout=xx); yy=tmp$y
我的同事找到了答案:将matlab的p
转换为pspline::smooth.pspline()
的spar
不是作为1/p
,而是作为(1-p)/p
,然后无论数值精度如何,结果都会一致:
c(predict(pspline::smooth.Pspline(
x = age,
y = diffs,
norder = 2,
method = 1,
spar = (1-0.0005) / 0.0005 # p given in MP and matlab as
),age))
[1] -63.46035 -64.04741 -64.61705 -65.13972 -65.61114 -66.01646 -66.31144
[8] -66.41232 -66.22285 -65.67263 -64.72443 -63.35823 -61.56761 -59.35675
[15] -56.73643 -53.73821 -50.40864 -46.79221 -42.94387 -38.91828 -34.76291
[22] -30.51801 -26.21863 -21.89122 -17.55320
谢谢你的尝试,但不要掷骰子。希望重现csaps()
。我根据我的问题修改了你的(Hiebeler的)R代码,但它没有起到作用。你已经有了一个方法,该方法在小数点后5位(在比率上)是一致的。我看不到试图获得与未定义的“某物”近似值的精确一致的价值。@DWin这是一些遗留代码的大型matlab->R转换项目中的一个小链轮。下游的很多东西都依赖于此,并且该函数在原始代码中使用了不止一次。这对我们很重要,因为下游的估计需要精确,就像在exactExact中一样?你刚才画了两条样条线穿过这组散乱的点。你怎么能断言它们中的任何一个是“精确的”?@DWin是的,这就像指画,我明白了。我并不认为这两者都准确地代表了数据。我需要的是准确地再现结果,而不是准确地找到真相。任务不是突发奇想。这个问题是正确的。如果它没有在R中实现,那么我就将就一下,但如果可能的话,我仍然会寻找答案。您应该检查R的结果对“spar”的变化有多敏感。