Calculates the linear regression parameters and evaluates the regression line at arbitrary abscissas

View versions (1)

Interface

Overview

Linear regression is a method to best fit a linear equation (straight line) of the form y(x) = a + bx to a collection of points (x_i,y_i) \, 1 \leq i \leq N, where b is the slope and a the intercept on the Y axis.

The algorithm basically requires minimisation of the sum of the squared distance from the data points to the proposed line. This is achieved by calculating the derivative with respect to a and b and setting these to zero.

Let us define the following

S_x = \sum_{i = 0} ^ {N - 1} (x_i - \overline{x}) ^ 2 \qquad
         S_y = \sum_{i = 0} ^ {N - 1} (y_i - \overline{y}) ^ 2
(1)
S_{xy} = \sum_{i = 0} ^ {N - 1} (x_i - \overline{x})(y_i - \overline{y})
(2)

Then the slope is

b = \frac{S_{xy}}{S_{x}}
(3)

the intercept on the Y axis

a = \overline{y} - b \overline{x}
(4)

Below you will find the regression graph for a set of arbitrary points, which were also used in the forthcoming example. The regression line, displayed in red, has been calculated using this class.

1/reglinear-378.png

Example 1

The following example displays the slope, Y intercept and regression coefficient for a certain set of 7 points.

#include <codecogs/maths/approximation/regression/linear.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
    double x[7] = { 1.5, 2.4, 3.2, 4.8,  5.0, 7.0,  8.43 };
    double y[7] = { 3.5, 5.3, 7.7, 6.2, 11.0, 9.5, 10.27 };

    Maths::Regression::Linear A(7, x, y);

    cout << "    Slope = " << A.getSlope() << endl;
    cout << "Intercept = " << A.getIntercept() << endl << endl;

    cout << "Regression coefficient = " << A.getCoefficient() << endl;

    cout << endl << "Regression line values" << endl << endl;
    for (double i = 0.0; i <= 3; i += 0.6) 
    {
        cout << "x = " << setw(3) << i << "  y = " << A.getValue(i);
        cout << endl;
    }
    return 0;

}

Output:

Slope = 0.904273
Intercept = 3.46212

Regression coefficient = 0.808257

Regression line values

x =   0  y = 3.46212
x = 0.6  y = 4.00469
x = 1.2  y = 4.54725
x = 1.8  y = 5.08981
x = 2.4  y = 5.63238
x =   3  y = 6.17494
GPL Licence — free for non commercial use. See Licence details.

Members of Linear

CLASS METHOD

Linear

Initializes the class by calculating the slope, intercept and regression coefficient based on the given constructor arguments.

Parameters

y
The y-coordinates of points
x
The x-coordinates of points
n
The number of initial points in the arrays x and y

The slope should not be infinite.

CLASS METHOD

getValue

Parameters

x
the abscissa used to evaluate the linear regression function

CLASS METHOD

getSlope

CLASS METHOD

getIntercept

CLASS METHOD

getCoefficient

The regression coefficient indicated how well linear regression fits to the original data. It is an expression of error in the fitting and is defined as:

r = \frac{S_{xy}}{\sqrt{S_{x} \cdot S_{y}}}
(5)

This varies from 0 (no linear trend) to 1 (perfect linear fit). If |S_y| = 0 and |S_x| \neq 0, then r is considered to be equal to 1.

CLASS METHOD

Linear_once

This function implements the Linear class for one off calculations, thereby avoid the need to instantiate the Linear class yourself.

Example 1

The following graph fits a straight line to the following values:

x = 1  y = 0.22
x = 2  y = 0.04
x = 3  y = -0.13
x = 4  y = -0.17
x = 5  y = -0.04
x = 6  y = 0.09
x = 7  y = 0.11

\graph n=7 x="1 2 3 4 5 6 7" y="0.22 0.04 -0.13 -0.17 -0.04 0.09 0.11" a=1:7 .input

Parameters

n
The number of initial points in the arrays x and y
x
The x-coordinates of points
y
The y-coordinates of points
a
The x-coordinate for the output location

Returns

the interpolated y-coordinate that corresponds to a.