clear all
close all

load 'C:\BESTAT\Descriptive\Descriptivedat\dmd.mat'
alpha = 0.05;
hemo = dmd( dmd(:,6)== 1, 3);
n=length(hemo)  %n=67
xbar = mean(hemo) %xbar = 92.9303
s2  = var(hemo)
s = sqrt(s2)
mu0 = 85
t = (xbar-mu0)/sqrt(s2/n)


%(a) 
[xbar - tinv(1-alpha/2,n-1) * s/sqrt(n),... 
    xbar + tinv(1-alpha/2,n-1) * s/sqrt(n)] 
%[90.5258   95.3347]

%(b)
tcrit = tinv(1-alpha,n-1) %1.6683
 
pval = 1-tcdf(t, n-1) 
%pval =  4.4003e-009

%(c)
mu1 =89;
pow1 = @(n) nctcdf( -tinv(1-alpha, n-1), n-1,-abs(mu0-mu1)*sqrt(n)/s);
pow1(n)
% 0.9497

beta = 0.01;
pow2 = @(n) nctcdf( -tinv(1-alpha, n-1), n-1,-abs(mu0-mu1)*sqrt(n)/s) - (1-beta)
%(d)

approxssize =  s^2 *(norminv(1-alpha) + norminv(1-beta))^2/(mu0 - mu1)^2  %95.7779
exactssize = fzero(pow2, approxssize)  %97.1509





