% LMex110501_4th.m
% Page 722-723 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu 11/28/10; revised 12/12/10
% Butterfat content vs. temperature
X=[64 65 65 64 61 55 39 41 46 59 56 56 62 37 37 45 57 58 60 55]';
Y=[4.65 4.58 4.67 4.60 4.83 4.55 5.14 4.71 4.69 4.65 4.36 4.82 4.65 ...
   4.66 4.95 4.60 4.68 4.65 4.60 4.46]';
plot(X,Y,'ok');axis([50 70 4.3 4.9]), xlabel('Temperature')
ylabel('Butterfat Content');figure(gcf)
C=corrcoef(X,Y);
r=C(1,2);
[n,c]=size(X);
t=sqrt(n-2)*r/sqrt(1-r^2);
if t<0
    p=2*tcdf(t,n-2);
else
    p=2*(1-tcdf(t,n-2));
end
fprintf('\nPearson''s r=%4.2f, 2-tailed P(t>|%4.2f| |Ho)= %6.3g\n',C(1,2),t,p)
% Fisher's approach
z=1/2*log((1+r)/(1-r))/sqrt(1/(n-3));
if z<0
    P= 2*normcdf(z);
else
    p=2*(1-normcdf(z));
end
fprintf('\nThe two-tailed Fisher''s approximate p = % 6.3g\n',P)

[r,pval] = corr(X,Y,'type','Pearson','tail','both');
fprintf('\nPearson''s r=%4.2f with 2-tailed p = %6.3g\n',r,pval)
[rho,pvalrho] = corr(X,Y,'type','Spearman','tail','both');
fprintf('\nSpearman''s rho is %4.2f with 2-tailed p=%4.3f\n',rho, pvalrho)
[tau,pvaltau] = corr (X,Y,'type','Kendall','tail','both');
fprintf('\nKendall''s tau is %4.2f with 2-tailed p=%4.3f\n',tau, pvaltau);

% Call's Gallagher's Kendall's tau
[TAU,PROB,S,Z,E]=kendall(X,Y);
fprintf('\n Kendall''s tau is %4.2f with 2-tailed p=%4.3f\n',TAU, 2*PROB)