%%%%%%%   25 August, 2003
%%%
%%%  Re-analyze the sequential effects in PLExp1 using the new tool PLM_sequential.
%%%  Note that there is a mistake in Table 3 (\ref{tab:SeqEff}) in
%%%  the draft PLPaper1/expmt.tex -- the rows and columns in the table
%%%  should be reversed.  P(Rt="R"|St1=L,Rt1=R)=0.519 instead of 0.505.
%%%
%%%  The old analysis confounds the effects of the previous response R(t-1)
%%%  and the background orientation. Given that P(R="Right") goes up in
%%%  context R, the previous response R(t-1) carries information about
%%%  the context orientation. This inflates the sequential dependency.
%%%
%%%  The new analysis below calculates separate probabilities for
%%%  each overall condition=<S(t),backgr> and partials them out of
%%%  the sequential probabilities.


%-- Import the raw data, read the relevant variables, and call PLM_SEQUENTIAL
 load S   % see PLEdata_by_sbj
 seq=zeros(16,10,13);for k=1:13 d=S(k).data;bl=d(:,1);Gabor_orient=d(:,5)/20+1.5;noise_orient=d(:,2)/30+1.5;stim=[Gabor_orient,d(:,3),noise_orient];seq(:,:,k)=PLM_sequential(stim,d(:,7),bl);end
 size(seq) % --> [16 10 13]

 s=mean(seq,3);for k=1:16 fprintf('%d  %d  %d  %d   %5.0f %5.0f %5.0f %5.0f    %6.3f  %+6.3f\n',s(k,:));end
 for k=1:4 foo=mean(s(4*k-3:4*k,:));fprintf('%d  %d  ----   %5.0f %5.0f %5.0f %5.0f    %6.3f  %+6.3f\n',foo([1 2 5:10]));end

curr  prev      N     N     N     N        P    delta
S  B  S  R     tot   Lft   Rgt   cor     Right  P("R")
------------------------------------------------------
1  1  1  1     947   802   145   802     0.159  -0.048
1  1  1  2     237   180    57   180     0.220  +0.013
1  1  2  1     379   291    88   291     0.225  +0.018
1  1  2  2     817   634   183   634     0.225  +0.017
1  1  ----     595   477   118   477     0.207  -0.000 <-- Mean total

1  2  1  1     785   544   241   544     0.325  -0.029
1  2  1  2     416   262   154   262     0.343  -0.011
1  2  2  1     233   142    90   142     0.389  +0.036
1  2  2  2     968   624   345   624     0.358  +0.004
1  2  ----     600   393   207   393     0.354  +0.000 <-- Mean total

2  1  1  1     961   320   641   641     0.667  -0.014
2  1  1  2     235    83   152   152     0.655  -0.026
2  1  2  1     375   117   258   258     0.697  +0.016
2  1  2  2     809   234   575   575     0.704  +0.024
2  1  ----     595   188   407   407     0.681  -0.000 <-- Mean total

2  2  1  1     786   182   604   604     0.769  -0.031
2  2  1  2     416    97   319   319     0.770  -0.030
2  2  2  1     235    51   184   184     0.807  +0.007
2  2  2  2     967   139   828   828     0.853  +0.053
2  2  ----     601   117   484   484     0.800  -0.000 <-- Mean total
------------------------------------------------------

 s4=(s(1:4,:)+s(5:8,:)+s(9:12,:)+s(13:16,:))/4;for k=1:4 fprintf('-  -  %d  %d   %5.0f %5.0f %5.0f %5.0f    %6.3f  %+6.3f\n',s4(k,3:10));end
-  -  1  1     869   462   407   648     0.480  -0.031   % S(t-1)=L, R(t-1)=L
-  -  1  2     326   156   171   228     0.497  -0.013   % S(t-1)=L, R(t-1)=R
-  -  2  1     305   150   155   219     0.530  +0.019   % S(t-1)=R, R(t-1)=L
-  -  2  2     890   407   483   665     0.535  +0.025   % S(t-1)=R, R(t-1)=R
--------------------------------------------------------
-  -  -  -     598   294   304   440     0.510  -0.000


 m=reshape(s4(:,10),2,2)';[m,mean(m,2) ; mean(m), mean(m(:))]
          prR=L     prR=R     Mean
-----------------------------------
prS=L   -0.0306   -0.0133   -0.0220
prS=R    0.0193    0.0246    0.0220
-----------------------------------
Mean    -0.0057    0.0057   -0.0000

 m1=reshape(s4(:,9),2,2)';[m1,mean(m1,2) ; mean(m1), mean(m1(:))]
          prR=L     prR=R     Mean      % uncorrected probabilities
-----------------------------------     % This is analogous to the
prS=L    0.4797    0.4970    0.4883     % old Table 3, except that
prS=R    0.5296    0.5349    0.5323     % the different conditions
-----------------------------------     % enter with equal weights
Mean     0.5046    0.5160    0.5103     % (instead of their raw freq's).


%-- Within-subject ANOVA
 DV=squeeze(seq(:,10,:));size(DV)    % delta_P, [16x13]
 IVs=zeros(16*13,3);for k=1:13 idx=[1:16]'+(k-1)*16;IVs(idx,1)=4*(k-1)+2*(seq(:,1,k)-1)+seq(:,2,k);IVs(idx,2:3)=seq(:,3:4,k);end;openvar IVs
% IVs = [replication, prev_Gabor, prev_resp],  where replication=[sbj*curr_Gabor*backgr]=13*2*2=52 levels

 [SS,df,MS,lbl] = anova(DV(:),IVs,'XSR');

Source         SumSq   eta2[%]     df        MeanSq
---------------------------------------------------
   X           0.000     0.00      51        0.0000
   S           0.100    21.63       1        0.1004
   R           0.007     1.43       1        0.0066
  XS           0.139    29.97      51        0.0027
  XR           0.158    34.04      51        0.0031
  SR           0.002     0.40       1        0.0019
 XSR           0.058    12.53      51        0.0011
 err           0.000     0.00       0        0.0000
Totl           0.464   100.00     207        0.0022
---------------------------------------------------

 err_idx=[4 5 7];   % XS + XR + XSR
 df_err=sum(df(err_idx)),SS_err=sum(SS(err_idx)),MS_err=SS_err/df_err
df_err = 153=51+51+51 ; SS_err = 0.3554 ; MS_err = 0.0023

 F = MS([2 3 6])'./MS_err , p = 1-fcdf(F,1,df_err)
     S(t-1)    R(t-1)     SxR
-------------------------------
F = 43.2404    2.8613    0.8053
p = 7.2e-10    0.0928    0.3709

 CI95=sqrt(MS_err/52).*tinv([0.025 0.975],df_err)
CI95 = [-0.0132 +0.0132]    % winthin-subject-and-condition confidence interval


%-- Subject-by-subject
 dP = squeeze(seq(:,10,:)) ;        % 16 conditions x 13 Ss
 dP4=(dP(1:4,:)+dP(5:8,:)+dP(9:12,:)+dP(13:16,:))/4;
 mean(dP4')
ans = [-0.0306 -0.0133  +0.0193 +0.0246]   % replicate the table above

 maineff_prevS = (dP4(3,:)-dP4(1,:)+dP4(4,:)-dP4(2,:))/2 ;
 maineff_prevR = (dP4(2,:)-dP4(1,:)+dP4(4,:)-dP4(3,:))/2 ;

 m=[maineff_prevS' maineff_prevR'] , [mean(m) ; std(m,1) ; min(m);median(m);max(m)]

%        Main effect of
% sbj   S(t-1)    R(t-1)
% ----------------------
%  3    0.0506    0.0131
%  4    0.1324    0.0525                 S(t-1)    R(t-1)
%  5   -0.0112    0.0476       --------------------------
%  6    0.1192   -0.0664       mean      0.0439    0.0113
%  7    0.0496    0.0448       std.dev.  0.0447    0.0476
%  8    0.0424    0.0356
% 10   -0.0009    0.0490       min      -0.0112   -0.0664
% 11    0.0068    0.0302       median    0.0424    0.0302
% 12    0.0127   -0.0481       max       0.1324    0.0871
% 14    0.0034   -0.0627       --------------------------
% 15    0.0246   -0.0016
% 16    0.0986   -0.0341
% 17    0.0431    0.0871
% ----------------------



***********************************************************************
***
*** Aug 26, 2003 --- Looking for sequential effects in the model traces
***

 load sched; load PLM_CACHE;
 Mparams=PLM_params
Mparams =
      rep_size: [7 5 1]
       max_act: 0.5000
      rep_gain: 0.8000
     rep_noise: 0.1000
     out_noise: 0.2000
      W_minmax: [-1 1]
        W_init: 0.1700
       W0_seed: [35x1 double]
    learn_rate: 0.0015
    runav_rate: 0.0200
     criterion: 2.2000
    fdbk_fract: 1
      blk_size: 300

 N_runs=50;seqL=zeros(16,10,N_runs);seqR=zeros(16,10,N_runs);
 for k=1:N_runs stim=block_from_cache(sched1,PLM_CACHE);stim=cat(1,stim{:});o=PLM_Hebb1(stim,Mparams);seqL(:,:,k)=PLM_sequential(stim,o(:,1));end
 for k=1:N_runs stim=block_from_cache(sched2,PLM_CACHE);stim=cat(1,stim{:});o=PLM_Hebb1(stim,Mparams);seqR(:,:,k)=PLM_sequential(stim,o(:,1));end

 s=(mean(seqL,3)+mean(seqR,3))/2;for k=1:16 fprintf('%d  %d  %d  %d   %5.0f %5.0f %5.0f %5.0f    %6.3f  %+6.3f\n',s(k,:));end
 for k=1:4 foo=mean(s(4*k-3:4*k,:));fprintf('%d  %d  ----   %5.0f %5.0f %5.0f %5.0f    %6.3f  %+6.3f\n',foo([1 2 5:10]));end

curr  prev      N     N     N     N        P    delta
S  B  S  R     tot   Lft   Rgt   cor     Right  P("R")
------------------------------------------------------
1  1  1  1     974   765   209   765     0.214  +0.030
1  1  1  2     220   180    40   180     0.181  -0.003
1  1  2  1     396   319    77   319     0.194  +0.010
1  1  2  2     802   684   118   684     0.147  -0.037
1  1  ----     598   487   111   487     0.184  +0.000 <-- Mean total

1  2  1  1     798   498   299   498     0.376  +0.041
1  2  1  2     393   267   126   267     0.321  -0.014
1  2  2  1     223   146    77   146     0.344  +0.010
1  2  2  2     978   688   290   688     0.297  -0.038
1  2  ----     598   400   198   400     0.334  +0.000 <-- Mean total

2  1  1  1     974   289   685   685     0.703  +0.037
2  1  1  2     224    76   147   147     0.659  -0.007
2  1  2  1     396   128   268   268     0.676  +0.010
2  1  2  2     798   298   500   500     0.626  -0.040
2  1  ----     598   198   400   400     0.666  +0.000 <-- Mean total

2  2  1  1     801   119   682   682     0.852  +0.036
2  2  1  2     400    78   322   322     0.806  -0.010
2  2  2  1     222    40   182   182     0.821  +0.004
2  2  2  2     969   208   761   761     0.786  -0.030
2  2  ----     598   111   487   487     0.816  -0.000 <-- Mean total
------------------------------------------------------

 s4=(s(1:4,:)+s(5:8,:)+s(9:12,:)+s(13:16,:))/4;for k=1:4 fprintf('-  -  %d  %d   %5.0f %5.0f %5.0f %5.0f    %6.3f  %+6.3f\n',s4(k,3:10));end
-  -  1  1     887   418   469   658     0.536  +0.036
-  -  1  2     309   150   159   229     0.492  -0.008
-  -  2  1     309   158   151   229     0.509  +0.009
-  -  2  2     887   469   417   658     0.464  -0.036
--------------------------------------------------------
-  -  -  -     598   299   299   443     0.500  +0.000


 m=reshape(s4(:,10),2,2)';[m,mean(m,2) ; mean(m), mean(m(:))]

           *** MODEL ***                          *** DATA  PLE1 ***
          prR=L     prR=R     Mean                 prR=L     prR=R     Mean
-----------------------------------      -----------------------------------
prS=L    0.0361   -0.0084    0.0139      prS=L   -0.0306   -0.0133   -0.0220
prS=R    0.0085   -0.0362   -0.0139      prS=R    0.0193    0.0246    0.0220
-----------------------------------      -----------------------------------
Mean     0.0223   -0.0223   -0.0000      Mean    -0.0057    0.0057   -0.0000

 m1=reshape(s4(:,9),2,2)';[m1,mean(m1,2) ; mean(m1), mean(m1(:))]

           *** MODEL ***                          *** DATA  PLE1 ***
          prR=L     prR=R     Mean                 prR=L     prR=R     Mean
-----------------------------------      -----------------------------------
prS=L    0.5362    0.4917    0.5140      prS=L    0.4797    0.4970    0.4883
prS=R    0.5086    0.4639    0.4862      prS=R    0.5296    0.5349    0.5323
-----------------------------------      -----------------------------------
Mean     0.5224    0.4778    0.5001      Mean     0.5046    0.5160    0.5103


#######################################################################
#### The sequential effects in the model seem exactly opposite    #####
#### to those in the data.  In particular, they are contrastive   #####
#### instead of assimilative.                                     #####
#######################################################################


***********************************************************************
***
*** Aug 27, 2003 --- Preliminary sequential analysis of the no-feedback data
***

 !copy C:\work\PLExp2\data\sbj0??.dat C:\work\PLExp2\raw_data.dat
 D=import_PLE2_data
42202 rows and 21 columns loaded.      % 10 partial Ss
 N=zeros(length(D),1);for k=1:length(D) N(k)=size(st(k).dprime,1);end   % number of blocks completed by each subject

 st=PLE2_stats(D) ; se=cat(1,st(:).seq_eff) ; [cat(1,D(:).sbj),N se]

  *** PLE2 partial data ***
  sbj   N     S(t-1)    R(t-1)
------------------------------
    1   32   -0.0537    0.0723
    2   16   -0.0102    0.1271
    4   16   -0.0075   -0.0022
    5    8    0.0123    0.0942
    6   16   -0.0320    0.1226
    7    8    0.0119    0.2115
    8   16   -0.0179   -0.0521
    9    8   -0.0029    0.0064
   10    8   -0.0020    0.1107
   20   12   -0.0177    0.0156


