% Linear Regression with Multiple Variables
% Barukyah Shaparenko and John M. Cimbala

clear all
close all

% Import data from Excel
% NOTE: Excel data file must reside in same folder as Matlab m-file
% Use xlsread or xlsxread

% Consider the function y = 10 + 2*x_1 + 3*x_2 - x_3
% The following table is created using this function:
xls_filename = 'Regression_multiple_variables.xls';
sheet_number = 1;
data_range_1 = 'A8:A16';
data_range_2 = 'B8:B16';
data_range_3 = 'C8:C16';
data_range_4 = 'D8:D16';
x_1 = xlsread(xls_filename,sheet_number,data_range_1);
x_2 = xlsread(xls_filename,sheet_number,data_range_2);
x_3 = xlsread(xls_filename,sheet_number,data_range_3);
y = xlsread(xls_filename,sheet_number,data_range_4);

% Now a linear regression is performed with y 
% as a function of all three independent variables.

% Use Matlab's left divide '\' to solve for coefficients.

N = length(x_1);
X = [ones(N,1) x_1 x_2 x_3];
b = X\y; % Excel Coefficients

% Discussion:
% "Intercept" is the y-intercept.
% "X Variable 1" is the slope of y with respect to the first variable x_1.
% "X Variable 2" is the slope of y with respect to the first variable x_2.
% "X Variable 3" is the slope of y with respect to the first variable x_3.

% So, we write the best-fit equation as:
% y = 10 + 2*x_1 + 3*x_2 - x_3
% [This agrees with the equation we 
% started with, verifying our procedure.]

% -----------------------------------------------

% Excel ANOVA: SS (Sum of Squares)
SS_regression = b'*X'*y - 1/N*(ones(N,1)'*y)^2;
SS_total = y'*y - 1/N*(ones(N,1)'*y)^2;
SS_residual = SS_total - SS_regression;

% Excel Regression Statistics
Multiple_R = sqrt(SS_regression/SS_total);
R_Square = Multiple_R^2;
Adjusted_R_Square = 1-(1-Multiple_R^2)*(N-1)/(N-1-(length(X(1,2:end))));
Standard_Error = sqrt(sum([b(1)+b(2)*x_1+b(3)*x_2+b(4)*x_3-y].^2)/(N-length(X(1,:))));
Observations = length(x_1);

fprintf('\n----------------------\n');
fprintf('Regression Statistics:\n');
fprintf('Multiple R \t\t\t%g\n',Multiple_R);
fprintf('R Square \t\t\t%g\n',R_Square);
fprintf('Adjusted R Square \t%g\n',Adjusted_R_Square);
fprintf('Standard Error \t\t%g\n',Standard_Error);
fprintf('Observations \t\t%g\n',Observations);

fprintf('\nCoefficients:\n');
fprintf('Intercept \t\t%g\n',b(1));
fprintf('X Variable 1 \t%g\n',b(2));
fprintf('X Variable 2 \t%g\n',b(3));
fprintf('X Variable 3 \t%g\n',b(4));
