Code covered by the BSD License  

Highlights from
chi2ssize

from chi2ssize by Antonio Trujillo-Ortiz
Sample size in tests concerning the variance.

chi2ssize(s2,s2e,n,h,beta,alpha)
function [chi2ssize] = chi2ssize(s2,s2e,n,h,beta,alpha)
%Sample size in tests concerning the variance.
%
%   Syntax: function [chi2ssize] = chi2ssize(s2,s2e,n,h,beta,alpha) 
%      
%     Inputs:
%          s2 - sample variance.
%         s2e - expected variance.
%          n - initial sample size.
%          h - hypothesis testing (right-hand = 1; left-hand = 2).
%       beta - Type II error.
%      alpha - significance level (Type I error: default = 0.05).
%     Output:
%          n - sample size required.
%
%    Example: From the example 7.11 from Zar (1999, p. 113) for a right-hand direction
%             hypothesis concerning the variance (h = 1) with a sample variance = 2.6898, 
%             an expected variance = 1.5, an initial sample size = 30 and a significance 
%             level = 0.05. How large is needed to perform the null hypothesis with a 90% 
%             power (beta = 0.1)?
%
%             Calling on Matlab the function: 
%                chi2ssize(2.6898,1.5,30,1,0.1)
%
%       Answer is:
%             It is required a sample size of at least: 51
%

%  Created by A. Trujillo-Ortiz and R. Hernandez-Walls
%             Facultad de Ciencias Marinas
%             Universidad Autonoma de Baja California
%             Apdo. Postal 453
%             Ensenada, Baja California
%             Mexico.
%             atrujo@uabc.mx
%  March 29, 2003.
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A. and R. Hernandez-Walls. (2003). chi2ssize: Sample 
%    size in tests concerning the variance. A MATLAB file. [WWW document]. 
%    URL http://www.mathworks.com/matlabcentral/fileexchange/
%    loadFile.do?objectId=3274&objectType=file
%
%  References:
% 
%  Zar, J. H. (1999), Biostatistical Analysis. 4th. ed. 
%              New-Jersey:Prentice Hall pp. 81, 113-114.
%

if nargin < 6,
   alpha = 0.05;   
end
    

a=alpha;
b=beta;

if h > 2,
   error('h must be 1 for right-hand direction or 2 for left-hand direction.');  
end

rt=s2e/s2; %Ratio of the expected(specified) and sample variances.

if h==1
   error=0.001; %Iterative process of estimating n.
   dif=1;
   while dif>error
      n=n+1;
      r=chi2inv(b,n-1)/chi2inv(1-a,n-1);
      dif=abs(r-rt); %The minimum sample size is that for which r=rt.
   end
   disp('     ')
   fprintf('It is required a sample size of at least:%3i\n\n', n);
else (h==2);
   error=0.0001; %Iterative process of estimating n.
   dif=1;
   while dif>error
      n=n+1;
      r=chi2inv(1-b,n-1)/chi2inv(a,n-1);
      dif=abs(r-rt); %The minimum sample size is that for which r=rt.
   end
   disp('     ')
   fprintf('It is required a sample size of at least:%3i\n\n', n);
end

Contact us at files@mathworks.com