% Hypersplenism  and White Blood Cell Count.
% Hypersplenism is a disorder that causes the spleen 
% to rapidly and prematurely destroy blood cells.
% In the general population the count of white
% blood cells is normal with the mean of
% $7200/mm^3$ and  standard deviation $\sigma = 1500.$
% 
% It is believed that hypersplenism decreases the leukocyte count.
% 
%  (a) If in a sample of 16  persons affected by hypersplenism
% the mean white blood cell count
% was found to be ${\bar X}= 5213/mm^3.$
% The sample standard deviation was $s = 1682/mm^3.$ Test
%  $H_0: \mu = 7200$ versus the alternative $H_1: \mu < 7200.$ 

%  (b) Find the power of the test against the alternative $H_1: \mu = 6000.$
% 
%  (c) What sample size is needed if the
%   effect $|mu1 - mu0|/sigma = 0.4$ is to be
%  detected with the power of 80\%

n=16; xbar=5213; mu0 = 7200; s=1682; alpha = 0.05;
t = (xbar - mu0)/(s/sqrt(n))
%t = -4.7253
tcdf(t,n-1) %p-value
%ans =  1.3543e-004
tinv(alpha, n-1) %RR bound
%ans = -1.7531

% Find the power against alternative H_1: mu=5800

esizet = abs(7200-6000)/s;
powert = nctcdf( -tinv(1-alpha, n-1), n-1,-esizet*sqrt(n)) 
% powert = 0.8592


%Sample Size
%power 80%, alpha 5% one sided, for difference of size 
% 600. Effect size 600/1682 =0.3567
beta = 0.2;

%Approx sample size 
ss = (norminv(1-beta) + norminv(1-alpha))^2/0.4^2
%ss =   38.6410
  

%Exact sample size
f = @(n)  nctcdf( -tinv(1-alpha, n-1), n-1, -sqrt(n)*0.4 )-(1-beta);
sss = fzero(f,ss)

%sss =  40.0291
  


   


%%


%%

% Example 7.20 fasting serum-cholesterol level among recent Asian
% immigrants compared to typical US
% US women 21-40: mu=190; immigrants mu=? normal.
% Xbar = 181.52, n=100, s = 40 units=[mg/dL]
%

% TEST STATISTICS
tstat=(181.52-190)*10/40
%tstat = -2.12

% alpha = 5%; two sided cut point has
% 2.5% tails:
tinv(0.975,99)
%ans = 1.9842
% reject H_0: since tstat is in RR=(-inf, -1.9842] U [1.9842, inf).

% TESTING USING p-VALUE
% pvalue
p = 2 * tcdf(-abs(tstat),99)

% note 2, since the test is 2 sided.
% p=0.0365  %%For alpha =5% reject H_o since 0.0365 < 0.05
%power
% Find the power against alternative H_1: mu=175
%  Phi( Phi^{-1}(alpha) + |mu_1 - mu_0|/(sigma/sqrt(n))  )
% power = normcdf( norminv(0.025) + 15*10/40)     %% power = 0.9633

power = tcdf( tinv(0.025, 99) + 15*10/40, 99)  %   0.9597

% sample size needed for power of 80%. Normal approximation
% in norminv is used since we do not know n.
% SS = sigma^2 (z_{1-beta} + z_{1-alpha} )^2 / (mu_1 - mu_0)^2 

ss = 40^2 * (norminv(0.8) + norminv(0.975))^2/225
%ss = 55.8143 approx 56


%%%%%
% Assume sigma=40 is known ---> z test
% since n = 100, even if sigma is not known, z test approximates.
%
zstat = (181.52-190)*10/40
% 5% two sided cut point
norminv(0.975)

% pvalue
p = 2 * normcdf(tstat)
%
%power against alternative |mu_1 - mu_0| = 10.
power = normcdf( norminv(0.025) + 15*10/40)
% sample size needed for power of 80%
ss = 40^2 * (norminv(0.8) + norminv(0.975))^2/225
%-----------------------------------------------------------------------

%Example 7.45 HYPERTENSION page 267
diff=[-6 3 2 -3 1 0 -1 1 3 -2];
vd = var(diff)
% statistic chisquare
csq = 9 * vd/35
% rejection region  bounds
chi2inv(0.025,9)
chi2inv(0.975,9)
%
% pvalue
pval = 2 * chi2cdf(2.1029, 9)


%%%%

%Cancer Example 7.47 page 270
% n=10000, prev=0.02; X=400; 

zstat = (400/10000 - 0.02)/sqrt(0.02*0.98/10000)

%cutpts two sided for Rejection Region RR
norminv(0.025)
norminv(0.975)

%
%cutpoint for oneseded alt p>0.02
norminv(0.95)

% pvalue two sided
pvalue = 2 * normcdf(-abs(zstat))

