# MATLAB: Least squares method - linear regression

Page 1 of 1

## 0 Replies - 1662 Views - Last Post: 02 June 2011 - 08:58 AM

### #1 DrAcid Reputation: 1
• Posts: 12
• Joined: 31-May 11

# MATLAB: Least squares method - linear regression

Posted 02 June 2011 - 08:58 AM

Description: Simple way to try this function. 1. Copy the function file to Your {work} directory 2. Input into console: x = 1:1:5; y = [1, 2, 1, 2, 3]; x_new = 6; LinearRegression(x, y, x_new)Everything is provided as help material for the function. Copy the function file to Your {work} directory. For help and detailed information input into console: help LinearRegression
```function[] = LinearRegression(x, y, x_new)
% This function calculates and returns coefficients of
% linear regression, then it plots a graph with given points,
% regression line and points of interest.
%
% Given points (input parameters x and y) are represented by red crosses
% Regression line is blue, whereas points of interest are black circles
%
% Function header can be modified to return B0 and B1 coefficients:
% Just replace the first line with this:
% function[B1, B0] = LinearRegression(x, y, x_new)
%
%---------------------------------------
% Regression line: y = B0 + B1*x
% Coefficients are calculated like this:
%
% B1 = (mean(x.*y) - mean(x)*mean(y)) / (mean(x.^2) - mean(x)^2)
% B0 = mean(y) - B1*mean(x)
%
%-------------[ DISCLAIMER ]------------
% This solution is not optimized neither for speed nor for precision.
% Use is at Your own risk and caution. Authors of this script are not
% responsible for any dead pets or exploded computers. Or anything else.
%
%--------------[ CREDITS ]--------------
% Authors of this simple script are:
%   Giorgi Maghlakelidze
%   Tamar Makharashvili
% June 2011, TSU
%
% Anyone is free to use, edit and distribute this script in any way or
% form they wish. May God be with You!

% this is calculated because it is used multiple times in different formulas
y_mean = mean(y);

B1 = (mean(x.*y) - mean(x)*y_mean) / (mean(x.^2) - mean(x)^2);
B0 = y_mean - B1*mean(x);

% Prepare regression line points for plotting
x1 = (min(x)-mean(x)):0.1:(max(x)+mean(x));
y1 = B0 + B1*x1;

% Find value at points of interest according to the regression line
y_new = B0 + B1*x_new;

% Basic analysis of coorelation
if B1 > 0
disp(['Positive linear coorelation: B1= ',num2str(B1), ' (B1 >0)'])
elseif B1 < 0
disp(['Negative linear coorelation: B1= ',num2str(B1), ' (B1 <0)'])
else
disp('No coorelation: B1 = 0')
end

% Obviuosly: plotting the points :)
plot(x, y, 'xr', x1, y1, x_new, y_new, 'ok')

% Values at given x points according to regression line
y_reg = B0 + B1*x;

% Calculate error (nashti)
e = mean((y - y_reg).^2);

% Calculate determination coefficient
R__2 = sum( (y_mean - y_reg).^2 ) / sum( (y_mean - y).^2 );

% Basic analysis of determination coefficient
if R__2 == 1
disp('Perfect fit: Determination Coeff = 100%')
else
disp([num2str(R__2*100), '% of known points agree with the regr. line'])
disp(['Error: ', num2str(e)])
end
```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }