Patrick Li Patrick Li - 1 month ago 21
R Question

Prediction at a new value using lowess function in R

I am using

lowess
function to fit a regression between two variables
x
and
y
. Now I want to know the fitted value at a new value of
x
. For example, how do I find the fitted value at
x=2.5
in the following example. I know
loess
can do that, but I want to reproduce someone's plot and he used
lowess
.

set.seed(1)
x <- 1:10
y <- x + rnorm(x)
fit <- lowess(x, y)
plot(x, y)
lines(fit)


enter image description here

Answer

Local regression (lowess) is a non-parametric statistical method, it's a not like linear regression where you can use the model directly to estimate new values.

You'll need to take the values from the function (that's why it only returns a list to you), and choose your own interpolation scheme. Use the scheme to predict your new points.

Common technique is spline interpolation (but there're others):

https://www.r-bloggers.com/interpolation-and-smoothing-functions-in-base-r/

EDIT: I'm pretty sure the predict function does the interpolation for you. I also can't find any information about what exactly predict uses, so I've tried to trace the source code.

https://github.com/wch/r-source/blob/af7f52f70101960861e5d995d3a4bec010bc89e6/src/library/stats/R/loess.R

else { ## interpolate
## need to eliminate points outside original range - not in pred_

I'm sure the R code calls the underlying C implementation, but it's not well documented so I don't know what algorithm it uses.

My suggestion is: either trust the predict function or roll out your own interpolation algorithm.