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

    %% Compute Power and Sample Size for One-Sided Test

% Copyright 2015 The MathWorks, Inc.


%%
% An employee wants to buy a house near her office. She decides to
% eliminate from consideration any house that has a mean morning commute
% time greater than 20 minutes. The null hypothesis for this right-sided
% test is H0: $\mu$ = 20, and the alternative hypothesis is HA: $\mu$ > 20.
% The selected significance level is 0.05.

%%
% To determine the mean commute time, the employee takes a test drive from
% the house to her office during rush hour every morning for one week, so
% her total sample size is 5. She assumes that the standard deviation,
% $\sigma$, is equal to 5.

%%
% The employee decides that a true mean commute time of 25 minutes is too
% different from her targeted 20-minute limit, so she wants 
% to detect a significant departure if the true mean is 25 minutes.
% Find the probability of incorrectly concluding that the mean commute time
% is no greater than 20 minutes.

%%
% Compute the power of the test, and then subtract the power from 1 to
% obtain $\beta$.
power = sampsizepwr('t',[20 5],25,[],5,'Tail','right');
beta = 1 - power

%%
% The $\beta$ value indicates a probability of 0.4203 that the
% employee concludes incorrectly that the morning commute is not
% greater than 20 minutes.

%%
% The employee decides tha this risk is too high, and she wants
% no more than a 0.01 probability of reaching an incorrect conclusion.
% Calculate the number of test drives the employee must take to obtain
% a power of 0.99.
nout = sampsizepwr('t',[20 5],25,0.99,[],'Tail','right')

%%
% The results indicate that she must take 18 test drives from a
% candidate house to achieve this power level.

%%
% The employee decides that she only has time to take 10 test drives.
% She also accepts a 0.05 probability of making an incorrect conclusion.
% Calculate the smallest true parameter value that produces a detectable
% difference in mean commute time.
p1out = sampsizepwr('t',[20 5],[],0.95,10,'Tail','right')

%%
% Given the employee's target power level and sample size, her test 
% detects a significant difference from a mean commute time of at least
% 25.6532 minutes.