# How do you get the regression equation from OLS and Lowess in R?

Below is an example scatter plot between the variable LWAGE (Log of salary) and the variable EXPER(number of years of experience) for a sample size n=3000. In this scatter plot there is the straight line OLS (green) and the curved line (nonparametric method lowess in red).

Here are the R codes :

scatterplot(Dataset$LWAGE~Dataset$EXPER, reg.line=lm, smooth=FALSE, spread=FALSE,
boxplots=FALSE, span=0.5, data=Dataset)
lines(lowess(Dataset$EXPER,Dataset$LWAGE),col='red', lwd=2)


I would have appreciated to know the R codes to get the equation of the straight line OLS (in green) and the equation for the curved line (lowess in red)?

The result for the OLS straight line should be something like: LWAGE = 6.25 + 0.05*EXPER

• Please take some time to learn the way to property format your posts. You can't attach files at this site, but you can insert a link to a file posted somewhere else (e.g. dropbox link). – Andy W Jul 14 '13 at 14:41
• @Frank Harrell explains the lowess case below (+1). For help on how to get the regression equation for the OLS straight line, see here (simplified answer), or here (more comprehensive). – gung Jul 14 '13 at 14:50
• @chi's answer to this question: stats.stackexchange.com/questions/24242/… also illustrates the use of the R 'pkg:rms' facilities for producing the requested equation in the non-spline situation. – DWin Jul 14 '13 at 17:09

A nonparametric smoother does not have an equation. You report it as a graph or table. If you need an equation, you can use a regression spline in $X$ when fitting the model (e.g., OLS). Here is one example in R.

require(rms)
f <- ols(y ~ rcs(exper,5), data=mydata)
# rcs = restricted cubic spline; 5 knots placed at quantiles of X
f   # show coefficients, stats
Function(f)  # show R code for fitted equation
latex(f)     # same in LaTeX notation

• Hi, Thanks a lot for your answer. If I consider the following model estimated by OLS : LWAGE = B0 + B1*EXPER + B2*EXPER(squared) + residuals, the marginal return (I mean 1 year more experience) is : B1+2*B2*EXP With my red smoothing curve, given that there is no equation for this curve is it possible to calculate the marginal return of experience ? If yes, how can I calculate the marginal return of experience ? Kind Regards, – varin sacha Jul 15 '13 at 16:45
• That is not completely defined. You have taken a quadratic relationship as a special case. Think more generally (e.g., spline functions), and instead of considering a change in $Y$ per one-unit change in $X$ just plot $\hat{f}(X)$ vs. $X$. – Frank Harrell Jul 15 '13 at 18:17
• Hi, Starting from my simple model:LWAGE=B0+B1*EXPER+residuals I insert in my model the variable EXPERSQ (=EXPER squared): LWAGE=B0+B1*EXPER+B2*EXPERSQ+residuals It would make sense to plot the quadratic fit on the plot (LWAGE and EXPER) something like: lines(exper, predict(lmod2), col=3) where lmod2 is the quadratic model. But this R script doesn't work. I need to consider the linear and quadratic terms together.What I want to do is predict using the quadratic model and then overlay the fitted line in the original plot, and see how close that comes to the red line. How can I do that with R? – varin sacha Jul 17 '13 at 20:22
• You need to study R a bit more. If you want to try the R rms package instead, you can use the following functions to do this easily: ols, datadist, Predict, plot. – Frank Harrell Jul 17 '13 at 20:53