Linear regression models the relation between an explanatory (independent) variable and a scalar response (dependent) variable by fitting a linear equation.

Image for post

For example, Modeling the weights of Individuals with their heights using a linear equation.

Before trying to model the relationship on the observed data, you should first determine whether there is a linear relation between them or not, usually, the scatter plot can be a helpful tool to view the relation between the data.

Image for post

By Sewaqu — Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=11967659

A linear regression line has an equation of the form Y = a + bX, where X is the explanatory variable and Y is the dependent variable. The slope of the line is, and a is the intercept (the value of y when x = 0).

In this article, We will implement the Simple Linear Regression model. Simple linear regression concerns two-dimensional sample points with one independent variable and one dependent variable and finds a linear function that predicts the dependent variable values as a function of the independent variable.

When you perform a simple linear regression (or any other type of regression analysis), you get a line of best fit. The data points usually don’t fallon this regression equation line; they are scattered around.

residual is a vertical distance between a data point and the regression line. Each data point has one residual. It is positive if it is above the regression line and negative if it is below the regression line. If the regression line passes through the point, the residual at that point is zero.

The main problem here is to minimize the total residual error to find the line of best fit, if you need more explanation on the theory behind the following equations, I recommend reading this article:

Linear Regression — Understanding the Theory

Without going into details, the equations that we should use are:

Image for post

the derivation can be found here

Simply we can divide it into the following for simplicity:

Image for post

Image for post

Image for post


Now we can start going through the implementation of Linear Regression

#statistics #machine-learning #data-science #c++ #cplusplus #programming-c

Linear Regression Implementation In C++
14.85 GEEK