Code covered by the BSD License  

Highlights from
fd23

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

fd23(D,alpha)
function fd23(D,alpha)
% FD23 2^3 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. Each of the three factors (k = 3)
%  of interest has only two levels. Because each of the replicate of such an
%  experimental design has exactly 2^3 (=8) experimental trials or runs. As in
%  the 2^2 experimental design, it is a basic block used to create other more
%  complicated experimental designs.
%
%  Next figure illustrates the eight-factor level or treatment combinations on a
%  2^3 factorial design.
%
%                                bc                 abc
%                                .-----------------. 
%                               /|                /| 
%                              / |               / | 
%                             /  |              /  |
%                            /   |             /   |
%                   2     c .----|-----------. ac  |
%                           |    |b          |     | ab    
%                           |    .-----------|-----.       2     
%   Level Factor B          |   /            |    /
%                           |  /             |   /      
%                           | /              |  /             Level Factor C         
%                           |/               | /
%                   1   (1) .----------------. a      1
%                                         
%                           1                2
%                             Level Factor A
%
%  Syntax: fd23(D,alpha) 
%      
%  Inputs:
%       D - matrix data (=[X Y]) (last column must be the Y-dependent variable).
%           (X-independent variable entry for the 2^3 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 3.2 of Myers and Montgomery (2002, p.104), an electrical
%  engineer is investigating a plasma etching process used in semiconductor 
%  manufacturing. He is studing the effects of three factors-anode-cathode-gap
%  (A: 1=0.8, 2=1.2 mm), C_2F_6 gas flow rate (B: 1=125, 2=300 sccm), and power
%  applied to the cathode (C: 1=200, 2=250 W)-on the etch rate (angstroms/min).
%  Each factor is run at two levels. Two replicates of a 2^3 factorial design 
%  is applied as shown next,
%
%                                           Etch Rate
%                                   ------------------------
%        Factor-level combination        1             2     
%         A         B         C
%    -------------------------------------------------------
%         1         1         1         247           400
%         2         1         1         470           446
%         1         2         1         429           405
%         2         2         1         435           445
%         1         1         2         837           850
%         2         1         2         551           670                             
%         1         2         2         775           865                           
%         2         2         2         660           530
%    -------------------------------------------------------
%
%  Data matrix must be:
%  D=[1 1 1 247;1 1 1 400;2 1 1 470;2 1 1 446;1 2 1 429;1 2 1 405;2 2 1 435;2 2 1 445;
%    1 1 2 837;1 1 2 850;2 1 2 551;2 1 2 670;1 2 2 775;1 2 2 865;2 2 2 660;2 2 2 530];
%
%  Calling on Matlab the function: 
%             fd23(D)
%
%  Answer is:
%
%  Analysis of Variance of the 2^3 Factorial Design.
%  ----------------------------------------------------------------------------------------------
%   SOV             SS             df           MS               F             P      Conclusion
%  ----------------------------------------------------------------------------------------------
%  Model        505676.4375         7
%  A             22575.0625         1        22575.0625        5.6446        0.0448        S
%  B               333.0625         1          333.0625        0.0833        0.7802       NS
%  C            378532.5625         1       378532.5625       94.6465        0.0000        S
%  AB             2678.0625         1         2678.0625        0.6696        0.4369       NS
%  AC            94710.0625         1        94710.0625       23.6808        0.0012        S
%  BC             3277.5625         1         3277.5625        0.8195        0.3918       NS
%  ABC            3570.0625         1         3570.0625        0.8926        0.3724       NS
%  Error         31995.5000         8         3999.4375
%  ----------------------------------------------------------------------------------------------
%  Total        537671.9375                                                                                                            15
%  ----------------------------------------------------------------------------------------------
%  Number of replicates on each factor-level are: 2
%  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 25, 2005.
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A., R. Hernandez-Walls, F.A. Trujillo-Perez. (2005). 23FD:2^3 Factorial Design Analysis.
%         A MATLAB file. [WWW document]. URL http://www.mathworks.com/matlabcentral/fileexchange/
%         loadFile.do?objectId=9448
%
%  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)))|...
        (length(find(D(:,3)==1))~=length(find(D(:,3)==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 ~= 3,
    disp('  ');
    disp('''Warning: The number of factors must be three.''');
    disp('Please, check it.');
    return;
end;    

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

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

Y = D(:,end);

O = sum(Y(1:n)); %factor-level combination 1 (low level A-low level B-low level C)
a = sum(Y(n+1:2*n)); %factor-level combination 2 (high level A-low level B-low level C)
b = sum(Y((2*n)+1:3*n)); %factor-level combination 3 (low level A-high level B-low level C)
ab = sum(Y((3*n)+1:4*n)); %factor-level combination 4 (high level A-high level B-low level C)
c = sum(Y((4*n)+1:5*n)); %factor-level combination 5 (low level A-low level B-high level C)
ac = sum(Y((5*n)+1:6*n)); %factor-level combination 6 (high level A-low level B-high level C)
bc = sum(Y((6*n)+1:7*n)); %factor-level combination 7 (low level A-high level B-high level C)
abc = sum(Y((7*n)+1:end)); %factor-level combination 8 (high level A-high level B-high level C)

CA = a-O+ab-b+ac-c+abc-bc; %contrast of the total effect of A
CB = b+ab+bc+abc-O-a-c-ac; %contrast of the total effect of B
CC = c+ac+bc+abc-O-a-b-ab; %contrast of the total effect of C
CAB = ab-a-b+O+abc-bc-ac+c; %contrast of the total effect of AB
CAC = O-a+b-ab-c+ac-bc+abc; %contrast of the total effect of AC
CBC = O+a-b-ab-c-ac+bc+abc; %contrast of the total effect of BC
CABC = abc-bc-ac+c-ab+b+a-O; %contrast of the total effect of ABC

A = (1/(4*n))*CA; %average effect of A
B = (1/(4*n))*CB; %average effect of B
C = (1/(4*n))*CC; %average effect of C
AB = (1/(4*n))*CAB; %average effect of AB
AC = (1/(4*n))*CAC; %average effect of AC
BC = (1/(4*n))*CBC; %average effect of BC
ABC = (1/(4*n))*CABC; %average effect of ABC

SSA = CA^2/(n*8); %sum of squares of A
SSB = CB^2/(n*8); %sum of squares of B
SSC = CC^2/(n*8); %sum of squares of C
SSAB = CAB^2/(n*8); %sum of squares of AB
SSAC = CAC^2/(n*8); %sum of squares of AC
SSBC = CBC^2/(n*8); %sum of squares of BC
SSABC = CABC^2/(n*8); %sum of squares of ABC
SSM = SSA+SSB+SSC+SSAB+SSAC+SSBC+SSABC; %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
dfC = max(D(:,3)-1); %degrees of freedom of C
dfAB = dfA*dfB; %degrees of freedom of AB
dfAC = dfA*dfC; %degrees of freedom of AC
dfBC = dfB*dfC; %degrees of freedom of BC
dfABC = dfA*dfB*dfC; %degrees of freedom of ABC
dfM = dfA+dfB+dfC+dfAB+dfAC+dfBC+dfABC; %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
MSC = SSC/dfC; %mean square of B
MSAB = SSAB/dfAB; %mean square of AB
MSAC = SSAC/dfAC; %mean square of AC
MSBC = SSBC/dfBC; %mean square of BC
MSABC = SSABC/dfABC; %mean square of ABC
MSE = SSE/dfE; %error mean square

FA = MSA/MSE;
PA = 1 - fcdf(FA,dfA,dfE);
FB = MSB/MSE;
PB = 1 - fcdf(FB,dfB,dfE);
FC = MSC/MSE;
PC = 1 - fcdf(FC,dfC,dfE);
FAB = MSAB/MSE;
PAB = 1 - fcdf(FAB,dfAB,dfE);
FAC = MSAC/MSE;
PAC = 1 - fcdf(FAC,dfAC,dfE);
FBC = MSBC/MSE;
PBC = 1 - fcdf(FBC,dfBC,dfE);
FABC = MSABC/MSE;
PABC = 1 - fcdf(FABC,dfAB,dfE);

MSM = SSM/dfM; %full model mean square
FM = MSM/MSE;
PM = 1 - fcdf(FM,dfM,dfE);

if PM >= alpha;
    disp('  ')
    fprintf('None of the variables on the model has a significant effect: F = %3.4f, P = %3.4f\n', FM,PM);
else
    if PA >= alpha;
        dsA ='NS';
    else
        dsA =' S';
    end;
    if  PB >= alpha;
        dsB ='NS';
    else
        dsB =' S';
    end;
    if PC >= alpha;
        dsC ='NS';
    else
        dsC =' S';
    end;
    if  PAB >= alpha;
        dsAB ='NS';
    else
        dsAB =' S';
    end;
    if  PAC >= alpha;
        dsAC ='NS';
    else
        dsAC =' S';
    end;
    if  PBC >= alpha;
        dsBC ='NS';
    else
        dsBC =' S';
    end;
    if  PABC >= alpha;
        dsABC ='NS';
    else
        dsABC =' S';
    end;
    disp('  ')
    disp('Analysis of Variance of the 2^3 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('C            %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSC,dfC,MSC,FC,PC,dsC);
    fprintf('AB           %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSAB,dfAB,MSAB,FAB,PAB,dsAB);
    fprintf('AC           %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSAC,dfAC,MSAC,FAC,PAC,dsAC);
    fprintf('BC           %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSBC,dfBC,MSBC,FBC,PBC,dsBC);
    fprintf('ABC          %11.4f%10i%18.4f%14.4f%14.4f%9s\n',SSABC,dfABC,MSABC,FABC,PABC,dsABC);
    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).');
end;

return;

Contact us at files@mathworks.com