% cohen.m
% 
% Two Sensors.
% A company producing a medical sensor A is applying for FDA approval
% of a new version B. Both sensors A and B are prone to errors, and
% a gold standard is absent.
% The FDA is requesting that the new sensor be comparable to
% the currently used one, and the company decides to include Cohen's 
%  \hat \kappa statistic in the report.
% 
% The experiment consisted of  n=2803  trials and resulted in a paired table \ttmat{[97 11;   6 2689]},
% where 97 was the number of (+,+) outcomes and 2689 the number of (--,--) outcomes.
% 
% Find  Cohen's  \hat \kappa  and 95\% confidence intervals for the population $\kappa$,
% based on the two estimators of variance, Block-Kraemer and Garner.
% 

data =[97 11;   6 2689]
 %data = 
 % 97          11 
 % 6         2689 
a=data(1,1); b=data(1,2); c=data(2,1); d=data(2,2);
apb = a+b; cpd = c+d; apc = a+c; bpd = b+d;
n = a + b + c + d;
 %----------- 
p0 = (a + d)/n;                         %Observed agreement 
pc = (apb*apc + cpd*bpd)/n^2;  %Chance agreement 
Pre  = (2*a + b + c)/(2 * n);   %Prevalence of +'s
pcc = Pre^2 + (1-Pre)^2;       %MLE of chance agreement 
 %------------ 
kappa = 2 * (a * d - b * c)/(apb*bpd + apc*cpd)  %0.9163 
 %or kappa=(p0-pc)/(1-pc) 

kappamle = (4 * (a*d - b*c) - (b -c)^2)/...
  ((2 * a + b + c) * (2 * d + b + c))   %0.9163 
 %or kappamle=(p0-pcc)/(1-pcc) 
 %------------ 
 %Block-Kraemer variance estimator 
varbk = (1- kappa)/n * ( (1- kappa)*(1-2* kappa) + ...
  (kappa * (2 -  kappa)/(2 * Pre * (1-Pre))))  %4.0731e-004 
 %Garner variance estimator 
vargarner = 4/( (1- pc)^2 * n^2 * (1/(a+1) + 1/(b+1) + ...
    1/(c+1) + 1/(d + 1) ) )   %4.0971e-004 
 %------------ 
 %Confidence intervals 
[ kappa - 1.96 * sqrt(varbk) ...
        kappa + 1.96 * sqrt(varbk)]   %0.8767   0.9558 
[ kappa - 1.96 * sqrt(vargarner) ...
    kappa + 1.96 * sqrt(vargarner)]   %0.8766   0.9560 
 

