[Fat Age Wt Ht Neck Chest Waist Hip Thigh Knee Ankle Bicep Forearm Wrist] ...
    = textread('bodyfat.txt','%f%d%f%f%f%f%f%f%f%f%f%f%f%f','headerlines',1);

x = Wt;
y = Fat;
n = length(y);
b1 = corr(x,y)*std(y)/std(x)
b0 = mean(y) - b1*mean(x)

x = Ht;
y = Fat;
n = length(y);
b1 = corr(x,y)*std(y)/std(x)
b0 = mean(y) - b1*mean(x)

A = [ones(n,1) Wt Ht];
b = A\y

figure(1);
plot3(Ht,Wt,Fat,'k.');
alpha(1.0);
%set(gca,'dataaspectratio',[1 1 0.5],'projection','perspective','box','on') 
set(gca,'projection','perspective','box','on') 
xlabel('Height','fontsize',14,'fontweight','bold','color',[1 0 0]) 
ylabel('Weight','fontsize',14,'fontweight','bold','color',[0 0 0]) 
zlabel('Fat','fontsize',14,'fontweight','bold','color',[0 0 1]) 
h = rotate3d; 
set(h,'ActionPostCallback',@align_axislabels) 

hold on;
xx = [min(Ht) max(Ht)]'*[1 1];
yy = [1  1]'*[min(Wt) max(Wt)];
zz = b(1) + b(3)*xx + b(2)*yy;
surf(xx,yy,zz);
alpha(0.5);
hold off;
title('Figure 4.8');


A = [ones(n,1) Wt];
b = A\Ht;
ht_others = Ht - A*b;
b = A\Fat;
fat_others = Fat - A*b;
figure(2);
plot(ht_others, fat_others, 'ko');
hold on;
A = [ones(n,1) ht_others];
b = A\fat_others
h1 = min(ht_others);
h2 = max(ht_others);
plot([h1 h2], [b(1)+b(2)*h1 b(1)+b(2)*h2], 'r-');
hold off;
xlabel('Height | others');
ylabel('Fat | others');
title('Figure 4.9 -- Added Variable Plot');

A = [ones(n,1) Ht];
b = A\Wt;
ht_others = Wt - A*b;
b = A\Fat;
fat_others = Fat - A*b;
figure(3);
plot(ht_others, fat_others, 'ko');
hold on;
A = [ones(n,1) ht_others];
b = A\fat_others
h1 = min(ht_others);
h2 = max(ht_others);
plot([h1 h2], [b(1)+b(2)*h1 b(1)+b(2)*h2], 'r-');
hold off;
xlabel('Weight | others');
ylabel('Fat | others');
title('Figure 4.9 -- Added Variable Plot');

corr_Age = corr(Fat, Age )
corr_Ht = corr(Fat, Ht )
corr_Wt = corr(Fat, Wt )
corr_Neck = corr(Fat, Neck )
corr_Chest = corr(Fat, Chest )
corr_Waist = corr(Fat, Waist )
corr_Hip = corr(Fat, Hip )
corr_Thigh = corr(Fat, Thigh )
corr_Knee = corr(Fat, Knee )
corr_Ankle = corr(Fat, Ankle )
corr_Bicep = corr(Fat, Bicep )
corr_Forearm = corr(Fat, Forearm )
corr_Wrist = corr(Fat, Wrist )

sprintf('Correlations:\n Age\t=%7.3f\n Ht\t=%7.3f\n Wt\t=%7.3f\n Neck\t=%7.3f\n Chest\t=%7.3f\n Waist\t=%7.3f\n Hip\t=%7.3f\n Thigh\t=%7.3f\n Knee\t=%7.3f\n Ankle\t=%7.3f\n Bicep\t=%7.3f\n Forearm\t=%7.3f\n Wrist\t=%7.3f\n ', ...
corr_Age, corr_Ht, corr_Wt, corr_Neck, corr_Chest, corr_Waist, corr_Hip, ...
corr_Thigh, corr_Knee, corr_Ankle, corr_Bicep, corr_Forearm, corr_Wrist)

A = [ones(n,1) ...
     Age ...
     Ht ...
     Wt ...
     Neck ...
     Chest ...
     Waist ...
     Hip ...
     Thigh ...
     Knee ...
     Ankle ...
     Bicep ...
     Forearm ...
     Wrist ...
     ];
b = A\Fat
R2 = 1 - sum((Fat-A*b).^2)/sum((Fat-mean(Fat)).^2)

A = [ones(n,1) ...
     (Age-mean(Age))/std(Age)...
     (Ht-mean(Ht))/std(Ht)...
     (Wt-mean(Wt))/std(Wt)...
     (Neck-mean(Neck))/std(Neck)...
     (Chest-mean(Chest))/std(Chest)...
     (Waist-mean(Waist))/std(Waist)...
     (Hip-mean(Hip))/std(Hip)...
     (Thigh-mean(Thigh))/std(Thigh)...
     (Knee-mean(Knee))/std(Knee)...
     (Ankle-mean(Ankle))/std(Ankle)...
     (Bicep-mean(Bicep))/std(Bicep)...
     (Forearm-mean(Forearm))/std(Forearm)...
     (Wrist-mean(Wrist))/std(Wrist)...
     ];
b = A\Fat
R2 = 1 - sum((Fat-A*b).^2)/sum((Fat-mean(Fat)).^2)

A = [ones(n,1) ...
     (Waist-mean(Waist))/std(Waist)...
     (Wrist-mean(Wrist))/std(Wrist)...
     ];
b = A\Fat
R2 = 1 - sum((Fat-A*b).^2)/sum((Fat-mean(Fat)).^2)

A = [ones(n,1) ...
     (Age-mean(Age))/std(Age)...
     (Ht-mean(Ht))/std(Ht)...
     (Wt-mean(Wt))/std(Wt)...
     ];
b = A\Fat
R2 = 1 - sum((Fat-A*b).^2)/sum((Fat-mean(Fat)).^2)
