Linear Regression Method Algorithm (Fit y=a+bx Curve)

Regression analysis is basically a set of statistical process for finding relationship among independent variables (explanatory variables) and dependent variable. If we are finding best equation relating these variables then it is known as best fit or curve fitting. When we are finding linear relationship among independent variables and dependent variable then it is called Linear Regression. In linear regression, when there is only one independent variable then the process is known as Simple Linear Regression.

Least Square Regression

The method of least squares is a standard approach in regression analysis to approximate the relation among dependent variable amd independent variables. In the least squares method the unknown parameters are estimated by minimizing the sum of the square of errors between the data and the model.

Simple linear regression has equation having form y = a + bx.

Procedure for Linear Regression (Fitting y = a + bx) using Least Square Method

1. Form normal equations:
   ∑y = na + b ∑x
   ∑xy = a∑x + b∑x2

2. Solve normal equations as simulataneous 
   equations for a and b

3. Substitute the value of a and b in 
   y= a + bx which is required line of best fit.

Linear Regression Algorithm (Fitting y = a + bx)

1. Start

2. Read Number of Data (n)

3. For i=1 to n:
     Read Xi and Yi
   Next i

4. Initialize:
     sumX = 0
     sumX2 = 0
     sumY = 0
     sumXY = 0

5. Calculate Required Sum
   For i=1 to n:
     sumX = sumX + Xi
     sumX2 = sumX2 + Xi * Xi
     sumY = sumY + Yi
     sumXY = sumXY + Xi * Yi
   Next i

6. Calculate Required Constant a and b of y = a + bx:
   b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
   a = (sumY - b*sumX)/n

7. Display value of a and b

8. Stop

Recommended Readings

  1. Linear Regression Method Algorithm
  2. Linear Regression Method Pseudocode
  3. Linear Regression Method Using C Programming
  4. Linear Regression Method Using C++ with Output