How to find simple linear regression?

If you are given a list of X and Y values, that are the coordinates of points on a graph. How do you find a single straight line that fits the points such that the straight line is the best match for the points?

We will make use of two equations:

    \[ \frac{ f - \bar{y}}{s_y} = r_{xy} \frac{ x - \bar{x}}{s_x} \]

and

    \[r_{xy} = \frac{ \overline{xy} - \bar{x}\bar{y} }{ \sqrt{ \left(\overline{x^2} - \bar{x}^2\right)\left(\overline{y^2} - \bar{y}^2\right)} }\]

where
\bar{x} is the average of x values of all coordinates,
\bar{y} is the average of y values of all coordinates,
s_x is the standard deviation of y values of all coordinates,
s_y is the standard deviation of y values of all coordinates,
r_{xy} can be found using the second equation.

Rearranging the first equation, we get

    \[f = \frac{s_y\times r_{xy}}{s_x}x - \frac{s_y\times r_{xy}\times\bar{x}}{s_x} + \bar{y}\]

This is the equation of the linear regression. f and x are variable, and we are finding the gradient, K and intercept, B which are \frac{s_y\times r_{xy}}{s_x} and - \frac{s_y\times r_{xy}\times\bar{x}}{s_x} + \bar{y} respectively.

Reference: https://en.wikipedia.org/wiki/Simple_linear_regression

Leave a Reply