Code covered by the BSD License  

Highlights from
fd22

from fd22 by Antonio Trujillo-Ortiz
2^2 Factorial Design Analysis.

fd22(D,alpha)
function fd22(D,alpha)
% FD22 2^2 Factorial Design Analysis.
%  This m-file is used in experiments involving several factors where it is
%  necessary to investigate the joint effects (main and interactions) of the 
%  factors on a response variable and determine by a formal analysis of 
%  variance which factor effects are nonzero. An important special case is
%  that where each of the two factors (k = 2) of interest has only two levels.
%  Because each of the replicate of such an experimental design has exactly 
%  2^2 (=4) experimental trials or runs. So, this results the simplest design
%  in the 2^k series. It is a basic block used to create other more complicated
%  experimental designs.
%
%  Next figure illustrates the four-factor level or treatment combinations on a
%  2^2 factorial design.
%
%                           b                ab
%                       2   .----------------.
%                           |                |
%                           |                |
%       Level Factor B      |                |
%                           |                |
%                           |                |
%                           |                |
%                       1   .----------------.
%                          (1)               a
%
%                           1                2
%                             Level Factor A
%
%
%  Syntax: fd22(D,alpha) 
%      
%  Inputs:
%       D - matrix data (=[X Y]) (last column must be the Y-dependent variable).
%           (X-independent variable entry for the 2^2 factorial design).
%   alpha - significance-value (default = 0.05).
%
%  Outputs:
%       A complete analysis of variance is summarized on a table.
%       [NOTE: If none of the variables on the model has a significant
%       effect, should appers this note and does not proceed with the
%       analysis of variance.]
%
%  From the example of Myers and Montgomery (2002, p.86-90), we are interested to investigate
%  the effect of the concentration of the reactant and the feed rate on the viscosity of 
%  product from a chemical process. Let the reactant concentration be factor A, and let the
%  two levels of interest be 15% (=1) and 25% (=2). The feed rate is factor B, with the low
%  level being 20 lb/hr (=1) and the high level being 30 lb/hr (=2). The experiment is 
%  replicated four times. The data are as follow,
%
%                                              Viscosity
%                               ---------------------------------------
%    Factor-level combination       1         2         3         4 
%         A         B
%    ------------------------------------------------------------------
%         1         1              145       148       147       140
%         2         1              158       152       155       152
%         1         2              135       138       141       139
%         2         2              150       152       146       149
%    ------------------------------------------------------------------
%
%  Data matrix must be:
%  D=[1 1 145;1 1 148;1 1 147;1 1 140;2 1 158;2 1 152;2 1 155;2 1 152;
%    1 2 135;1 2 138;1 2 141;1 2 139;2 2 150;2 2 152;2 2 146;2 2 149];
%
%  Calling on Matlab the function: 
%             fd22(D)
%
%  Answer is:
%
%  Analysis of Variance of the 2^2 Factorial Design.
%  ----------------------------------------------------------------------------------------------
%   SOV             SS             df           MS               F             P      Conclusion
%  ----------------------------------------------------------------------------------------------
%  Model           551.1875         3
%  A               410.0625         1          410.0625       49.0848        0.0000        S
%  B               138.0625         1          138.0625       16.5262        0.0016        S
%  AB                3.0625         1            3.0625        0.3666        0.5562       NS
%  Error           100.2500        12            8.3542
%  ----------------------------------------------------------------------------------------------
%  Total           651.4375        15
%  ----------------------------------------------------------------------------------------------
%  Number of replicates on each factor-level are: 4
%  With a given significance level of: 0.05
%  The results are significant (S) and/or not significant (NS).
%
%  Created by A. Trujillo-Ortiz, R. Hernandez-Walls and F.A. Trujillo-Perez
%             Facultad de Ciencias Marinas
%             Universidad Autonoma de Baja California
%             Apdo. Postal 453
%             Ensenada, Baja California
%             Mexico.
%             atrujo@uabc.mx
%  Copyright (C) December 24, 2005.
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A., R. Hernandez-Walls, F.A. Trujillo-Perez. (2005). FD22:2^2 Factorial Design Analysis.
%         A MATLAB file. [WWW document]. URL http://www.mathworks.com/matlabcentral/fileexchange/
%         loadFile.do?objectId=9444
%
%  References:
%  Myers, R. H. and Montgomery, D. C. (2002), Response Surface Methodology:Process and Product
%          Optimization Using Designed Experiments. 2nd. Ed. NY: John Wiley & Sons, Inc.
%

if nargin < 2,
    alpha = 0.05; %(default)
end; 

if (alpha <= 0 | alpha >= 1),
    disp('  ');
    fprintf('Warning: significance level must be between 0 and 1\n');
    return;
end;

if (length(find(D(:,1)==1))~=length(find(D(:,1)==2)))|(length(find(D(:,2)==1))~=length(find(D(:,2)==2))),
    disp('  ');
    disp('''Warning: Some of the factor-level combination(s) has(have) different number of replicates.''');
    disp('Please, check it.');
    return;
end;

if size(D,2)-1 ~= 2,
    disp('  ');
    disp('''Warning: The number of factors must be two.''');
    disp('Please, check it.');
    return;
end;    

N = size(D,1); %total number of observations

n = length(find(D(:,1)==1))/2; %number of replicates

Y = D(:,end);

O = sum(Y(1:n)); %factor-level combination 1 (low level A-low level B)
a = sum(Y(n+1:2*n)); %factor-level combination 2 (high level A-low level B)
b = sum(Y((2*n)+1:3*n)); %factor-level combination 3 (low level A-high level B)
ab = sum(Y((3*n)+1:end)); %factor-level combination 4 (high level A-high level B)

CA = ab+a-b-O; %contrast of the total effect of A
CB = ab+b-a-O; %contrast of the total effect of B
CAB = ab+O-a-b; %contrast of the total effect of AB

A = (1/(2*n))*CA; %average effect of A
B = (1/(2*n))*CB; %average effect of B
AB = (1/(2*n))*CAB; %average effect of AB

SSA = CA^2/(n*4); %sum of squares of A
SSB = CB^2/(n*4); %sum of squares of B
SSAB = CAB^2/(n*4); %sum of squares of AB
SSM = SSA+SSB+SSAB; %sum of squares of full model
SSTo = sum(Y.^2)-(sum(Y)^2/N); %total sum of squares
SSE = SSTo-SSM; %error sum of squares

dfA = max(D(:,1)-1); %degrees of freedom of A
dfB = max(D(:,2)-1); %degrees of freedom of B
dfAB = dfA*dfB; %degrees of freedom of AB
dfM = dfA+dfB+dfAB; %degrees of freedom of full model
dfTo = N-1; %total degrees of freedom
dfE = dfTo-dfM; %error degrees of freedom

MSA = SSA/dfA; %mean square of A
MSB = SSB/dfB; %mean square of B
MSAB = SSAB/dfAB; %mean square of AB
MSE = SSE/dfE; %error mean square

FA = MSA/MSE;
PA = 1 - fcdf(FA,dfA,dfE);
FB = MSB/MSE;
PB = 1 - fcdf(FB,dfB,dfE);
FAB = MSAB/MSE;
PAB = 1 - fcdf(FAB,dfAB,dfE);

if PA >= alpha;
    dsA ='NS';
else
    dsA =' S';
end;
if  PB >= alpha;
    dsB ='NS';
else
    dsB =' S';
end;
if  PAB >= alpha;
    dsAB ='NS';
else
    dsAB =' S';
end;

disp('  ')
disp('Analysis of Variance of the 2^2 Factorial Design.')
fprintf('----------------------------------------------------------------------------------------------\n');
disp(' SOV             SS             df           MS               F             P      Conclusion');
fprintf('----------------------------------------------------------------------------------------------\n');
fprintf('Model        %11.4f%10i\n',SSM,dfM);
fprintf('A            %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSA,dfA,MSA,FA,PA,dsA);
fprintf('B            %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSB,dfB,MSB,FB,PB,dsB);
fprintf('AB           %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSAB,dfAB,MSAB,FAB,PAB,dsAB);
fprintf('Error        %11.4f%10i%18.4f\n',SSE,dfE,MSE);
fprintf('----------------------------------------------------------------------------------------------\n');
fprintf('Total        %11.4f%10i\n',SSTo,dfTo);
fprintf('----------------------------------------------------------------------------------------------\n');
fprintf('Number of replicates on each factor-level are: %i\n', n);
fprintf('With a given significance level of: %.2f\n', alpha);
disp('The results are significant (S) and/or not significant (NS).');

return;

Contact us at files@mathworks.com