%  Cell Counts. 
% In a quantitative engineering physiology laboratory, a team of four students was
% asked to make a LabVIEW program to automatically count cells in a hemocytometer
% 
%  This automatic count was to be compared with the manual count collected through an inverted bright field microscope. The  manual count  is considered
%  a gold standard.
% 
% 
%    The experiment consisted of placing 10 $\mu$L of cell solutions at
%   two levels of cell confluency:   20\% and 70\%.
%  There were $n_1 = 12$ pairs of measurements (automatic and manual counts) 
%  at 20\% and $n_2 = 10$ pairs  at 70\%.
%  
% (a) Are the automated and manual counts significantly different
% for a fixed confluency level? What are the confidence intervals for the
% population differences if a normality of measurements is assumed?
% 
% (b) If the difference between automated and manual counts constitutes an
% error, are the errors comparable for the two confluency levels?
% 
% %Exercise Cell Counts
A20 =[34 44 40 62 53 51 30 33 38 51 26 48];
M20 =[30 43 34 53 49 39 37 42 30 50 35 54];
A70 =[72 82 100 94 83 94 73 87 107  102];
M70 =[76 51  92 77 74 81 72 87 100  104];
n1 = length(A20);  n2 = length(A70);
md20 = mean(A20-M20)
sd20=std(A20-M20) 
md70 = mean(A70-M70) 
sd70=std(A70-M70) 


%(a)
t1 = md20/(sd20/sqrt(n1))   %0.5520
t2 = md70/(sd70/sqrt(n2))   %2.4072
pval1=2 * tcdf(-abs(t1), n1-1) %0.5920
pval2=2 * tcdf(-abs(t2), n2-1) %0.0394


%(b)
[md20 - tinv(0.975, n1-1)*sd20/sqrt(n1)   ...
    md20 + tinv(0.975, n1-1)*sd20/sqrt(n1)]
    %-3.4853    5.8186

[md70 - tinv(0.975, n2-1)*sd70/sqrt(n1)  ...
    md70 + tinv(0.975, n2-1)*sd70/sqrt(n1)]
    %1.1372   14.8628
    
%(c)
s2p = ((n1-1)*sd20 + (n2-1)*sd70)/(n1 + n2 -2)
t0 = (md20 - md70)/sqrt(s2p * (1/n1 + 1/n2))
 %-5.3933 -- significant