# loess example span = 0.8 model = loess(dist ~ speed, cars, span = span, degree = 1, control = loess.control(surface = "direct")) new.input.value = 21 predict(model, new.input.value) # simple loess implementation X = cbind(rep(1, length(cars$speed)), cars$speed) y = cars$dist neighbor.count = as.integer(span * nrow(X)) distance.threshold = sort(abs(X[,2] - new.input.value))[neighbor.count] weights = rep(0, nrow(X)) selected = abs(X[,2] - new.input.value) <= distance.threshold # weights derived using the tricube weight function weights[selected] = (1 - (abs(X[selected,2] - new.input.value)/distance.threshold)^3)^3 W = diag(weights) coefficients = solve(t(X) %*% W %*% X, diag(ncol(X))) %*% t(X) %*% W %*% y coefficients[1] + coefficients[2] * new.input.value