Non-Linear Regression in R
Last Updated :
17 Apr, 2025
Non-Linear Regression is a statistical method that is used to model the relationship between a dependent variable and one of the independent variable(s). In non-linear regression, the relationship is modeled using a non-linear equation. This means that the model can capture more complex and non-linear relationships between the variables, but also requires more computational resources and a more sophisticated approach to estimate the model's parameters.
We will implement non-linear regression model in R programming language. Before starting, make sure the following packages are installed:
install.packages("minpack.lm")
install.packages("ggplot2")
Examples of Non-Linear Regression
We will look at some example implementation of Non-Linear Regression in R using different models like exponential, polynomial (quadratic and cubic) and visualize them.
Example 1
This example demonstrates exponential regression in R using the ggplot2
and nls
packages. We begin by loading the necessary libraries and generating sample data for the independent variable x
and the dependent variable y
. The model is then fitted using the nls()
function, which performs non-linear least squares estimation. In this case, we define the relationship between x
and y
using the formula:
y= a\cdot\exp(b\cdot x)
This represents an exponential function with parameters a
and b
. Initial values for these parameters are provided via the start
argument as list(a = 4, b = 2)
to guide the fitting process.
R
library(minpack.lm)
library(ggplot2)
x <- c(0, 1, 2, 3, 4, 5)
y <- c(1, 2, 4, 8, 16, 32)
start_values <- c(a=4, b=2)
fit <- nls(y ~ a * exp(b * x),
start = start_values,
algorithm = "port",
control = nls.control(maxiter = 1000))
summary(fit)
Output:
Formula: y ~ a * exp(b * x)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a 1.000e+00 6.277e-14 1.593e+13 <2e-16 ***
b 6.931e-01 1.331e-14 5.206e+13 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.247e-13 on 4 degrees of freedom
Algorithm "port", convergence message: absolute function convergence (6)
Plot the Exponential Regression line with points
R
ggplot(data.frame(x, y), aes(x, y)) +
geom_point() +
geom_line(aes(x, predict(fit, newdata = data.frame(x)))) +
ggtitle("Exponential Regression") +
xlab("x") +
ylab("y")
Output:
Exponential Regression model generated using random dataExample 2
This example illustrates polynomial regression of degree 2 in R, using the ggplot2
package for visualization and the lm()
function from base R to fit the model. The independent variable x
is defined as a sequence of integers from 1 to 10, while the dependent variable y
is generated using the equation:
y=x^2+x+2+\text{random noise}
Random noise is introduced using the rnorm()
function with a mean of 0 and a standard deviation of 10 to simulate real-world variability. A data frame df
is created containing the x
and y
values. The polynomial regression model is then fitted using lm(y ~ poly(x, 2), data = df)
, where poly(x, 2)
indicates a second-degree polynomial.
R
#data
x <- 1:10
y <- x^2 + x + 2 + rnorm(10, 0, 10)
df <- data.frame(x, y)
#fitting the model
fit <- lm(y ~ poly(x, 2), data = df)
summary(fit)
Output:
Call:
lm(formula = y ~ poly(x, 2), data = df)
Residuals:
Min 1Q Median 3Q Max
-9.975 -5.664 2.832 5.523 6.885
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 47.140 2.395 19.686 2.18e-07 ***
poly(x, 2)1 85.981 7.573 11.354 9.21e-06 ***
poly(x, 2)2 18.278 7.573 2.414 0.0465 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 7.573 on 7 degrees of freedom
Multiple R-squared: 0.9506, Adjusted R-squared: 0.9365
F-statistic: 67.37 on 2 and 7 DF, p-value: 2.676e-05
Plot the regression line
R
library(ggplot2)
#plotting the model
ggplot(df, aes(x, y)) +
geom_point() +
geom_line(aes(x, predict(fit))) +
ggtitle("Polynomial Regression")
Output:
Polynomial Regression generated for numbers 1 to 10Example 3
This example illustrates cubic regression in R, using the ggplot2
package for visualization and the lm()
function from base R to fit the model. The independent variable x
is defined as a sequence of integers from 1 to 10, while the dependent variable y
is generated using the equation:
y = x^3 - 2x^2 + x + 2 + \text{random noise}
Random noise is introduced using the rnorm()
function with a mean of 0 and a standard deviation of 10 to simulate real-world variability.
R
x <- 1:10
y <- x^3 - 2 * x^2 + x + 2 + rnorm(10, 0, 10)
df <- data.frame(x, y)
fit <- lm(y ~ poly(x, 3), data = df)
summary(fit)
Output:
Call:
lm(formula = y ~ poly(x, 3), data = df)
Residuals:
Min 1Q Median 3Q Max
-11.268 -7.677 -4.150 7.141 16.517
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 234.036 3.826 61.169 1.28e-09 ***
poly(x, 3)1 748.529 12.099 61.867 1.20e-09 ***
poly(x, 3)2 328.759 12.099 27.172 1.64e-07 ***
poly(x, 3)3 61.231 12.099 5.061 0.00231 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 12.1 on 6 degrees of freedom
Multiple R-squared: 0.9987, Adjusted R-squared: 0.998
F-statistic: 1530 on 3 and 6 DF, p-value: 4.86e-09
Plot the regression line
R
#plotting the regression line
library(ggplot2)
ggplot(df, aes(x, y)) +
geom_point() +
geom_line(aes(x, predict(fit))) +
ggtitle("Cubic Regression")
Output:
Cubic regression x from 1 to 10Example 4
This example illustrates quadratic regression in R, using the ggplot2
package for visualization and the lm()
function from base R to fit the model. The independent variable x
is defined as a sequence of integers from 1 to 10, while the dependent variable y
is generated using the equation:
y = x^2 + 2x + 2 + \text{random noise}
Random noise is introduced using the rnorm()
function with a mean of 0 and a standard deviation of 10 to simulate real-world variability
R
x <- 1:10
#quadratic equation
y <- x^2 + 2 * x + 2 + rnorm(10, 0, 10)
df <- data.frame(x, y)
fit <- lm(y ~ poly(x, 2), data = df)
summary(fit)
Output:
Call:
lm(formula = y ~ poly(x, 2), data = df)
Residuals:
Min 1Q Median 3Q Max
-13.2189 -7.5274 0.0416 5.6517 16.7008
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 51.901 3.384 15.339 1.21e-06 ***
poly(x, 2)1 102.037 10.700 9.536 2.92e-05 ***
poly(x, 2)2 19.363 10.700 1.810 0.113
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 10.7 on 7 degrees of freedom
Multiple R-squared: 0.9308, Adjusted R-squared: 0.9111
F-statistic: 47.11 on 2 and 7 DF, p-value: 8.699e-05
Plot the regression line
R
#import libraries
library(ggplot2)
#plotting the model
ggplot(df, aes(x, y)) +
geom_point() +
geom_line(aes(x, predict(fit))) +
ggtitle("Quadratic Regression")
Output:
Quadratic equation with x equal to 1 through 10
Similar Reads
Simple Linear Regression in R
Regression shows a line or curve that passes through all the data points on the target-predictor graph in such a way that the vertical distance between the data points and the regression line is minimum What is Linear Regression?Linear Regression is a commonly used type of predictive analysis. Linea
12 min read
Local Regression in R
In this article, we will discuss what local regression is and how we implement it in the R Programming Language. What is Local Regression in R?Local regression is also known as LOESS (locally estimated scatterplot smoothing) regression. It is a flexible non-parametric method for fitting regression m
4 min read
Simple Linear Regression in Python
Simple linear regression models the relationship between a dependent variable and a single independent variable. In this article, we will explore simple linear regression and it's implementation in Python using libraries such as NumPy, Pandas, and scikit-learn.Understanding Simple Linear RegressionS
7 min read
Normal Equation in Linear Regression
Linear regression is a popular method for understanding how different factors (independent variables) affect an outcome (dependent variable. At its core, linear regression aims to find the best-fitting line that minimizes the error between observed data points and predicted values. One efficient met
8 min read
Assumptions of Linear Regression
Linear regression is the simplest machine learning algorithm of predictive analysis. It is widely used for predicting a continuous target variable based on one or more predictor variables. While linear regression is powerful and interpretable, its validity relies heavily on certain assumptions about
7 min read
How to Plot the Linear Regression in R
In this article, we are going to learn to plot linear regression in R. But, to plot Linear regression, we first need to understand what exactly is linear regression. What is Linear Regression?Linear Regression is a supervised learning model, which computes and predicts the output implemented from th
8 min read
Solving Linear Regression in Python
Linear regression is a widely used statistical method to find the relationship between dependent variable and one or more independent variables. It is used to make predictions by finding a line that best fits the data we have. The most common approach to best fit a linear regression model is least-s
3 min read
Regression in machine learning
Regression in machine learning refers to a supervised learning technique where the goal is to predict a continuous numerical value based on one or more independent features. It finds relationships between variables so that predictions can be made. we have two types of variables present in regression
5 min read
Non-Linear Regressions with Caret Package in R
Non-linear regression is used to fit relationships between variables that are beyond the capability of linear regression. It can fit intricate relationships like exponential, logarithmic and polynomial relationships. Caret, a package in R, offers a simple interface to develop and compare machine lea
3 min read
Linear Regression and Group By in R
Linear regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables. In R programming language it can be performed using the lm() function which stands for "linear model". Sometimes, analysts need to apply linear regression sepa
3 min read