gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:01:06 25 4
gpt4 key购买 nike

我知道平滑参数(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))

最佳答案

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

代码不是最整洁的,但是很容易理解。另外,如果您在 cv=T中指定 smooth.spline:
R > xyspline <- smooth.spline(x, y, cv=T)
R > xyspline$spar
[1] 0.3881

关于r - 如何为smooth.spline()选择平滑参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14929268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com