How to Perform Lowess Smoothing in R (Step-by-Step)


In statistics, the term lowess refers to “locally weighted scatterplot smoothing” – the process of producing a smooth curve that fits the data points in a scatterplot.

To perform lowess smoothing in R we can use the lowess() function, which uses the following syntax:

lowess(x, y, f = 2/3)

where:

  • x: A numerical vector of x values.
  • y: A numerical vector of y values.
  • f: The value for the smoother span. This gives the proportion of points in the plot which influence the smooth at each value. Larger values result in more smoothness.

The following step-by-step example shows how to perform lowess smoothing for a given dataset in R.

Step 1: Create the Data

First, let’s create a fake dataset:

df <- data.frame(x=c(1, 1, 2, 2, 3, 4, 6, 6, 7, 8, 10, 11, 11, 12, 13, 14),
                 y=c(4, 7, 9, 10, 14, 15, 19, 16, 17, 21, 22, 34, 44, 40, 43, 45))

Step 2: Plot the Data

Next, let’s plot the x and y values from the dataset:

plot(df$x, df$y)

Step 3: Plot the Lowess Curve

Next, let’s plot the lowess smoothing curve over the points in the scatterplot:

#create scatterplot
plot(df$x, df$y)

#add lowess smoothing curve to plot
lines(lowess(df$x, df$y), col='red')

Lowess smoothing example in R

Step 4: Adjust the Smoother Span (Optional)

We can also adjust the f argument in the lowess() function to increase or decrease the value used for the smoother span.

Note that the larger the value we provide, the smoother the lowess curve will be.

#create scatterplot
plot(df$x, df$y)

#add lowess smoothing curves
lines(lowess(df$x, df$y), col='red')
lines(lowess(df$x, df$y, f=0.3), col='purple')
lines(lowess(df$x, df$y, f=3), col='steelblue')

#add legend to plot
legend('topleft',
       col = c('red', 'purple', 'steelblue'),
       lwd = 2,
       c('Smoother = 1', 'Smoother = 0.3', 'Smoother = 3'))

Lowess smoothing curves in R

Additional Resources

How to Plot Multiple Lines in One Chart in R
How to Create a Scatterplot with a Regression Line in R
How to Perform Polynomial Regression in R

Leave a Reply

Your email address will not be published.