
%Gender and Hair Color
Xi =[32  43  16  9];
ni =[87 108 80 25];
pi = Xi./ni     %0.3678    0.3981    0.2000    0.3600
pbar = sum(Xi)/sum(ni)     % 0.3333
chi2 = sum(  (Xi - ni*pbar).^2./(ni * pbar * (1-pbar)) ) %8.9872
pval = 1-chi2cdf(chi2, 4-1)  %0.0295

% In contingency tables  tablerxc([Xi; ni-Xi])  will give chi2=8.9872

 X=[];
for i=1:3
    for j = i+1:4
      X=[X;     i,j, abs(pi(i)-pi(j)), ...
          sqrt(chi2inv(1-0.05, 4-1)) * sqrt(  pi(i)* (1- pi(i))/ni(i) +   pi(j)* (1- pi(j))/ni(j)) , ...
          abs(pi(i)-pi(j)) > ...
          sqrt(chi2inv(1-0.05, 4-1)) * sqrt(  pi(i)* (1- pi(i))/ni(i) +   pi(j)* (1- pi(j))/ni(j)) ];   
    end
end
X
%         p(i)       p(j)         |p(i)-p(j)|    crit       different
%     1.0000    2.0000    0.0303     0.1955      0
%     1.0000    3.0000    0.1678     0.1911      0
%     1.0000    4.0000    0.0078     0.3048      0
%     2.0000    3.0000    0.1981     0.1816      1
%     2.0000    4.0000    0.0381     0.2989      0
%     3.0000    4.0000    0.1600     0.2961      0

