clear
mypath  = pwd;% '...'; % set the path to SD_RM_Exp01_dataset.csv
cd(mypath)
tab     = readtable('SD_RM_Exp01_dataset.csv');
% get the subjects identifier
obs     = unique(tab.obs);
% number of subjects
n       = numel(unique(obs));
% exclude control trials
tab(tab.stop==1 | tab.expe==1,:) = [];
% preallocate (sdp_* = error over Delta; npa_* = model-free estimate)
[sdp_ran,sdp_rot,sdp_repo] = deal(NaN(n,6),NaN(n,6),NaN(n,7));
[npa_ran,npa_rot,npa_repo] = deal(NaN(n,1),NaN(n,1),NaN(n,1));
% the two deltas considered for non-parametric analysis
negside = [-40 -20];
posside = [ 20  40];
for i = 1:n
    % select individual data
    i_data       = tab(tab.obs==i,:);
    %----------------------------------------------------------------------
    % random condition
    ran          = i_data(i_data.cond==0,:);
    sdp_ran(i,:) = grpstats(ran.errorc,ran.delta,'mean');
    % delta values
    x_ran        = unique(ran.delta(~isnan(ran.delta)));
    % model free estimate
    npa_ran(i,:) = [mean(sdp_ran(i,ismember(x_ran,posside))) - ...
                    mean(sdp_ran(i,ismember(x_ran,negside)))];
    %----------------------------------------------------------------------
    % rotational condition
    rot          = i_data(i_data.cond==1,:);
    sdp_rot(i,:) = grpstats(rot.errorc,rot.delta,'mean');
    % delta values
    x_rot        = unique(rot.delta(~isnan(rot.delta)));
    % model free estimate
    npa_rot(i,:) = [mean(sdp_rot(i,ismember(x_rot,posside))) - ...
                    mean(sdp_rot(i,ismember(x_rot,negside)))];
    %----------------------------------------------------------------------
    % previous reported orientation
    % bound to +/- 60° as for delta
    i_data.deltar(i_data.deltar> 60) =  60;
    i_data.deltar(i_data.deltar<-60) = -60;
    sdp_repo(i,:)= grpstats(i_data.errorc,i_data.deltar,'mean');
    % delta values
    x_repo       = unique(i_data.deltar(~isnan(i_data.deltar)));
    % model free estimate
    npa_repo(i,:)= [mean(sdp_repo(i,ismember(x_repo,posside))) - ...
                    mean(sdp_repo(i,ismember(x_repo,negside)))];
end
% example of group statistic on model-free estimates
% (simple t-test against zero for each condition)
[h,p,ci,stat] = ttest([npa_ran npa_rot npa_repo]);
effsize       = mean([npa_ran npa_rot npa_repo]) ...
               ./std([npa_ran npa_rot npa_repo]);
