Open In App

Curve Fitting in R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Curve fitting in R is the process of finding a mathematical curve that best describes the relationship between input and output variables in a dataset. It is used when the data does not follow a straight line, allowing us to model complex relationships and predict unknown values.

Common Methods for Curve Fitting in R

We use the following methods to fit curves, depending on the nature of our data.

  • Linear Regression: Fits a straight line using least squares and is best for linear relationships.
  • Polynomial Regression: Extends linear regression with polynomial terms and is suitable for nonlinear trends.
  • LOESS / LOWESS: Fits local curves using smoothing and is ideal for noisy, scattered data.
  • Exponential / Logarithmic Models: Models growth, decay or log relationships and is common in biology and finance.
  • Spline Regression: Fits flexible curves with piecewise polynomials and is useful for complex datasets.

Implementing Curve Fitting Using Polynomial Models in R

We will now perform curve fitting using polynomial regression in R programming language, which includes data visualization, model fitting, model evaluation and then plotting the best-fitting curve.

1. Visualizing the Data

We start by plotting the given data points using a scatter plot. This allows us to observe the pattern and spread of the data.

  • sample_data: A data frame containing x and y values.
  • plot(): A base R function used to create a two-dimensional scatter plot.
  • sample_data$x, sample_data$y: Vectors representing the x-axis and y-axis values to be plotted.
R
sample_data <- data.frame(x=1:10,
                          y=c(25, 22, 13, 10, 5, 
                              9, 12, 16, 34, 44))
plot(sample_data$x, sample_data$y)

Output:

scatter_plot
Output

2. Fitting Polynomial Regression Models

We create multiple polynomial regression models to fit curves of increasing complexity to the data, with each model representing a specific polynomial degree.

  • lm(): Used to fit linear models. Here, it fits polynomial relationships between x and y.
  • poly(x, degree, raw=TRUE): Generates polynomial terms of the specified degree from vector x.
  • plot(): Used again to display the scatter plot before adding fitted lines.
  • seq(1, 10, length=10): Generates a sequence of evenly spaced values from 1 to 10, which are used as x-axis values for prediction.
  • predict(): Computes predicted y-values for a given model and new x-values.
  • lines(): Adds the fitted curve to the existing scatter plot.
R
linear_model1 <- lm(y ~ x, data=sample_data)
linear_model2 <- lm(y ~ poly(x,2,raw=TRUE), data=sample_data)
linear_model3 <- lm(y ~ poly(x,3,raw=TRUE), data=sample_data)
linear_model4 <- lm(y ~ poly(x,4,raw=TRUE), data=sample_data)
linear_model5 <- lm(y ~ poly(x,5,raw=TRUE), data=sample_data)

plot(sample_data$x, sample_data$y)
x_axis <- seq(1, 10, length=10)
lines(x_axis, predict(linear_model1, data.frame(x=x_axis)), col='green')
lines(x_axis, predict(linear_model2, data.frame(x=x_axis)), col='red')
lines(x_axis, predict(linear_model3, data.frame(x=x_axis)), col='purple')
lines(x_axis, predict(linear_model4, data.frame(x=x_axis)), col='blue')
lines(x_axis, predict(linear_model5, data.frame(x=x_axis)), col='orange')

Output:

best_fitting_curve
Output

3. Evaluating the Best Fit Model

We evaluate the fitted models based on their adjusted R-squared values, which indicate how well each model explains the variability in the response data while accounting for the number of predictors.

  • summary(): Returns a detailed summary of the linear model.
  • $adj.r.squared: Extracts the adjusted R-squared value from the model summary.

Higher values of adjusted R-squared indicate better model performance.

R
cat("R-square value for Model 1:", summary(linear_model1)$adj.r.squared, "\n")
cat("R-square value for Model 2:", summary(linear_model2)$adj.r.squared, "\n")
cat("R-square value for Model 3:", summary(linear_model3)$adj.r.squared, "\n")
cat("R-square value for Model 4:", summary(linear_model4)$adj.r.squared, "\n")
cat("R-square value for Model 5:", summary(linear_model5)$adj.r.squared, "\n")

Output:

values_For_linear_models
Output

4. Plotting the Best Fit Curve

We select the model with the highest adjusted R-squared value based on the evaluation (in this case, the 4th-degree model) and visualize it clearly on the scatter plot.

  • best_model: Stores the final selected linear model.
  • The lines() function overlays the best-fit curve on the scatter plot.
R
best_model <- lm(y ~ poly(x,4,raw=TRUE), data=sample_data)
plot(sample_data$x, sample_data$y)
x_axis <- seq(1, 10, length=10)
lines(x_axis, predict(best_model, data.frame(x=x_axis)), col='green')

Output:

best_fit_curve
Output

The plot shows a U-shaped curve with a dip followed by a sharp rise, created by fitting a 4th-degree polynomial to the scatter points.


Explore