 
load liver.dat 
data=liver;
% ====== split into training and checking data
fprintf('Doing data preprocessing...\n'); 
% == Data normalization: everything is within [0 1]
%col_min = min(data);
%col_range = range(data);
%data_n = size(data, 1);
%tmp = ones(data_n, 1);
%normalized_data = (data-col_min(tmp, :))./col_range(tmp, :);

% Get training and checking data
%trn_data = normalized_data(1:2:size(data, 1), :);
%chk_data = normalized_data(2:2:size(data, 1), :);
trn_data = data(1:2:size(data, 1), :);
chk_data = data(2:2:size(data, 1), :);

% ====== Get variable names
%   1. mpg:           continuous
%   2. cylinders:     multi-valued discrete
%   3. displacement:  continuous
%   4. horsepower:    continuous
%   5. weight:        continuous
%   6. acceleration:  continuous
%   7. model year:    multi-valued discrete
%   8. origin:        multi-valued discrete
%   9. car name:      string (unique for each instance)

input_name = str2mat('mcy', 'alkphos', 'sgpt', 'sgot', ...
	'gammagt', 'drinks' );

% ====== Do linear regression
fprintf('Doing linear regression...\n'); 
fprintf('Linear regression with %d linear parameters:\n', size(data, 2)); 
A_trn = [trn_data(:, 1:size(data,2)-1) ones(size(trn_data,1), 1)]; 
B_trn = trn_data(:, size(data,2));
coef = A_trn\B_trn;
trn_error = norm(A_trn*coef-B_trn)/sqrt(size(trn_data,1));

A_chk = [chk_data(:, 1:size(data,2)-1) ones(size(chk_data,1), 1)]; 
B_chk = chk_data(:, size(data,2));
chk_error = norm(A_chk*coef-B_chk)/sqrt(size(chk_data,1));

fprintf('\tRMSE for training data: %g\n', trn_error); 
fprintf('\tRMSE for checking data: %g\n', chk_error); 
