R – NonLinear Least Square
In non-linear function, the points plotted on the graph are not linear and thus, do not give a curve or line on the graph. So, non-linear regression analysis is used to alter the parameters of the function to obtain a curve or regression line that is closed to your data.
To perform this,
Non-Linear Least Square approach is used to minimize the total sum of squares of residual values or error values i.e., the difference between vertical points on the graph from regression line and will fit the non-linear function accordingly.
Mathematical Formula:
where,
r is residual or error value between 2 points.
Above mathematical function to find minimum residual function can be performed in R using
resid()
function.
Regression analysis is widely used in all types of business issues to perform a smart decision or predicting the future by altering a factor of their business.
In R language, Non-linear Least Square function is represented as –
Syntax:
nls(formula, start)
where,
formula indicates the model formula i.e., non-linear function
start is a list of starting estimates
Note: To know about more optional parameters of nls(), use below command in R console –
help("nls")
Example 1 :
In this example, a non-linear function is taken and plotted on the graph as points.
x < - seq( 0 , 10 , 0.1 )
y < - rnorm( 101 , 5 , 1 )
png( file = "nls.png" )
m < - nls(y~a * x ^ 3 + b * x + c,
start = list (a = 1 , b = 2 , c = 1 ))
plot(x, y, col.lab = "darkgreen" ,
col.axis = "darkgreen" )
lines(x, predict(m))
dev.off()
print ( sum (resid(m)^ 2 ))
|
Output :
[1] 106.4507
Example 2 :
In this example, below code is accepting a non-linear function as shown:
Further plotting the points and regression line and also, finding out the goodness of fit also by using
cor() method.
x < - seq( 0 , 100 , 1 )
y< - ((runif( 1 , 10 , 20 ) * x) / (runif( 1 , 0 , 10 ) + x)) +
rnorm( 101 , 0 , 1 )
png( file = "nls2.png" )
m< - nls(y~a * x / (b + x), start = list (a = 1 , b = 2 ))
cor(y, predict(m))
sum (resid(m)^ 2 )
plot(x, y)
lines(x, predict(m))
dev.off()
|
Output :
[1] 0.9622681
[1] 108.1481
Last Updated :
22 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...