如何为smooth.spline()选择平滑参数?

use*_*001 7 lambda r smooth spline smoothing

我知道平滑参数(lambda)对于拟合平滑样条非常重要,但是我没有看到关于如何选择合理的lambda(spar =?)的任何帖子,我被告知spar通常在0到1之间有没有人可以在使用smooth.spline()时分享你的经验?谢谢.

    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))
Run Code Online (Sandbox Code Playgroud)

liu*_*hao 9

agstudy提供了一种可视化的选择方式spar.我记得我从线性模型类中学到的东西(但不精确)是使用交叉验证来选择"最佳" spar.这是从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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

代码不是最好的,但很容易理解.此外,如果指定cv=Tsmooth.spline:

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


ags*_*udy 5

smooth.spline你的帮助下有以下几点:

使用的计算λ(作为\ code {spar}的函数)是λ= r*256 ^(3*spar - 1)

晶石可以大于1(但我猜不会太多).我认为您可以改变这些参数并通过绘制不同翼梁的拟合值以图形方式选择它.例如:

spars <- seq(0.2,2,length.out=10)          ## I will choose between 10 values 
dat <- data.frame(
  spar= as.factor(rep(spars,each=18)),    ## spar to group data(to get different colors)
  x = seq(1:18),                          ## recycling here to repeat x and y 
  y = c(1:3,5,4,7:3,2*(2:5),rep(10,4)))
xyplot(y~x|spar,data =dat, type=c('p'), pch=19,groups=spar,
       panel =function(x,y,groups,...)
       {
          s2  <- smooth.spline(y,spar=spars[panel.number()])
          panel.lines(s2)
          panel.xyplot(x,y,groups,...)
       })
Run Code Online (Sandbox Code Playgroud)

例如,我得到spars = 0.4的最佳结果

在此输入图像描述