clear all
close all


 disp('Multivariate Data: Body Fat Example')
 lw = 3; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 15;
 msize = 10;


load('C:\BESTAT\Descriptive\Descriptivedat\fat.dat')
 % load('fat.dat')
casen = fat(:,1);
broz = fat(:,2);
siri = fat(:,3); 
densi = fat(:,4); 
age = fat(:,5);
weight = fat(:,6);   
height = fat(:,7);   
adiposi = fat(:,8); 
ffwei = fat(:,9);   
neck = fat(:,10); 
chest = fat(:,11);
abdomen  = fat(:,12);   
hip = fat(:,13); 
thigh = fat(:,14);
knee = fat(:,15);   
ankle = fat(:,16);
biceps = fat(:,17);
forearm = fat(:,18);     
wrist = fat(:,19);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = [broz  densi weight adiposi biceps];

varNames = {'broz'; 'densi'; 'weight'; 'adiposi'; 'biceps'};

% covariances and correlations

% mean, covariance and correlation
%              matrices
Xbar = mean(X)
S = cov(X)
R = corr(X)

% S, R  by ``hand''
[n p]=size(X);
H = eye(n) - 1/n * ones(n,1)*ones(1,n);
S = 1/(n-1) * X' * H * X;
stds = sqrt(diag(S));
D = diag(stds);
R = inv(D) * S * inv(D);
%S and R here coincide with S and R 
% calculated by built in finctions cov and cor.


Xc= X- repmat(mean(X),n,1);  %center X
% subtract component means from variables in each observation.

% there are two interesting transformations
Y =  Xc * inv(D); % the scaling transform for 
% which S=R
cov(Y)

% and Mahalanobis transform 
M = sqrtm(inv(S))  %sqrtm is a sqare root of matrix

 Z = Xc * M; %Z has uncorrelated components

cov(Z) %should be identity matrix

figure(1)
imagesc(X)
colorbar
%print -depsc 'C:\Springer\Descriptive\Descriptiveeps\fat5X.eps'

figure(2)
imagesc(Y)
colorbar
%print -depsc 'C:\Springer\Descriptive\Descriptiveeps\fat5Y.eps'


figure(3)
imagesc(Z)
colorbar
% print -depsc 'C:\Springer\Descriptive\Descriptiveeps\fat5Z.eps'

