Could the equation of the curve provided by LOESS be obtained?

Considering the LOESS fitting done with the following R code:

x<-c(1:20)
y<-rnorm(20,10,5)
l_model<-loess(y~x,span = 0.5)
plot(x,y,pch=20,cex=0.5)
lines(x,l_model$fitted,col='red',lwd=2)  My question is, can I extract the equation (y~x) of this curve? How to do it with R? 2 Answers [Note that "how do I do in R" questions are usually off topic; however there's enough to say of a statistical nature that I think we can discuss this here.] The weights to observations change as$x$changes, so - while there is an underlying simple equation that could be used to find the fitted value at any specified$x=x_0$* - that equation will be different for every such value of$x$you evaluate it at (whether at a data point or not). That's not really any more useful than just giving the fitted value at any$x$you ask for it at since the particular equation you use at some point is not really any use at some other point. * which is to say, if I'm a LOESS function, when you give me$x_0$I should be able to return to you a final set of weights to use in a weighted regression with those weights you can just call a weighted least squares routine to obtain the fitted line used to obtain that$\hat{y}$for that value,$x_0$. i.e. if LOESS tells you what weights it worked out at that point you can get the equation it used at that point. With a single predictor, you could potentially trace out the changing value of the slope and intercept parameter as you progress across the data (i.e. give$\hat{\beta_0}(x)$and$\hat{\beta_1}(x)$; that might be interesting in some situations. If your favourite LOESS routine won't tell you the information you want, you can 1. implement whichever version of LOESS you like directly yourself. It's not particularly complex - though there are a couple of different versions around in various references (Bill Cleveland's ideas developed over time and he produced several different implementations with a number of colleagues) 2. Find an implementation you can modify (e.g. see netlib dloess here: http://www.netlib.org/a/dloess; there are other loess implementations to be found there and elsewhere) 3. Or, since you have R. see loess and stats:::simpleLoess; (those in turn call other code, in C and Fortran which you can find also). You might be able to modify the code (well, a copy of the code) to get the local slope. 4. Probably the easiest by a long way: you can also approximate the local slope numerically. If for small$h$(* see below) you take$b(x_0)=(\hat{y}(x_0+h/2)-\hat{y}(x_0-h/2))/h$then that will be a good approximation of the local slope. Here's an example I did (adapting the cars example from the loess help): The original loess fit to the data is on the left; the numerically approximated slope is shown on the right. There are two different slope calculations with different values of$h\$, shown in red (dashed) and black (solid) so that both can be seen; they are visually indistinguishable.

* you want it fine enough that the slope isn't changing much in the interval but not so fine that it's numerically unstable to get the slope by finite differences. Done with care it should be quite suitable.

(Appendix: Since people always ask for it, this is the code used; it's a bit rough)

    opar <- par()
par(mfrow=c(1,2))

cars.lo <- loess(dist ~ speed, cars)
xx <- seq(5, 25,.01)
pred <-  predict(cars.lo, data.frame(speed = xx))
plot(dist~speed,cars,col=rgb(.5,.5,.5,.5),pch=16)
lines(pred~xx,lwd = 2)

nn <- length(pred)
sl <- diff(pred,lag=2)/(xx[3]-xx[1])
plot(sl~xx[2:(nn-1)],type="l",lwd=2) # plot using h/2 = 0.01 in black

xx2 <- seq(5, 25,.05)
nn2 <- length(pred2)
pred2 <-  predict(cars.lo, data.frame(speed = xx2))
sl2 <- diff(pred2,lag=2)/(xx2[3]-xx2[1])
lines(sl2~xx2[2:(nn2-1)],col=2,lty=2,lwd=2) # plot using h/2 = 0.05 in red
par(opar)

• Thanks for your detailed answer. My purpose of desiring an fitting equation is to trace the value of the slope k(x,y) at each given x. Since a fix equation is not exist for LOESS, is there any other method to get the slopes? Feb 27, 2017 at 3:49
• I added some discussion Feb 27, 2017 at 4:34
• And an example. Feb 27, 2017 at 7:46
• That's really a super beneficial answer. Feb 28, 2017 at 2:14

You should try multivariate adaptive regression splines via the earth package in R. http://uc-r.github.io/mars