Regression Method Algorithm Using Least Square Method

 

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.

Regression Method Algorithm Using Least Square Method


 

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

 

This C program implements Linear Regression Method using Least Square Method to find linear equation of best fit.

In this program we first read n data points from user and then we implement linear regression using C programming language as follow:

 

C Program: Linear Regression (Fitting y = a+bx)

#include<stdio.h>

#include<conio.h>

#define S 50

 

int main()

{

 int n, i;

 float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b;

 clrscr();

 /* Input */

 printf("How many data points?\n");

 scanf("%d", &n);

 printf("Enter data:\n");

 for(i=1;i<=n;i++)

 {

  printf("x[%d]=",i);

  scanf("%f", &x[i]);

  printf("y[%d]=",i);

  scanf("%f", &y[i]);

 }

 /* Calculating Required Sum */

 for(i=1;i<=n;i++)

 {

  sumX = sumX + x[i];

  sumX2 = sumX2 + x[i]*x[i];

  sumY = sumY + y[i];

  sumXY = sumXY + x[i]*y[i];

 }

 /* Calculating a and b */

 b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);

 a = (sumY - b*sumX)/n;

 /* Displaying value of a and b */

 printf("Values are: a=%0.2f and b = %0.2f",a,b);

 printf("\nEquation of best fit is: y = %0.2f + %0.2fx",a,b);

 getch();

 return(0);

}

C Program Output: Linear Regression

How many data points?

4

Enter data:

x[1] = 0

y[1] = -1

x[2] = 2

y[2] = 5

x[3] = 5

y[3] = 12

x[4] = 7

y[4] = 20

Values are: a=-1.14 and b=2.90

Equation of best fit is: y = -1.14 + 2.90x

 

Note: indicates ENTER is pressed.


Regression Method Algorithm Using Least Square Method

Solve the initial value problems using Modified Euler's Method.

Integratea function using numerically using trapezoidal Method.

Post a Comment

0 Comments