3
I’m facing a problem with smoothing curves adjusted by functions lowess
or loess
in R, for the choice of the smoothing parameter f
in the lowess
or span
in the loess
I looked for some tips on web and found the resolution of a similar problem for function smooth.spline()
as follows
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 on the same data set, 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"
I’d appreciate it if you could help, thank you
Ps: I haven’t posted my own data because I don’t have it yet
thanks in advance
The error is caused because
lowess
returns a list ofx
andy
, whilesmooth.spline
returns a class objectsmooth.spline
. The functioncrossvall
You know what to do with the result of the second, but not the first. Are you sure this solution fits your problem? Perhaps the ideal is to post your original problem and not your attempt to adapt another solution to it.– Molx
Thank you friend for clarifying the error. My problem is to "simply" find a way to optimize lowess or loess functions for the best fit parameter to be chosen. The problem is q, unfortunately I have no idea how to do this, so looking for similar problems I found this solution for Smooth.Spline function, so I thought I’d adapt it to these two. The fact is that I wanted to use a regression method (curve adjustment) that is not parametric at all (q does not depend on any parameter, or q suffers the minimum action of the user). Very Obrg for the help
– david clarck
Your problem does not seem to be too complex. Perhaps the solution involves putting the
loess
within a function, and then useoptimize
for the parameter in question. If you have the input data, the parameter to be optimized (within a range) and the criterion to consider optimal, finding the ideal value should not be complicated.– Molx