lowess in R: Scatter Plot Smoothing

lowess in r

In this article, we will share with you the definition, the syntax, and how to use the lowess in R while working with the graph. The lowess() function in R is used to visualize the graph. Let’s learn more about it with the explanation and examples below.

What does the lowess do in R?

The lowess() function in R is used to visualize the graph while working with the unsmooth data. This function can help users convert from a complicated graph to the more simple graph than this one. Let’s learn about the syntax of the lowess() function below.

Syntax:

lowess(x, y, f, iter, delta = 0.01 * diff(range(x)))

Parameters:

  • x,y: The vector of the latitudes and longitudes to plot the graph. The default y is NULL.
  • f: The default is 2/3. The value is used to make the graph smoother. Called a smoother span.
  • iter: The default is 3. The number of iterations which this function will perform.
  • delta: The default is 1/100 the size of x.

After learning what the lowess() does in R, and the syntax of this function, you will learn how to use the lowess() function in the next title below.

How to use the lowess in R?

As you have learned in the previous part, the lowess() function helps you visualize the graph while working with unsmooth data. Let’s learn how to use the lowess() function with the examples below.

Prepare the dataset

First, we must create the data frame to visualize data. For convenience, I will use the ‘cars’ data which is available in the R language. 

Let’s take a look at the data set.

cars

Output

    speed dist
1      4    2
2      4   10
3      7    4
4      7   22
…
50    25   85

This dataset has 50 rows and 2 columns.

Use the lowess() function 

In this example, I will use the lowess() function with the default syntax to visualize the dataset above.

Let’s take a look at the initial graph.

# Create the latitude and longitude
x <- cars$speed
y <- cars$dist

# Plot the initial graph
plot(x, y, type = "l", col = "red")

Output

Use the lowess function to visualize this graph.

Look at the example below.

x <- cars$speed
y <- cars$dist

# Smooth the data with the lowess() function
lowess_points <- lowess(x, y)
plot(lowess_points, type = "l", col = "blue")

Output

Use the lowess() function with the specified smoother span

You can customize your graph by assigning the value to a parameter named ‘f’ in the lowess() function.

Look at the example below.

x <- cars$speed
y <- cars$dist

# Smooth the data with the lowess() function with f as 0.13
lowess_points <- lowess(x, y, f = 0.13)
plot(lowess_points, type = "l", col = "blue")

Output

Click here, if you are working with the geographical data.

Summary

You have learned the definition, and how to perform the lowess in R. You can visualize your graph by using the lowess() function. It is useful for you when working with unsmooth data. If you have any questions about this guide, leave your comment below, and I will answer your questions. Thanks!

Maybe you are interested:

    Posted in R

    Leave a Reply

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