How to use Lowess Smoothing in R with example

Lowess Smoothing in R, The term “locally weighted scatterplot smoothing” is used in statistics to describe the process of creating a smooth curve that matches the data points in a scatterplot.

The lowess() function in R can be used to do lowess smoothing with the following syntax.

Regression Analysis Example-Ultimate Guide » finnstats

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

where:

x: A numerical vector of x values.
y: A numerical vector of y values.
f: The smoother span's value is this. This indicates the percentage of points in the plot that have an impact on the smooth at each value. More smoothness is achieved with higher values.

The example below explains how to use R to conduct lowess smoothing on a given dataset.

Step 1: Create the Data

Let’s start by making an x, y dataset.

df <- data.frame(x=c(1, 1, 2, 2, 3, 4, 5, 5, 6, 9, 10, 10, 11, 11, 12, 14),
                 y=c(14, 17, 12, 90, 24, 25, 9, 6, 17, 22, 23, 33, 45, 42, 41, 55))
df
    x  y
1   1 14
2   1 17
3   2 12
4   2 90
5   3 24
6   4 25
7   5  9
8   5  6
9   6 17
10  9 22
11 10 23
12 10 33
13 11 45
14 11 42
15 12 41
16 14 55

Step 2: Make a graph of the data

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

plot(df$x, df$y)

Step 3: Draw the Lowess Curve on a graph.

Then, across the points in the scatterplot, plot the lowess smoothing curve.

Random Number Generator in R » finnstats

make a scatterplot

plot(df$x, df$y)

add lowess smoothing curve to plot

lines(lowess(df$x, df$y), col='red')

Step 4: Smoother Span can be tweaked (Optional)

We can also change the value utilized for the smoother span by changing the f argument in the lowess() algorithm.

Naive Bayes Classifier in Machine Learning » Prediction Model » finnstats

It’s worth noting that the higher the value, the smoother the lowess curve will be.

Now make a scatterplot

plot(df$x, df$y)
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')
legend('topleft',
       col = c('red', 'purple', 'steelblue'),
       lwd = 2,
       c('Smoother = 1', 'Smoother = 0.3', 'Smoother = 3'))

You may also like...

Leave a Reply

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

1 × 2 =