如何为smooth.spline()选择平滑参数? [英] how do I select the smoothing parameter for smooth.spline()?

查看:884
本文介绍了如何为smooth.spline()选择平滑参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道平滑参数(lambda)对于拟合平滑样条曲线非常重要,但是我在这里没有看到有关如何选择合理的lambda(spar =?)的任何文章,有人告诉我spar通常在0比1.在使用smooth.spline()时,谁能分享您的经验?谢谢.

I know that the smoothing parameter(lambda) is quite important for fitting a smoothing spline, but I did not see any post here regarding how to select a reasonable lambda (spar=?), I was told that spar normally ranges from 0 to 1. Could anyone share your experience when use smooth.spline()? Thanks.

    smooth.spline(x, y = NULL, w = NULL, df, spar = NULL,
          cv = FALSE, all.knots = FALSE, nknots = NULL,
          keep.data = TRUE, df.offset = 0, penalty = 1,
          control.spar = list(), tol = 1e-6 * IQR(x))

推荐答案

agstudy提供了一种选择spar的直观方法.我记得我从线性模型类中学到的(但不完全是)是使用交叉验证来选择最佳" spar.这是从agstudy借来的一个玩具示例:

agstudy provides a visual way to choose spar. I remember what I learned from linear model class (but not exact) is to use cross validation to pick "best" spar. Here's a toy example borrowed from agstudy:

x = seq(1:18)
y = c(1:3,5,4,7:3,2*(2:5),rep(10,4))
splineres <- function(spar){
  res <- rep(0, length(x))
  for (i in 1:length(x)){
    mod <- smooth.spline(x[-i], y[-i], spar = spar)
    res[i] <- predict(mod, x[i])$y - y[i]
  }
  return(sum(res^2))
}

spars <- seq(0, 1.5, by = 0.001)
ss <- rep(0, length(spars))
for (i in 1:length(spars)){
  ss[i] <- splineres(spars[i])
}
plot(spars, ss, 'l', xlab = 'spar', ylab = 'Cross Validation Residual Sum of Squares' , main = 'CV RSS vs Spar')
spars[which.min(ss)]
R > spars[which.min(ss)]
[1] 0.381

代码不是最整洁的,但是很容易理解.另外,如果您在smooth.spline中指定cv=T:

Code is not neatest, but easy for you to understand. Also, if you specify cv=T in smooth.spline:

R > xyspline <- smooth.spline(x, y, cv=T)
R > xyspline$spar
[1] 0.3881

这篇关于如何为smooth.spline()选择平滑参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆