www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ComputeSampleSizeForABinomialTestExample.m

    %% Compute Sample Size for a Binomial Test
% Compute the sample size, _n_, required to distinguish _p_ = 0.30 from _p_ =
% 0.36, using a binomial test with a power of 0.8.

% Copyright 2015 The MathWorks, Inc.

napprox = sampsizepwr('p',0.30,0.36,0.8)

%%
% The result indicates that a power of 0.8 requires a sample size of 485.
% However, this result is approximate.

%%
% Make a plot to see if any smaller _n_ values provide the required power
% of 0.8.
nn = 1:500;
pwrout = sampsizepwr('p',0.3,0.36,[],nn);
nexact = min(nn(pwrout>=0.8))

figure
plot(nn,pwrout,'b-',[napprox nexact],pwrout([napprox nexact]),'ro')
grid on

%%
% The result indicates that a sample size of 462 also provides a power of
% 0.8 for this test.