clear all
close all
  disp('Cardiac')
  set(0, 'DefaultAxesFontSize', 16);
  lw =2.5;  fs = 16;  msize = 10;


%
% Thirty three patients undergoing cardiac bypass surgery were randomized to one of three groups:
% 
% 1. Patients received a 50% nitrous oxide and 50% oxygen mixture continuously for 24 hours.
% 
% 2. Patients received a 50% nitrous oxide and 50% oxygen mixture only during the operation.
% 
% 3. Patients received no nitrous oxide but received 35-50% oxygen for 24 hours.
% 
% 
% The the measured responses are red cell folate levels (ng/ml) for the three groups 
% after 24 hours ventilation.  The question of interest is whether the three ventilation 
% methods result in a different mean red cell folate level.


   gr1 = [243,   251,   275,   291,   347,   354,   380,   392,   309,   288,   297];
   gr2 = [206,   210,   226,   249,   255,   273,   285,   295,   282,   222,   277 ];
   gr3 = [241,   258,   270,   293,   328,   302,   292,   273,   235,   256,   299 ];  

   data = [... 
   243 1;
   251 1;
   275 1;
   291 1;
   347 1;
   354 1;
   380 1;
   392 1;
   309 1;
   288 1;
   297 1;
   206 2;
   210 2;
   226 2;
   249 2;
   255 2;
   273 2;
   285 2;
   295 2;
   282 2;
   222 2;
   277 2;
   241 3;
   258 3;
   270 3;
   293 3;
   328 3;
   302 3;
   292 3;
   273 3;
   235 3;
   256 3;
   299 3];
   
resp=data(:,1); groups = data(:,2);
[p table stats] = anova1(resp, groups, 'on')

m=stats.means
multcompare(stats) % 1-2; 1-3; 2-3


% 2-1
[m(2)-m(1)-qtukey(30,3,0.95)*stats.s/sqrt(11)  m(2)-m(1)  m(2)-m(1)+qtukey(30,3,0.95)*stats.s/sqrt(11)]
% 3-1
[m(3)-m(1)-qtukey(30,3,0.95)*stats.s/sqrt(11)  m(3)-m(1)  m(3)-m(1)+qtukey(30,3,0.95)*stats.s/sqrt(11)]
% 3-2
[m(3)-m(2)-qtukey(30,3,0.95)*stats.s/sqrt(11)  m(3)-m(2)  m(3)-m(2)+qtukey(30,3,0.95)*stats.s/sqrt(11)]


% Contrast 
c = [1 1 -2];
L = c(1)*m(1) + c(2)*m(2)+c(3)*m(3)
LL= m * c'
stdL = stats.s * sqrt( c(1)^2/11 + c(2)^2/11 + c(3)^2/11 )
t = LL/stdL

%test H_o: alpha * c' = 0  H_1: alpha * c' > 0
1-tcdf(0.3648, 30)

%or 95% confidence interval
[LL -  tinv(0.975, 30)*stdL, LL +  tinv(0.975, 30)*stdL]





