lowess() R Smoothing Function | 2 Example Codes for Normalization by Lowess Regression

 

Basic R Syntax:

lowess_values <- lowess(x, y)
plot(lowess_values, type = "l")

 

The lowess R function computes the lowess smoother. Typically, the smoothed values are used for visualization in statistical graphics. The basic syntax for lowess in R is illustrated above.

In the following R tutorial, I’ll show two reproducible examples for the application of lowess in the R programming language.

I assume that you know at this point, how the lowess regression works. If not, you may want to have a look at this tutorial first.

So without further ado, let’s start right away…

 

Example 1: Lowess Smoothing in R – The Basic Application

Before we can start with the example, we need to load some data into R (or RStudio). For the example, I’m going to use the beaver1 data set, a data frame consisting of time series of body temperature dynamics of beavers.

We can load the example data as follows:

data(beavers)                                                       # Load data into R

The two variables we are interested in, are the time (measured in minutes) and the body temperature of the beavers. Let’s extract this data from the data matrix:

Minutes <- seq(10, nrow(beaver1) * 10, 10)                          # Extract minutes...
Temperature <- beaver1$temp                                         # ... and temperature

Now, we can compute the lowess regression values with the R lowess function:

lowess_values <- lowess(Minutes, Temperature)                       # Calculate lowess regression

That’s it, the computed X and Y values of the lowess regression are stored in the new data object lowess_values.

Typically, lowess values are used for visualization. Let’s do this!

First, we create a regular scatter plot in R:

plot(Minutes, Temperature, type = "l",                              # Regular X-Y plot in R 
     main = "Body Temperature of Beavers Over Time")

 

lowess R function example - graphic 1 - before application

Graphic 1: Scatter Plot before Application of lowess().

 

You probably have seen such a plot many times, haven’t you?

We can overlay this scatter plot with the lowess values as follows:

lines(lowess_values, col = "red")                                   # Add line for lowess regression

 

lowess R function example - graphic 2 - after application

Graphic 2: Scatter Plot after Application of lowess().

 

As you can see, the plot is overlaid by a line – the lowess regression.

Looks nice, doesn’t it? But don’t stop reading here. There’s a nice feature of the lowess R function that I want to show you in the next example…

 

Example 2: Lowess Regression with Differing Smoother Span

An important option of the lowess R function is the smoother span. The smoother span determines the number of data points which influence the smooth at each value. The larger the smoother span, the more extreme the smoothing.

Let’s see how that works in practice. I’m just continuing the code of example 1, so make sure to run this part of the code, before you continue here.

lines(lowess(Minutes, Temperature, f = 0.1), col = "green")     # Add lowess values with different normalization
lines(lowess(Minutes, Temperature, f = 5), col = "cornflowerblue")

Let’s also add a legend to our plot:

legend("topleft",                                               # Add legend to scatter plot
       col = c("red", "green", "cornflowerblue"),
       lty = 1,
       lwd = 2,
       c("Default Smoothing", "Smoother Span = 0.1", "Smoother Span = 5"))

 

lowess R function example - graphic 3 - after application of different smoother spans

Graphic 3: Scatter Plot after Application of lowess() with Varying Smoothing.

 

We drew two more regression lines to our plot. Once, with a smoothing of 0.1 (green line) and once with a smoothing of 5 (blue line). As you can see, the smaller smoother span leads to a much closer approximation of the observed values than the larger smoother span.

 

Video Tutorial: Fitting a Regression Line to a Plot

Do you have problems to understand the previous examples? No problem, there is a very good video tutorial available at the StatQuest channel of Josh Starmer. In the video, he is explaining the theoretical concept of fitting a regression curve to some real data. If you are struggling with the idea of lowess regression, the video might be helpful for you.

 

 

Further Reading

 



 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top