I'm experiencing a problem with smoothing curves adjusted by the lowess
or loess
functions in R, as far as the smoothing parameter f
in lowess
or span
in loess
I looked for some tips on web and found a similar problem solving for the smooth.spline()
function of the following way
tuneSpline = function(x,y,span.vals=seq(0.1,1,by=0.05),fold=10){
require(bootstrap)
fun.fit <- function(x,y,span) {smooth.spline(x = x,y = y,spar = span)}
fun.predict <- function(fit,x0) {predict(fit,x0)$y}
mae <- sapply(span.vals, function(span){
y.cv <- bootstrap::crossval(
x,y,fun.fit,fun.predict,span=span,ngroup = fold
)$cv.fit
fltr <- which(!is.na(y.cv))
mean(abs(y[fltr]-y.cv[fltr]))
})
span.vals[which.min(mae)]
}
attach(cars)
tuneSpline(speed,dist,fold = length(dist))
# 0.75
I tried to modify this routine as follows:
tuneSpline = function(x,y,span.vals=seq(0.1,1,by=0.05),fold=10){
require(bootstrap)
fun.fit <- function(x,y,f) {lowess(x = x,y = y, f= f)}
fun.predict <- function(fit,x0) {predict(fit,x0)$y}
mae <- sapply(span.vals, function(f){
y.cv <- bootstrap::crossval(
x, y, fun.fit, fun.predict, f=f, ngroup = fold
)$cv.fit
fltr <- which(!is.na(y.cv))
mean(abs(y[fltr]-y.cv[fltr]))
})
span.vals[which.min(mae)]
}
However when I run code in the same dataset, it gives the following error:
attach(cars)
tuneSpline(speed,dist,fold = length(dist))
# Error in UseMethod("predict") :
# método não aplicável para 'predict' aplicado a um objeto de classe "list"
Would appreciate if you could help, thank you
Ps: I did not post my own data because I do not have it yet
Thank you in advance