clear all
close all

% Read file
M = dlmread('decath.txt',' ');

% Use IAAF decathlon point conversion formulas
scores(:,1) = 25.4347.*(18-(M(:,1)./100)).^1.81;  % 100 meter dash
scores(:,2) = 0.14354.*(M(:,2)-220).^1.4;         % Long jump
scores(:,3) = 51.39*(M(:,3)./100-1.5).^1.05;      % Shot put
scores(:,4) = 0.8465*(M(:,4)-75).^1.42;           % High jump
scores(:,5) = 1.53775*(82-(M(:,5)/100)).^1.81;    % 400 meter dash
scores(:,6) = 5.74352*(28.5-(M(:,6)/100)).^1.92;  % 110 meter hurdles
scores(:,7) = 12.91*(M(:,7)/100-4).^1.1;          % Discus throw
scores(:,8) = 0.2797*(M(:,8)-100).^1.35;          % Pole vault
scores(:,9) = 10.14*(M(:,9)/100-7).^1.08;         % Javelin throw
min = floor(M(:,10)/10000);
sec = floor(M(:,10)/100) - 100*min;               % Convert to # seconds
hsec = M(:,10) - 100*sec - 10000*min;
time = 60*min + sec + .01*hsec;
scores(:,10) = 0.03768*(480-time).^1.85;          % 1500 meter dash
scores = floor(scores);

% Calculate total scores
totals = sum(scores,2);

% Find events with maximum correlation
corrs = corrcoef(scores);
corrs = triu(corrs,1);  % Above diagonal
[vals,event1] = max(corrs);
[val,event2] = max(vals);
event1 = event1(event2);

% 2-D scatter plot of shot put versus discus throw
scatter(scores(:,event1),scores(:,event2),'+');
xlabel('Points in shot put')
ylabel('Points in discus')
title('Positive correlation between scores')

% Principal Component Analysis
[pcs,newdata,var,t2] = princomp(scores);
pcs(:,1)                % Display first component

% Factor Analysis
[Loadings,specVar,rot,stats,preds] = factoran(scores,2,'rot','none');
Loadings(:,1)           % Display first factor

% Cluster Analysis - try hierarchical first
Ghier = clusterdata(newdata,3);
figure
gscatter(scores(:,8),totals,Ghier,'brg','+x*')  % Bad grouping
xlabel('Points in pole vault')
ylabel('Total points in decathlon')
title('Poor grouping using hierarchical cluster analysis')

% Cluster Analysis - now try K-means
Gkmns = kmeans(newdata,3,'replicates',10);
figure
gscatter(scores(:,8),totals,Gkmns,'brg','+x*')  % Better grouping
xlabel('Points in pole vault')
ylabel('Total points in decathlon')
title('Better grouping using K-means cluster analysis')