1 Algoritmo

  • Para \(x_i, \ i = 1, \ldots, n\):
    • Se eligen un total de \(k = s * n\) puntos alrededor de \(x_i\).
    • Se ajusta un modelo de regresiĆ³n lineal en \(x_i\) utilizando los k puntos.
    • El valor predicho pra \(x_i \Rightarrow \hat f(x_i) = \hat \beta_0 + \hat \beta_1 x_i\).
  • El parĆ”metro s controla la suavidad de la curva (\(s \in [0,1]\)).
  • Se pueden estimar otras funciones polinĆ³micas distintas a la recta.

2 Estimacion del modelo

library(ISLR)
datos = Wage
datos = datos[datos$wage<250,]
m1 = loess(wage ~ age, data = datos, span = 0.2)
m2 = loess(wage ~ age, data = datos, span = 0.5)

3 Prediccion

age_grid = seq(from = min(datos$age), to = max(datos$age), by = 1)
alfa = 0.05
yp1 = predict(m1, newdata = data.frame(age = age_grid), se = T)
yp2 = predict(m2, newdata = data.frame(age = age_grid), se = T)
yp11 = yp1$fit + qnorm(alfa/2)*yp1$se.fit
yp12 = yp1$fit + qnorm(1-alfa/2)*yp1$se.fit
plot(datos$age,datos$wage, cex = 0.5, col = "darkgrey", ylab = "wage (x 1000 $)", xlab = "age")
#
lines(age_grid, yp1$fit, col = "blue", lwd = 2)
lines(age_grid, yp11, col = "blue", lty = 3)
lines(age_grid, yp12, col = "blue", lty = 3)
#
lines(age_grid, yp2$fit, col = "red", lwd = 2)
#
legend(60,200, legend = c("s = 0.2", "s = 0.5"), col = c("blue","red"), lty = 1, lwd = 2)