logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

Linear Regression

This program fits a straight line to a given set of coordinates using the method of least squares ( linear regression ).
 
The coefficients of the line, coefficient of determination, coefficient of correlation and standard error of estimate are calculated. Once the line has been fitted, you may predict values of y for given values of x.

We develop the following Matlab code (note that Matlab has its own built-in functions to make linear regression

easier for all of us, but we'd like to show a step-by-step way to do it, to understand the inner concepts):



function [y0, a, b, r2, r, k2] = lin_reg(x, y, x0)
% Number of known points
n = length(x);

% Initialization
j = 0; k = 0; l = 0; m = 0; r2 = 0;

% Accumulate intermediate sums
j = sum(x);
k = sum(y);
l = sum(x.^2);
m = sum(y.^2);
r2 = sum(x.*y);

% Compute curve coefficients
b = (n*r2 - k*j)/(n*l - j^2);
a = (k - b*j)/n;

% Compute regression analysis
j = b*(r2 - j*k/n);
m = m - k^2/n;
k = m - j;

% Coefficient of determination
r2 = j/m;

% Coefficient of correlation
r = sqrt(r2);

% Std. error of estimate
k2 = sqrt(k/(n-2));

% Interpolation value
y0 = a + b*x0;



If we have the following data available (where every yi  has its correspondent xi):

x = [71  73  64  65  61  70  65  72  63  67  64];
y = [160 183 154 168 159 180 145 210 132 168 141];

we can call the function above in this manner (to obtain interpolated values for x = 70 and x = 72):

[y0, a, b, r2, r, k2] = lin_reg(x, y, 70)
[y0] = lin_reg(x, y, 72)

And the Matlab response is:

y0 =   176.5139
a =   -106.7917
b =      4.0472
r2 =    0.5563
r =     0.7458
k2 =   15.4135
y0 =  184.6083



We can use the 'polyfit' and 'polyval' instructions in Matlab for this purpose, like this:

a = polyfit(x, y, 1)
y0 = polyval(a, 70)
y0 = polyval(a, 72)

Fitting a straight line through the data means thet we want to find the polynomial coefficients of a first order polynomial such that a1xi + a0  gives the best approximation for yi. We find the coefficients with 'polyfit' and evaluate any xi with 'polyval'.

The Matlab results is

a =      4.0472 -106.7917
y0 =   176.5139
y0 =   184.6083

confirming our previous numbers.


 From 'Linear Regression' to home

 From 'Linear Regression' to 'Matlab Cookbook'
     
Top

Simple Interpolation

Video: Curve Fitting




footer for linear regression page