csaps()
在 matlab 中,根据平滑参数的特定定义进行三次样条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
(你可以吗?)。我能得到的最接近的结果是包的smooth.Pspline()
功能。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
csaps() 帮助页面在这里
smooth.spline()
可以在这里spar
找到帮助(未给出代码,因为我认为和之间的关系可能p
很复杂,所以可能不值得走这条路)
pspline::smooth.Pspline()
帮助在这里
这个其他人从 2008 年开始的任务似乎没有得到答复,让我觉得自己像这个人。
R充满了样条函数,所以如果你们中的聪明人可以指出我与matlab做同样的事情csaps()
(或沿着这些路线的技巧),我会非常感激。
[EDIT 19-8-2013]
spar
需要指定为(1-p)/p
(而不是1/p
),然后结果将与数值精度一致。请参阅下面的答案。