# 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") |

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 |

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 |

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 |

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") |

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

**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 |

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

**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") |

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")) |

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"))

**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

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R