Entering edit mode
Hi,
Does anyone know well about the parameter optimization for loess or smooth.spline?
In function loess or smooth.spline, the parameter span (or spar for smooth.spline) is very important for the fitting. I have tried to use the following method to optimize the parameter spar for smooth.spline:
tuneSpline = function(x,y,span.vals=seq(0.1,1,by=0.05),fold=10){
mae <- numeric(length(span.vals))
fun.fit <- function(x,y,span) {smooth.spline(x = x,y = ,spar = span)}
fun.predict <- function(fit,x0) {predict(fit,x0)$y}
ii = 0
for(span in span.vals){
ii <- ii+1
y.cv <- crossval(x,y,fun.fit,fun.predict,span=span,ngroup = fold)$cv.fit
fltr <- !is.nay.cv)
save(fltr,y.cv,y,file="tmp.rda")
mae[ii] <- mean(abs(y[fltr]-y.cv[fltr]))
}
span <- span.vals[which.min(mae)]
return(span)
}
require(graphics)
attach(cars)
tuneSpline(speed,dist,fold = length(dist))
## return 0.1
But the optimized spar by this method is always 0.1 (the minimum value in span.vals).
It's weird and I think the result may not be right.
Can anyone help me about this issue?
Best regards!
Bo