%
clear all
close all


disp('Difference in Proportions from a Single Sample')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 15);
  fs = 15;
  msize = 10;
% end of preamble--------------------------------------
% In a sample of 578 school age children 466 are vaccinated against rubella
% and 477 against measles. 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 rubella and measles. This number is 458.


%
i=9
a=466+1-i; b=i-1; c=11-1+i; d=101+1-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


CI1  = [ diff - 1.96 * sqrt(s2),   diff + 1.96 * sqrt(s2) ]

df=p2-p1
ss2= p1*(1-p1)/n + p2*(1-p2)/n - 2 * (p11*p22 - p21*p12)/n


s2ind = p1*(1-p1)/n + p2*(1-p2)/n

sind = sqrt(s2ind)

CI1  = [ diff - 1.96 * sqrt(s2ind),   diff + 1.96 * sqrt(s2ind) ]

ub=[]; lb=[];
% fixed marginals
  for i=1:102
a=466+1-i; b=i-1; c=11-1+i; d=101+1-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) ];

  end
  
  close all
  range=(0:101) + 365;
  plot(range, lb,'k-','LineWidth',2)
  hold on
   plot(range, ub,'k-','LineWidth',2)
   plot(range, 0*range,'k-')
   xlabel('# of children vaccinated against both rubella and measles')
   ylabel('Upper and lower bound of 95% CI')
%   print -depsc 'C:\BESTAT\Two\Twoeps\vaccination2.eps'
   
   
   