Adding non-parametric model curves with lowess

In this recipe, we will learn how to use lowess, a non-parametric model, and add the resulting prediction curve to a scatter plot.

Getting ready

For this recipe, we don't need to load any additional libraries. We just need to type the recipe at the R prompt or run it as a script.

How to do it...

First, let's make a simple scatter plot with the pre-loaded cars dataset and add a couple of lowess lines to it:

plot(cars, main = "lowess(cars)")
lines(lowess(cars), col = "blue")
lines(lowess(cars, f=0.3), col = "orange")
How to do it...

How it works...

Standard R sessions include the lowess() function. It is a smoother which uses locally weighted polynomial regression. The first argument, in this instance, is a data frame called cars giving the x and y variables (speed and dist). So we apply the lowess function to the dataset cars and in turn pass that result to the lines() function. The result of lowess is a list with components named x and y. The lines() function automatically detects that and uses the appropriate values to draw a smooth line through the scatter plot. The second smooth line has an additional argument f, which is known as the smoother span. This gives the proportion of points in the plot which influence the smoothening at each value. Larger values give more smoothness. The default value is approximately 0.67, so when we changed it to 0.3 we get a less smooth fit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset