function  [res,lbl] = partition(dim,data,names)

% PARTITION -- partition the SS and df in full-factorial ANOVAs
%
% [sets,setlbl] = partition(dim,[],[names])
% [par,lbl] = partition(dim,data,[names])
%
% DIM is an integer from 1 to 4 indicating the number of factors (IVs).
% SETS = partition(dim,[]) is a cell array suitable for passing to SUMSQ.
% SETLBL(k,:) is a 4-element string labeling the corresponding split set,
% e.g., ['   A';'   B';'--AB';'Totl']. You can customize factor identifiers
% in NAMES (one letter per factor only); default NAMES='ABCDEF...'.
%
% DATA typically is either SS or DF as produced by SUMSQ(DV,IVs,sets).
% PAR = partition(dim,data)   partitions interaction terms from 'cell' SSs.
%
% !!WARNING!! For imbalanced designs PARTITION may give misleading results.
%
% Example: [sets,setlbl]=partition(2,[]),[foo,lbl]=partition(2,zeros(4,1))
%  sets   = {   [1],   [2], [1 2]       ,[]}'
%  setlbl = ['   A';'   B';'--AB'       ,'Totl']
%     lbl = ['   A';'   B';'  AB',' err','Totl']
%
% See also ANOVA, SUMSQ.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2002/12/20 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

if (nargin==2)  names = 'ABCDEDFGH' ; end    % default factor labels

%---  Dispatch
if (isempty(data))    % generate SETS
  switch  dim
    case 1 ; [res,lbl] = gen_sets1(names) ;
    case 2 ; [res,lbl] = gen_sets2(names) ;
    case 3 ; [res,lbl] = gen_sets3(names) ;
    case 4 ; [res,lbl] = gen_sets4(names) ;
    otherwise 
      error('PARTITION implemented for up to 4 dimensions only. Sorry...') ;
  end
else   % partition into non-overlapping chunks, assuming appropriate SETS
  switch  dim
    case 1 ; [res,lbl] = partition1(data,names) ;
    case 2 ; [res,lbl] = partition2(data,names) ;
    case 3 ; [res,lbl] = partition3(data,names) ;
    case 4 ; [res,lbl] = partition4(data,names) ;
    otherwise 
      error('PARTITION implemented for up to 4 dimensions only. Sorry...') ;
  end
end


%%%%%%%%%%%%%%%%% SET GENERATOR SUBFUNCTIONS  %%%%%%%%%%%%%%%%%%%%
function  [sets,lbl] = gen_sets1(nm)
sets = {[1],[]}' ;              % {'A','Ttl'}
lbl(1,:) = ['   ' nm(1)] ;
lbl(2,:) = 'Totl' ; 
%% End of GEN_SETS1

function  [sets,lbl] = gen_sets2(nm)
sets = {[1],[2],[1 2],[]}' ;
lbl = repmat(' ',length(sets),4) ;
lbl(1:2,4) = nm(1:2)' ;            % 'A','B'
lbl(3,:) = ['--' nm(sets{3})] ;    % '--AB'
lbl(4,:) = 'Totl' ; 
%% End of GEN_SETS2

function  [sets,lbl] = gen_sets3(nm)
sets = {[1],[2],[3],[1 2],[1 3],[2 3],[1 2 3],[]}' ;
lbl = repmat(' ',length(sets),4) ;
lbl(1:3,4) = nm(1:3)' ;            % 'A','B','C'
lbl(4,:) = ['--' nm(sets{4})] ;    % '--AB'
lbl(5,:) = ['--' nm(sets{5})] ;    % '--AC'
lbl(6,:) = ['--' nm(sets{6})] ;    % '--BC'
lbl(7,:) = ['-'  nm(sets{7})] ;    % '-ABC'
lbl(8,:) = 'Totl' ; 
%% End of GEN_SETS3

function  [sets,lbl] = gen_sets4(nm)
sets = {[1],[2],[3],[4],[1 2],[1 3],[1 4],[2 3],[2 4],[3 4],...
        [1 2 3],[1 2 4],[1 3 4],[2 3 4],[1 2 3 4],[]}' ;
lbl = repmat(' ',length(sets),4) ;
lbl(1:4,4) = nm(1:4)' ;            % 'A','B','C','D'
lbl( 5,:) = ['--' nm(sets{ 5})] ;    % '--AB'
lbl( 6,:) = ['--' nm(sets{ 6})] ;    % '--AC'
lbl( 7,:) = ['--' nm(sets{ 7})] ;    % '--AD'
lbl( 8,:) = ['--' nm(sets{ 8})] ;    % '--BC'
lbl( 9,:) = ['--' nm(sets{ 9})] ;    % '--BD'
lbl(10,:) = ['--' nm(sets{10})] ;    % '--CD'
lbl(11,:) = ['-'  nm(sets{11})] ;    % '-ABC'
lbl(12,:) = ['-'  nm(sets{12})] ;    % '-ABD'
lbl(13,:) = ['-'  nm(sets{13})] ;    % '-ACD'
lbl(14,:) = ['-'  nm(sets{14})] ;    % '-BCD'
lbl(15,:) = nm(sets{15}) ;           % 'ABCD'
lbl(16,:) = 'Totl' ; 
%% End of GEN_SETS4


%%%%%%%%%%%%%%%%% PARTITION SUBFUNCTIONS  %%%%%%%%%%%%%%%%%%%%
function  [par,lbl] = partition1(d,nm)
if (length(d)~=2)
  error('Incorrect DATA length for DIM=1. Check your PARTITION set.') ;
end
par = zeros(3,1) ;     lbl = repmat(' ',3,4) ;
par(1) = d(1) ;        lbl(1,4) = nm(1) ;
par(2) = d(2) - d(1) ; lbl(2,:) = ' err' ;
par(3) = d(2) ;        lbl(3,:) = 'Totl' ;
%% End of PARTITION1

function  [par,lbl] = partition2(d,nm)
if (length(d)~=4)
  error('Incorrect DATA length for DIM=2. Check your PARTITION set.') ;
end
par = zeros(5,1) ;     lbl = repmat(' ',5,4) ;
par(1:2) = d(1:2) ;    lbl(1:2,4) = nm(1:2)' ;     % 'A','B'
par(3) = d(3)-d(1)-d(2) ; lbl(3,3:4) = nm(1:2) ;   % 'AB'
par(4) = d(4) - d(3) ;    lbl(4,:) = ' err' ;
par(5) = d(4) ;           lbl(5,:) = 'Totl' ;
%% End of PARTITION2

function  [par,lbl] = partition3(d,nm)
if (length(d)~=8)
  error('Incorrect DATA length for DIM=3. Check your PARTITION set.') ;
end
par = zeros(9,1) ;     lbl = repmat(' ',9,4) ;
par(1:3) = d(1:3) ;    lbl(1:3,4) = nm(1:3)' ;     % 'A','B','C'
par(4) = d(4)-d(1)-d(2) ; lbl(4,3:4) = nm([1 2]) ; % 'AB'
par(5) = d(5)-d(1)-d(3) ; lbl(5,3:4) = nm([1 3]) ; % 'AC'
par(6) = d(6)-d(2)-d(3) ; lbl(6,3:4) = nm([2 3]) ; % 'BC'
par(7) = d(7)-sum(par(1:6)) ; lbl(7,2:4) = nm(1:3) ;  % 'ABC'
par(8) = d(8) - d(7) ;    lbl(8,:) = ' err' ;
par(9) = d(8) ;           lbl(9,:) = 'Totl' ;
%% End of PARTITION3

function  [par,lbl] = partition4(d,nm)
if (length(d)~=16)
  error('Incorrect DATA length for DIM=3. Check your PARTITION set.') ;
end
par = zeros(17,1) ;    lbl = repmat(' ',17,4) ;
par(1:4) = d(1:4) ;    lbl(1:4,4) = nm(1:4)' ;     % 'A','B','C','D'
par(5) = d(5)-d(1)-d(2) ; lbl(5,3:4) = nm([1 2]) ; % 'AB'
par(6) = d(6)-d(1)-d(3) ; lbl(6,3:4) = nm([1 3]) ; % 'AC'
par(7) = d(7)-d(1)-d(4) ; lbl(7,3:4) = nm([1 4]) ; % 'AD'
par(8) = d(8)-d(2)-d(3) ; lbl(8,3:4) = nm([2 3]) ; % 'BC'
par(9) = d(9)-d(2)-d(4) ; lbl(9,3:4) = nm([2 4]) ; % 'BD'
par(10)=d(10)-d(3)-d(4) ; lbl(10,3:4)= nm([3 4]) ; % 'CD'
par(11) = d(11)-sum(par([1 2 3, 5 6 8])) ; lbl(11,2:4) = nm([1 2 3]); %'ABC'
par(12) = d(12)-sum(par([1 2 4, 5 7 9])) ; lbl(12,2:4) = nm([1 2 4]); %'ABD'
par(13) = d(13)-sum(par([1 3 4, 6 7 10])); lbl(13,2:4) = nm([1 3 4]); %'ACD'
par(14) = d(14)-sum(par([2 3 4, 8 9 10])); lbl(14,2:4) = nm([2 3 4]); %'BCD'
par(15) = d(15)-sum(par(1:14)) ;           lbl(15,:)   = nm(1:4) ;   %'ABCD'
par(16) = d(16) - d(15) ; lbl(16,:) = ' err' ;
par(17) = d(16) ;         lbl(17,:) = 'Totl' ;
%% End of PARTITION4

%%
%%%%% End of file PARTITION.M
