我正在尝试根据GCV优化LOESS拟合的跨度值。以下是我正在使用的代码。
gcv.calc <- function(span){
for (sp in span)
{
fit <- loess(y ~ x, data = df, span = sp)
res <- residuals(fit)
sse <- sum(res ^ 2)
## Calculate trace of Hat Matrix
H <- X %*% (solve(t(X) %*% X)%*% t(X)) ### solve() calculates the inverse of a matrix
traceMat <- sum(diag(H))
GCV <- (n * sse) / (n - traceMat) ^ 2
cal[[length(cal)+1]] = GCV
}
return(cal)
}
span <- seq(from = 0. 1, to = 0.5, by = 0.01)
output <- gcv.calc(span)
&#13;
我希望将LOESS平滑化,以便在跨度变化时调整自由度,并尽量减少GCVS尽可能低的自由度。
如何将正则化参数和平滑约束结合到帽子矩阵中?