%
clear all
close all


disp('Difference in Proportions from a Single Sample')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 15);
  fs = 15;
  msize = 10;
% In a sample of 578 school age children 466 are vaccinated against
% Varicella and 477 against Hepatitis B . We are interested in inference
% about the difference of the corresponding population proportions.
 
%In the case of single sample we need additional info: # of children
%vaccinated against both Varicella and Hepatitis B . This number is 458.


ub=[]; lb=[];
% fixed marginals
  for i=0:101
a=466-i; b=i; c=11+i; d=101-i;
n= a+b+c+d;
p1=(a+b)/n;
p2=(a+c)/n;
p11= a/n;
p12 = b/n;
p21= c/n;
p22=d/n;
diff= p21 - p12;    
s2 = (p21  +  p12  - (p21 - p12)^2)/n;
lb=[lb     diff - 1.96 * sqrt(s2)];
ub=[ub    diff + 1.96 * sqrt(s2) ];
  if (i == 8) 
      s2 = (p21  +  p12)/n;
      pval = 2* normcdf( - abs(diff)/sqrt(s2))
      a
  end
  end
     close all
  range= 466  - (0:101);
  plot(range, lb,'ko','MarkerSize',5)
  hold on
   plot(range, ub,'ko','MarkerSize',5)
   plot(range, 0*range,'k-')
   plot(466:-1:455, ub(1:12),'ro','MarkerSize',5)
   plot(466:-1:455, lb(1:12),'ro','MarkerSize',5)
   xlabel('Vaccinated against both Hepatitis B and Varicella')
   ylabel('95% CI')
  % print -depsc 'C:\STAT\Two\Twoeps\vaccination2.eps'
 
   
