Matlab的csaps()的R等效项是什么

编程入门 行业动态 更新时间:2024-10-24 15:14:07
本文介绍了Matlab的csaps()的R等效项是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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] % 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

csaps()帮助页面位于此处

可以找到

smooth.spline()帮助这里(未给出代码,因为我认为spar和p之间的关系可能很毛茸茸,所以也许不值得沿着这条路走)

pspline::smooth.Pspline()帮助位于此处

此其他人从2008年以来的追求似乎一直没有答案,这让我感到这家伙.

R充满了样条曲线,所以如果你们中的高超技巧可以使我指向与matlab的csaps()做相同事情的人(或沿这些思路的把戏),那我将是最感激的.

需要将spar指定为(1-p)/p(而不是1/p),然后结果将与数值精度相吻合.请参阅下面的答案.

解决方案

我的同事找到了答案:一个将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() in matlab does a cubic spline according to a particular definition of the smoothing parameter p. Here is some matlab code and its result:

% 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

I'd like to get the same result in R. I've tried base::smooth.spline(), but the smoothing parameter spar is specified in a different way that I can't seem to relate to matlab's p (can you?). The closest result I've been able to get has been with the smooth.Pspline() function of the pspline package. Here is some code to get things rolling in 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

The csaps() help page is here

smooth.spline() help can be found here (code not given because I think maybe the relation between spar and p is pretty hairy, so maybe not worth going down this path)

pspline::smooth.Pspline() help is here

This other person's quest from 2008 appears to have gone unanswered, making me feel like this guy.

R is chock full of spline doings, so if the saavy amongst ye can point me to the one that does the same thing as matlab's csaps() (or a trick along those lines) I'd be most appreciative.

[EDIT 19-8-2013] spar needs to be specified as (1-p)/p (rather than 1/p) and then results will agree to as far as numerical precision can take you. See answer below.

解决方案

My colleague found the answer: One converts matlab's p to pspline::smooth.Pspline()'s spar not as 1/p, but as (1-p)/p, and then results will agree out to whatever the degree of numerical precision is:

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

更多推荐

Matlab的csaps()的R等效项是什么

本文发布于:2023-10-07 15:31:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Matlab   csaps

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!