%FatAnalysis - Math 151 
%first read in data from a file
%   load fatdead.dat
% which contains data on fat intake and
% artherosclerosis death rate in Norway during
% World War two (vectors are f, d and y for years)
%then read in this file of commands for MATLAB
who,pause
plot(y,f,'+')
title('fat intake versus year'),pause
plot(y,d,'+')
title('death rate versus year'),pause
plot(y,f,'+', y,d,'og')
title('both versus year'),pause
plot(f,d,'+')
title('fat intake versus death rate'),pause
%Now compute the regression line and plot it 
c=polyfit(f,d,1),pause
m=[min(f) max(f)],pause
x=polyval(c,m),pause
plot(f,d,'+',m,x),pause
% Now compute the Sum of the Square Errors for Regression SSR
% and the Total Sum of Square Errors TSS
% and from this compute RSquared as SSR/TSS
%First compute the linear estimates for death rates
% for the fat intakes in the vector f - call these dhat
dmean=mean(d)
dhat=polyval(c,f)
SSR=sum((dhat-dmean).^2),pause
TSS=sum((d-dmean).^2),pause
RSquared=SSR/TSS,pause
R=sqrt(RSquared),pause
% Now compute the correlation coefficient and see that 
% it gives R
corrcoef(f,d), pause

