% Example 12.4. Power and Sample Size for 2 x 6 Table
%    (a) Find the power in a 2 × 6 contingency table, for n = 180
%         and w = 0.3 (medium effect).
%
w = 0.3; n = 180; k = (2-1)*(6-1); alpha = 0.05; lambda=n*w^2
pow = 1-ncx2cdf( chi2inv(1-alpha,k), k, lambda) %0.8945
%
%    (b) What sample size ensures the power of 95% in a contingency table
%          2×6, for an effect of w = 0.3 and ? = 0.05?
%
beta = 0.05; alpha = 0.05; k = (2-1)*(6-1); w=0.3;
pf = @(n) ncx2cdf( chi2inv(1-alpha,k), k, n*w^2) - beta;
ssize = fzero(pf, 200) %219.7793 approx 220