clear all; close all;

addpath ../../Toolboxes/Palamedes

%% Define what to analyse

% Which subjects?
subjects = [1:34];

% Which data?
load('P:/3018029.09/data.mat')

%% Output variables

rhoDiffRT   = [];     
rhoDiffConf = [];       
rhoRTConf   = [];

%% ========= Correlations ========== %%

for iSubject = 1:length(subjects)
    
    %% Select the subject's data
    
    subject_data = data(data.subject == subjects(iSubject), :);
    subject_data_array = table2array(subject_data); % Convert to array for easier selection
    
    stim_levels_all = table2array(unique(subject_data(:,'relativeCoherence')));
    stim_levels     = stim_levels_all((size(stim_levels_all)/2+1):size(stim_levels_all),:);
    
    stimLevelsFineGrain=[min(stim_levels_all):max(stim_levels_all)./1000:max(stim_levels_all)];
    
    %% Calculate correlation coefficients
    
    % difficulty and RT - Pearson's correlation
    
    % Create matrix of variables of interest
    matrix_relativeCoherenceAbs_rt = [table2array(subject_data(:,'relativeCoherenceAbs'))'; table2array(subject_data(:,'rt'))']';
    % Remove rows where RT is NaN, or else corrcoef won't work
    matrix_relativeCoherenceAbs_rt = matrix_relativeCoherenceAbs_rt(~isnan(matrix_relativeCoherenceAbs_rt(:,2)),:);
    % Calculate correlation coefficients
    corrDiffRT{iSubject} = corrcoef(matrix_relativeCoherenceAbs_rt);
    
    % difficulty and confidence - Spearman's correlation
    
    % Create matrix of variables of interest
    matrix_relativeCoherenceAbs_confidence = [table2array(subject_data(:,'relativeCoherenceAbs'))'; table2array(subject_data(:,'confidence'))']';
    % Remove rows where RT is NaN, or else corr won't work
    matrix_relativeCoherenceAbs_confidence = matrix_relativeCoherenceAbs_confidence(~isnan(matrix_relativeCoherenceAbs_confidence(:,2)),:);
    % Calculate correlation coefficients
    corrDiffConf{iSubject} = corr(matrix_relativeCoherenceAbs_confidence,'Type','Spearman');
    
    % RT and confidence - Spearman's correlation
    
    % Create matrix of variables of interest
    matrix_rt_confidence = [table2array(subject_data(:,'rt'))'; table2array(subject_data(:,'confidence'))']';
    % Remove rows where RT is NaN, or else corr won't work
    matrix_rt_confidence = matrix_rt_confidence(~isnan(matrix_rt_confidence(:,2)),:);
    % Calculate correlation coefficients
    corrRTConf{iSubject} = corr(matrix_rt_confidence,'Type','Spearman');
    
    % Take relevant variables of each
    rhoDiffRT   = [rhoDiffRT, corrDiffRT{iSubject}(1,2)];
    rhoDiffConf = [rhoDiffConf, corrDiffConf{iSubject}(1,2)];
    rhoRTConf   = [rhoRTConf, corrRTConf{iSubject}(1,2)];
    
end

% Calculate means and standard errors

rhoDiffRT_mean      = mean(rhoDiffRT);
    rhoDiffRT_sd = std(rhoDiffRT);
rhoDiffConf_mean    = mean(rhoDiffConf);
    rhoDiffConf_sd = std(rhoDiffConf);
rhoRTConf_mean      = mean(rhoRTConf);
    rhoRTConf_sd = std(rhoRTConf);
    
means = [rhoDiffRT_mean, rhoDiffConf_mean, rhoRTConf_mean];
sds = [rhoDiffRT_sd, rhoDiffConf_sd, rhoRTConf_sd];

%% Plot the group trends

% Mean response time per stimulus level, with standard errors
figure; hold on;
    title('Correlations')
    bar(means);
    errorbar(means,sds,'.'); 
    ylim([-1 1]); 
    set(gca,'XTick',1:3,'XTickLabel',{'Stim lvl x RT','Stim lvl x Conf', 'RT x Conf'});
    ylabel('Average correlation coefficient')
    hold off;
    saveas(gcf,['P:/3018029.09/Graphs/Sanity checks/Correlation coefficients.png'])
    
