%%%%%%%%%%%   Show that GROUP is not significant  %%%%%%%%%%%%%
%%
%%  The between-subject error term can be obtained by simply doing a
%%  one-way ANOVA over the subject 'totals' (ZPcongr summed over all conditions).
%%  See p.442 in Howell's (1992) "Statistical Methods for Psychology"

 load S;gr=[S(:).group]';zP13=zeros(13,1);for k=1:13 zP13(k)=sum(S(k).dprime(:));end
 anova1(zP13,gr)

Source   SS      df      MS        F
----------------------------------------
Group	  457.4   1	 457.4    0.2235
Error	22510    11	2046
Total	22967    12


F(1,11)=0.224 -- not significant

%%  We can conclude that there is no MAIN effect of GROUP.
%%  We have not tested for interactions, but we assume they aren't signif. either.
%%  Therefore, we can ignore GROUP in all subsequent analyses and treat the whole
%%  population of subjects as a homogenous sample.  (NB: Provided that we always
%%  use CONGRUENCE rather than GABOR_ORIENT as our main variable.)



%%%%%%%%%%%   Show that POSITION is not significant  %%%%%%%%%%%%%
%%

%% This will be complicated because PLE_DATA_BY_SBJ ignores position altogether.
%% However, it is counterbalanced anyway, so we can assume it is controlled for.



%%%%%%%%%%%   Main within-subject ANOVA  %%%%%%%%%%%%%
%%
 [blk,diff,congr]=ind2sub([32,3,2],[1:192]');cond=[blk,diff,congr-1];clear blk diff congr
 data=zeros(0,7);for k=1:13;data=[data ; repmat([S(k).sbj,S(k).group],192,1),cond,S(k).zP_congr(:),S(k).tmean_RT(:)];end
 DVs=data(:,[6 7]);IVs=data(:,[1 3 4 5]); [sets,setlbl]=partition(4,[],'SBDC');
 [SS1,df1]=sumsq(DVs(:,1),IVs,sets);for k=1:length(df1) fprintf('%s %8.2f  %6d\n',setlbl(k,:),SS1(k),df1(k));end
 [SS,lbl]=partition(4,SS1,'SBDC');df=partition(4,df1);for k=1:length(df) fprintf('%s %8.2f  %6d\n',lbl(k,:),SS(k),df(k));end

Source           SumSq    df
-------------------------------
Total      3679.30    2495
 Sbj         478.47      12

 W/in sbj   3200.80    2483
   cells BS    883.21    415
     Block      193.43     31
     B * S      211.31    372

   cells DS     917.52    38
    Difficty     407.08     2
    D * S         31.97    24

   cells DBS  1488.47   1247
    cells DB    626.28     95
     D * B        25.76     62
    D * BS      140.44    744

   cells CS   1099.32     25
    Congr       389.78      1
    C * S       231.07     12

   cells CBS  1966.51    831
    cells CB    618.75     63
     C * B        35.54     31
    C * BS      426.91    372

   cells DCS  2419.06     77
    cells DC   1618.98      5
     D * C       822.12      2
    DC * S       58.56     24

   cells DCB  1917.82    191
    DC * B       44.11     62

   cells DCBS 3679.30   2495 = Total
    DC * BS     182.75    744
-------------------------------

And after trimming the intermediate results

Source       SumSq  eta2[%]   df     MeanSq         F     prob
----------------------------------------------------------------
Sbj         478.47   13.0     12      39.87
Block       193.43    5.3     31       6.24     10.99    <<1e-16
B * S       211.31    5.7    372       0.57
Difficulty  407.08   11.1      2     203.54    152.82    2.2e-14
D * S        31.97    0.9     24       1.33
Diff*Block   25.76    0.7     62       0.42      2.20    9.2e-07
DB * S      140.45    3.8    744       0.19
Congr       389.78   10.6      1     389.78     20.24    7.3e-04
C * S       231.07    6.3     12      19.26
Congr*Blk    35.54    1.0     31       1.15      0.999    0.4717
CB * S      426.91   11.6    372       1.15
Diff*Congr  822.12   22.3      2     411.06    168.45   7.44e-15
DC * S       58.57    1.6     24       2.44
Dif*Con*Bl   44.11    1.2     62       0.71      2.90   1.26e-11
DCB * S     182.73    5.0    744       0.25
Total      3679.30  100.0   2495       1.47
----------------------------------------------------------------


%%%%%%%%  Reaction times   %%%%%%%%%%%%%%%%%%%%%%%
%%
 [SS1,df1]=sumsq(DVs(:,2),IVs,sets);for k=1:length(df1) fprintf('%s %15.0f  %6d\n',setlbl(k,:),SS1(k),df1(k));end
 [SS,lbl]=partition(4,SS1,'SBDC');df=partition(4,df1);for k=1:length(df) fprintf('%s %15.0f  %6d\n',lbl(k,:),SS(k),df(k));end
 SS(16)=[];df(16)=[];lbl(16,:)=[];eta2=SS./SS(end);MS=SS./df;
 for k=1:length(df) fprintf('%s %15.0f   %6.2f  %6d  %12.2f\n',lbl(k,:),SS(k),eta2(k)*100,df(k),MS(k));end

 num=[2 3 4 ,  8  9 10 , 14]';den=[5 6 7 , 11 12 13 , 15]';F=MS(num)./MS(den);p=1-fcdf(F,df(num),df(den));
 for k=1:length(num) fprintf('%s  %4d %4d   %6.2f  %f\n',lbl(num(k),:),df(num(k)),df(den(k)),F(k),p(k));end

Source         SumSq   eta2[%]     df        MeanSq
---------------------------------------------------
   S        15120307    38.57      12    1260025.56
   B        14177945    36.17      31     457353.08
   D           40063     0.10       2      20031.31
   C           15603     0.04       1      15602.67
  SB         7197027    18.36     372      19346.85
  SD           84034     0.21      24       3501.42
  SC          148078     0.38      12      12339.87
  BD           47201     0.12      62        761.31
  BC           40815     0.10      31       1316.62
  DC            6182     0.02       2       3090.81
 SBD          553322     1.41     744        743.71
 SBC          892774     2.28     372       2399.93
 SDC           40146     0.10      24       1672.73
 BDC           43134     0.11      62        695.71
SBDC          791051     2.02     744       1063.24
Totl        39197680   100.00    2495      15710.49
---------------------------------------------------

Source df1  df2        F      prob
----------------------------------
   B    31  372    23.64  0.000000
   D     2   24     5.72  0.009297
   C     1   12     1.26  0.282808
  BD    62  744     1.02  0.429303
  BC    31  372     0.55  0.977804
  DC     2   24     1.85  0.179316
 BDC    62  744     0.65  0.981396
----------------------------------


%%%%%%%   Simple effects of Contrast for Congruent stimuli  %%%%%%%%%%%%%
%%
%% See Section 13.4 "Simple interaction effects" in Howell (1992, pp.392-5).

 idx=find(data(:,5)==1);DV=data(idx,6);IVs=data(idx,[1 3 4]);[s,keys]=split(DV,IVs(:,3));[keys,aggreg(s)./2]
 Diff    zP     (Note that DV=2.*zP in all ANOVAs)
---------------
    1    1.0308
    2    0.9559
    3    0.8210

%- Combine SBJ and BLOCK into a single IV with 13*32=416 levels and then do a one-way ANOVA
 [keys,foo,SxB]=unique(IVs(:,[1 2]),'rows');clear foo;
 [SS,df,MS]=anova(DV,[SxB,IVs(:,3)],'SD');F=MS(2)/MS(3),p=1-fcdf(F,df(2),df(3))

Source         SumSq   eta2[%]     df        MeanSq
---------------------------------------------------
   S          774.44    77.75     415          1.87
   D           37.61     3.78       2         18.81
  SD          183.97    18.47     830          0.22
Totl          996.03   100.00    1247          0.80
---------------------------------------------------

F(2,830) = 84.8469 ; p < 1e-15


%- Do the same thing in a different way in order to check the results
 s=split(DV,IVs(:,[1 2]));mnZ=aggreg(s);detrendedZ=DV-mnZ(SxB);s=split(detrendedZ,IVs(:,3));dm3=aggreg(s)'./2;[1:3;dm3;dm3+mean(DV./2)]

Difficulty    1.0000    2.0000    3.0000
Detrended Z   0.0949    0.0200   -0.1149
Raw Z=DV/2    1.0308    0.9559    0.8210

 anova1(detrendedZ,IVs(:,3))
% MS(C)=18.81 again and SS(err)=184 again. The error df's, however, are inflated
% because the 415 df used up for detrending are not taken into account.


%%% Finally, do it on the aggregated profiles
 clear all;load HUMAN_ZCONGR;z=HUMAN_ZCONGR([1 3 5],:)';describe(z)
    Mean  Std.dev       Min     Q25  Median     Q75     Max
------------------------------------------------------------
   0.821    0.150      0.47    0.70    0.85    0.93    1.05
   0.956    0.179      0.60    0.87    0.98    1.07    1.46
   1.031    0.207      0.63    0.90    1.04    1.20    1.34
------------------------------------------------------------
   0.936    0.179      0.56    0.82    0.96    1.07    1.28

 p=anova1(z)   % F(2,93)=11.12; p=4.6732e-005    disregarding the correspondences within each block

 DV=z(:);IVs=[repmat([1:32]',3,1) , [repmat(1,32,1);repmat(2,32,1);repmat(3,32,1)]];
 [SS,df,MS]=anova(DV,IVs,'BD');F=MS(2)/MS(3),p=1-fcdf(F,df(2),df(3))

Source         SumSq   eta2[%]     df        MeanSq
---------------------------------------------------
   B            2.66    71.02      31        0.0859
   D            0.72    19.30       2        0.3617
  BD            0.36     9.68      62        0.0058
Totl            3.75   100.00      95        0.0395
---------------------------------------------------

F(2,62) = 61.8299 ; p = 1.6653e-015

 idx=find(z(:,1)<z(:,2) & z(:,2)<z(:,3));length(idx), plot(idx,z(idx,:),'o');axis([0 33 0 2]);

This ordering holds for 22 of 32 blocks. Observed (=22) >> expected frequency (=5=32/6).



%%%%%%%%%%%%%%%%%%%%    WITHIN-BLOCK ANOVAs
 z=HUMAN_ZCONGR';mean(z)
    0.8210    1.1264    0.9559    0.5653    1.0308   -0.0696

 Cp=repmat([.245 .245 .160 .160 .106 .106],32,1);congr=repmat([ones(32,1) zeros(32,1)],1,3);block=repmat([1:32]',1,6);
 DV=z(:);IVs=[block(:),congr(:),Cp(:)];[SS,df,MS]=anova(DV,IVs,'BCD');
 SSerr=sum(SS([4 5 7]));dferr=sum(df([4 5 7]));MSerr=SSerr/dferr;[SSerr dferr MSerr]   % pooled error term (see Table 14.14 in Howell, 1992)

%Source         SumSq   eta2[%]    df    MeanSq        F    prob
%-----------------------------------------------------------------
% Block         3.720    10.09     31    0.1200      9.2   <1e-16
% Difficulty    7.829    21.23      2    3.9143    299.3   <1e-16
% Congruence    7.496    20.32      1    7.4958    573.2   <1e-16
% Diff*Congr   15.810    42.87      2    7.9050    604.5   <1e-16
% Error         2.027     5.50    155    0.0131
%  Diff*Blk      0.495     1.34     62    0.0080
%  Congr*Blk     0.683     1.85     31    0.0220
%  Df*Con*Blk    0.848     2.30     62    0.0137
% Total        36.881   100.00    191    0.1931
%-----------------------------------------------------------------

 idx=find(IVs(:,2)==0);[ss,d,ms]=anova(DV(idx),IVs(idx,[1 3],'BD');  % Incongruent only
 idx=find(IVs(:,2)==1);[ss,d,ms]=anova(DV(idx),IVs(idx,[1 3],'BD');  % Congruent only

%%Alternative partitioning highlighting the separate CONGRUENCE levels
%Source         SumSq   eta2[%]    df    MeanSq        F    prob
%-----------------------------------------------------------------
% Block         3.720    10.09     31    0.1200      9.2   < 1e-16
% Congruence    7.496    20.32      1    7.4958    573.2   < 1e-16
% Diff @ Con    0.723     1.96      2    0.3617    61.83   1.7e-15
% Diff @ Inc   22.915    62.13      2   11.4576   876.12   < 1e-16
% Error         2.027     5.50    155    0.0131
% Total        36.881   100.00    191    0.1931
%-----------------------------------------------------------------
% Blck @ Con    2.662     7.21     31    0.0859
% Blck @ Inc    1.741     4.72     31    0.0562
%-----------------------------------------------------------------

 sigma_err=sqrt(MSerr/32); t95=tinv(.975,dferr);CI95=sigma_err*t95;
sigma_err = 0.0202 ; dferr = 155 ; t95 = 1.975 ; CI95 = 0.0399 ;


