Product: TIBCO Spotfire®
How can I extract the coefficients from a loess model?
The coefficients from a loess() model are not available. The following response on R-Help group give a simple explanation of why:
There are two alternate methods to obtain coefficients that could be helpful:
The loess() function is fit by a weighted linear or quadratic regression, with weights that vary smoothly as x changes. For any given value x0, the program computes the regression internally.
1. The set of regression coefficients is computed internally.
However, because the weights are changing, the line or parabola determined by those coefficients does NOT match the loess curve in general. Because of this, it would be rare for the coefficients to be useful.
For example, the following code demonstrates that when using linear loess (degree=1), the fitted curve is nonlinear.
set.seed(0) # for reproducibility
x _ runif(20)
y _ runif(20)
fit _ loess(y~x, degree=1)
abline(v=x) # these are the original x values
# Now zero in on a region with a larger curvature, with high resolution
x2 _ seq(.7, .78, length=100)
plot( x2, predict(fit, newdata=data.frame(x=x2)), type="l")
Since the set of regression coefficients is computed internally, they are not directly available, for example with coef().
To recreate them, one could do a weighted linear or quadratic regression. See the references listed in the help file for loess for
details on computation of the weights. If you are using robust fitting then the weights will change with each iteration.
2. The second possibility is the coefficients for a Taylor-Series approximation to the curve at the point. It is easiest to get these numerically, evaluating the first (and second) derivative(s) of the curve at any given point by predict()ing the fit at the point and nearby points, e.g.
x0 _ .5 # point at which I want predictions
delta _ .001 # small value, for numeric differentiation
x.temp _ c(x0, x0+delta)
pred _ predict(fit, newdata=data.frame(x=x.temp))
coef(lm( pred ~ x.temp))
abline(coef(lm( pred ~ x.temp)))
Alternately, the help file for loess() lists a number of references which describe the algorithm. Using these references it would be possible to find formulae for calculating the derivatives analytically.
The details for the loess() function and predict.loess() are discussed in the following technical report: