Best value for span / f parameter in lowess / loess function in R

3

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

    
asked by anonymous 11.07.2015 / 01:18

1 answer

1

It would be nice to post what the purpose of doing this, it may be that loess functions are not the best solution. The purpose of this type of function is usually not to achieve the greatest fit to the data but to better represent the trend in the data, eliminating all that is "noise." It is relatively simple to adjust the parameter to minimize some error function, but perhaps the best solution is another model.

Minimizing the quadratic error:

tuneSpline = function(x,y,span.vals=seq(0.01,1,by=0.01)){
  theta <- function(x,y,f){ lowess(x=x,y=y, f= f)$y }
  mqe <- sapply(span.vals, function(f){
    ylow <- theta(x,y,f=f)
    fltr <- which(!is.na(ylow))
    mean((y[fltr]-ylow[fltr])^2)
  })
  list(mspan=span.vals[which.min(mqe)],mqe=cbind(span.vals,mqe))
}

attach(cars)
tuneSpline(speed,dist)$mspan
plot(tuneSpline(speed,dist)$mqe)

[1] 0.12

    
14.07.2015 / 03:38