% This scripts calculates the response times for the adaptation (tae) and
% choice repetition (sd) tasks.
% It saves the rts for each task respectively.
% (c) Ella Bosch, 2021

clear all; close all;

% This analysis uses cleaned data with crit 0:
% 0 = exclude poor performers on SD task and people who do not meet inclusion criteria

%% TAE RT ANALYSIS

% load data
tae_data = readtable('P:\3018029.04\Data (drive)\data_tae_clean.csv');

% read stimulus levels
stim_levels_tae = unique(tae_data.stimulus_evidence);

for iGroup = 1:2
    
    % Select data for this group
    data_group = tae_data(tae_data.group == (iGroup-2)*-1,:);
    % note: this way, iGroup 1 = ASD and iGroup 2 = control
        
    % Make a list of the number of subjects in the group
    subjects_group{iGroup} = unique(data_group.subject);
    
    for iSubject = 1:length(subjects_group{iGroup})

        % Select data for this subject
        data_subj = tae_data(tae_data.subject == subjects_group{iGroup}(iSubject),:);
        
        % Calculate mean rt and sd over all stimuli
        tae_rt_tot_mean{iGroup}(iSubject,:) = mean(data_subj.rt);
        tae_rt_tot_sd{iGroup}(iSubject,:) = std(data_subj.rt);
        
        for iStimulus = 1:length(stim_levels_tae)
            
            % Select data for one stimulus level
            data_subj_stim = data_subj(data_subj.stimulus_evidence == stim_levels_tae(iStimulus),:);
            
            % Calculate mean rt and sd per stimulus level
            tae_rt_stim_mean{iGroup}(iSubject,iStimulus) = mean(data_subj_stim.rt);
            tae_rt_stim_sd{iGroup}(iSubject,iStimulus) = std(data_subj_stim.rt);
            
        end
        
    end
    
end

%% Test group differences

[h,p,ci,stats] = ttest2(tae_rt_tot_mean{1},tae_rt_tot_mean{2});
            
%% Save the output

tae_rt = cell(9,3);
tae_rt{1,2} = 'ASD';
tae_rt{1,3} = 'TD';
tae_rt{2,1} = 'Mean rt over all stimuli';
tae_rt{2,2} = tae_rt_tot_mean{1};
tae_rt{2,3} = tae_rt_tot_mean{2};
tae_rt{3,1} = 'SDs over all stimuli';
tae_rt{3,2} = tae_rt_tot_sd{1};
tae_rt{3,3} = tae_rt_tot_sd{2};
tae_rt{4,1} = 'Mean rt per stimulus level';
tae_rt{4,2} = tae_rt_stim_mean{1};
tae_rt{4,3} = tae_rt_stim_mean{2};
tae_rt{5,1} = 'SDs per stimulus level';
tae_rt{5,2} = tae_rt_stim_sd{1};
tae_rt{5,3} = tae_rt_stim_sd{2};
tae_rt{6,1} = 'T-test mean rt';
tae_rt{6,2} = 'h';
tae_rt{6,3} = h;
tae_rt{7,2} = 'p';
tae_rt{7,3} = p;
tae_rt{8,2} = 'ci';
tae_rt{8,3} = ci;
tae_rt{9,2} = 'stats';
tae_rt{9,3} = stats;

save('P:/3018029.04/Analysis Scripts/Export/tae_rt.mat','tae_rt');

% create table 
subjects = [subjects_group{1}; subjects_group{2}];
group = [ones(length(subjects_group{1}),1); zeros(length(subjects_group{2}),1)];
tae_rt_mean = [tae_rt_tot_mean{1}; tae_rt_tot_mean{2}];
rt_means_tae_sd = table(subjects, group, tae_rt_mean);
writetable(rt_means_tae_sd, 'P:/3018029.04/Analysis Scripts/Export/rt_means_tae_sd.csv');

%% SD RT ANALYSIS

clear all

% load data
sd_data = readtable('P:\3018029.04\Data (drive)\data_sd_clean.csv');

% read stimulus levels
stim_levels_sd = unique(sd_data.currStimEvidence);

for iGroup = 1:2
    
    % Select data for this group
    data_group = sd_data(sd_data.group == (iGroup-2)*-1,:);
    % note: this way, iGroup 1 = ASD and iGroup 2 = control
        
    % Make a list of the number of subjects in the group
    subjects_group{iGroup} = unique(data_group.subject);
    
    for iSubject = 1:length(subjects_group{iGroup})

        % Select data for this subject
        data_subj = sd_data(sd_data.subject == subjects_group{iGroup}(iSubject),:);
        
        % Calculate mean rt and sd over all stimuli
        sd_rt_tot_mean{iGroup}(iSubject,:) = mean(data_subj.currRt);
        sd_rt_tot_sd{iGroup}(iSubject,:) = std(data_subj.currRt);
        
        for iStimulus = 1:length(stim_levels_sd)
            
            % Select data for one stimulus level
            data_subj_stim = data_subj(data_subj.currStimEvidence == stim_levels_sd(iStimulus),:);
            
            % Calculate mean rt and sd per stimulus level
            sd_rt_stim_mean{iGroup}(iSubject,iStimulus) = mean(data_subj_stim.currRt);
            sd_rt_stim_sd{iGroup}(iSubject,iStimulus) = std(data_subj_stim.currRt);
            
        end
        
    end
    
end

%% Test group differences

[h,p,ci,stats] = ttest2(sd_rt_tot_mean{1},sd_rt_tot_mean{2});
            
%% Save the output

sd_rt = cell(9,3);
sd_rt{1,2} = 'ASD';
sd_rt{1,3} = 'TD';
sd_rt{2,1} = 'Mean rt over all stimuli';
sd_rt{2,2} = sd_rt_tot_mean{1};
sd_rt{2,3} = sd_rt_tot_mean{2};
sd_rt{3,1} = 'SDs over all stimuli';
sd_rt{3,2} = sd_rt_tot_sd{1};
sd_rt{3,3} = sd_rt_tot_sd{2};
sd_rt{4,1} = 'Mean rt per stimulus level';
sd_rt{4,2} = sd_rt_stim_mean{1};
sd_rt{4,3} = sd_rt_stim_mean{2};
sd_rt{5,1} = 'SDs per stimulus level';
sd_rt{5,2} = sd_rt_stim_sd{1};
sd_rt{5,3} = sd_rt_stim_sd{2};
sd_rt{6,1} = 'T-test mean rt';
sd_rt{6,2} = 'h';
sd_rt{6,3} = h;
sd_rt{7,2} = 'p';
sd_rt{7,3} = p;
sd_rt{8,2} = 'ci';
sd_rt{8,3} = ci;
sd_rt{9,2} = 'stats';
sd_rt{9,3} = stats;

save('P:/3018029.04/Analysis Scripts/Export/sd_rt.mat','sd_rt');

% add to table 
rt_means_tae_sd = readtable('P:/3018029.04/Analysis Scripts/Export/rt_means_tae_sd.csv');
sd_rt_mean = [sd_rt_tot_mean{1}; sd_rt_tot_mean{2}];
rt_means_tae_sd = [rt_means_tae_sd table(sd_rt_mean)];
writetable(rt_means_tae_sd, 'P:/3018029.04/Analysis Scripts/Export/rt_means_tae_sd.csv');

