VOOZH about

URL: https://www.geeksforgeeks.org/r-machine-learning/local-regression-in-r/

⇱ Local Regression in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Local Regression in R

Last Updated : 4 Jul, 2025

Local regression is also known as LOESS (locally estimated scatterplot smoothing) regression. It is a flexible non-parametric method for fitting regression models to data. Local regression models adapt to the local structure of the data and make them particularly useful for analyzing complex relationships and non-linear patterns.

Implementation of Local Regression in R

We implement the LOESS (Locally Estimated Scatterplot Smoothing) technique in R to model non-linear relationships through a step-by-step approach.

1. Installing and Loading Required Packages

We install the essential packages for visualization and data manipulation using install.packages() and load them with library().

  • ggplot2: used for creating advanced visualizations.
  • dplyr: used for data manipulation and filtering.

2. Preparing the Data for Local Regression

We import the dataset, remove missing values, and handle outliers to ensure clean and structured input for the regression.

  • read.csv: used to read the dataset.
  • na.omit: used to remove rows with missing values.
  • sd: used to calculate standard deviation for outlier threshold.
  • filter: used to exclude extreme values beyond the threshold.

3. Performing Local Regression

We fit the LOESS model using the loess() function with specified formula and dataset.

  • loess: used to fit a local regression model.
  • summary: used to display model details.

Output:

👁 data
Output

4. Visualizing the Local Regression Results

We visualize the original data points and the fitted LOESS curve using ggplot2.

  • ggplot: used to initialize a plot.
  • aes: used to map variables to axes.
  • geom_point: used to plot the original data points.
  • geom_smooth(method = "loess"): used to overlay a LOESS smoothed line.

Output:

👁 gh
Local Regression in R

The plot shows a smooth, non-linear LOESS curve fitted to a small dataset, closely following the five data points using a quadratic polynomial. Despite the limited data, the model effectively captures the underlying trend through localized interpolation.

Comment

Explore