clear all; close all;

addpath ../../Toolboxes/Palamedes

%% Define what to analyse

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

% Which plots?
plots_p_repeat = true;

%% REAL DATA %%

load('P:/3018029.09/data_sd_clean.mat')
folder_str = 'Clean data compared to simulated data';

%% Add row to data for choice repetition

choiceRepetition    = (data_sd.prevResponseDir == data_sd.currResponseDir); 
data_sd             = [data_sd, table(choiceRepetition)]; 

%% ========= P_repeats ========== %%

for iSubject = 1:length(subjects)
    
    %% Select the subject's data
    
    subject_data = data_sd(data_sd.subject == subjects(iSubject), :);
    
    stim_levels_all = table2array(unique(subject_data(:,'currRelativeCoherence')));
    %stim_levels     = stim_levels_all((size(stim_levels_all)/2+1):size(stim_levels_all),:);
    stim_levels = unique(abs(stim_levels_all)); % does the same thing, but perhaps more readable.
    
    %% p_repeat for previous stimulus level
    for iStimulus = 1:length(stim_levels)
        
        % Select data for PREVIOUS stimulus levels
        subject_data_stim = subject_data(subject_data.prevRelativeCoherenceAbs == stim_levels(iStimulus), :);
            
        % Calculate p_repeat for each current response separately
        subject_data_stim_0 = subject_data_stim(subject_data_stim.currResponseDir == 0, :);
        subject_data_stim_1 = subject_data_stim(subject_data_stim.currResponseDir == 1, :);
  
        p_repeat(iSubject,iStimulus) = (mean(subject_data_stim_0.choiceRepetition)+mean(subject_data_stim_1.choiceRepetition))/2;
        
        %% p_repeat for previous stim level, split for prev RT
        
        % Split for prev RT
        subject_data_stim_rtsorted = sortrows(subject_data_stim,'prevRt');
        length_rt = length(subject_data_stim_rtsorted.prevRt);
        %half_length_rt = length(subject_data_stim_rtsorted.prevRt)/2;
        half_length_rt = floor(length(subject_data_stim_rtsorted.prevRt)/2);
        %{
        if isinteger(half_length_rt)
            % Low prev RTs
            subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:half_length_rt,:);
            % High prev RTs
            subject_data_stim_prev_highRt = subject_data_stim_rtsorted((half_length_rt+1):length_rt,:);
        else
            % Low prev RTs
            subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:floor(half_length_rt),:);
            % High prev RTs
            subject_data_stim_prev_highRt = subject_data_stim_rtsorted(ceil(half_length_rt):length_rt,:);
        end
        %}
        
        % Low prev RTs
        subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:half_length_rt,:);
        % High prev RTs
        subject_data_stim_prev_highRt = subject_data_stim_rtsorted((half_length_rt+1):length_rt,:);
        
        
        % Calculate p_repeat for each prev stimulus level, for prev low RTs
        subject_data_stim_prev_lowRt_0 = subject_data_stim_prev_lowRt( ...
            subject_data_stim_prev_lowRt.currResponseDir == 0, :);
        subject_data_stim_prev_lowRt_1 = subject_data_stim_prev_lowRt( ...
            subject_data_stim_prev_lowRt.currResponseDir == 1, :);

        p_repeat_lowRt(iSubject,iStimulus) = (mean(subject_data_stim_prev_lowRt_0.choiceRepetition)+mean(subject_data_stim_prev_lowRt_1.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev high RTs
        subject_data_stim_prev_highRt_0 = subject_data_stim_prev_highRt( ...
            subject_data_stim_prev_highRt.currResponseDir == 0, :);
        subject_data_stim_prev_highRt_1 = subject_data_stim_prev_highRt( ...
            subject_data_stim_prev_highRt.currResponseDir == 1, :);

        p_repeat_highRt(iSubject,iStimulus) = (mean(subject_data_stim_prev_highRt_0.choiceRepetition)+mean(subject_data_stim_prev_highRt_1.choiceRepetition))/2;
        
        
        %% p_repeat for previous stim level, split for prev correctness
        
        % Split data into previous incorrect and previous correct
        subject_data_stim_prev_incorr   = subject_data_stim(subject_data_stim.prevCorrect == 0, :);
        subject_data_stim_prev_corr     = subject_data_stim(subject_data_stim.prevCorrect == 1, :);
        
        % Calculate p_repeat for each prev stimulus level, for prev
        % incorrect
        subject_data_stim_prev_incorr_0 = subject_data_stim_prev_incorr( ...
            subject_data_stim_prev_incorr.currResponseDir == 0, :);
        subject_data_stim_prev_incorr_1 = subject_data_stim_prev_incorr( ...
            subject_data_stim_prev_incorr.currResponseDir == 1, :);
        
        p_repeat_prev_incorr(iSubject,iStimulus) = (mean(subject_data_stim_prev_incorr_0.choiceRepetition)+mean(subject_data_stim_prev_incorr_1.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev correct
        subject_data_stim_prev_corr_0 = subject_data_stim_prev_corr( ...
            subject_data_stim_prev_corr.currResponseDir == 0, :);
        subject_data_stim_prev_corr_1 = subject_data_stim_prev_corr( ...
            subject_data_stim_prev_corr.currResponseDir == 1, :);

        p_repeat_prev_corr(iSubject,iStimulus) = (mean(subject_data_stim_prev_corr_0.choiceRepetition)+mean(subject_data_stim_prev_corr_1.choiceRepetition))/2;
        
        %% p_repeat for previous stim level, split for prev confidence
        
        % Split data into previous low and high confidence
        subject_data_stim_prev_lowConf      = subject_data_stim(subject_data_stim.prevConfidence <= 2, :);
        subject_data_stim_prev_highConf     = subject_data_stim(subject_data_stim.prevConfidence >= 3, :);
        
        % Calculate p_repeat for each prev stimulus level, for prev
        % low conf
        subject_data_stim_prev_lowConf_0 = subject_data_stim_prev_lowConf( ...
            subject_data_stim_prev_lowConf.currResponseDir == 0, :);
        subject_data_stim_prev_lowConf_1 = subject_data_stim_prev_lowConf( ...
            subject_data_stim_prev_lowConf.currResponseDir == 1, :);
        
        p_repeat_prev_lowConf(iSubject,iStimulus) = (mean(subject_data_stim_prev_lowConf_0.choiceRepetition)+mean(subject_data_stim_prev_lowConf_1.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev 
        % high conf
        subject_data_stim_prev_highConf_0 = subject_data_stim_prev_highConf( ...
            subject_data_stim_prev_highConf.currResponseDir == 0, :);
        subject_data_stim_prev_highConf_1 = subject_data_stim_prev_highConf( ...
            subject_data_stim_prev_highConf.currResponseDir == 1, :);

        p_repeat_prev_highConf(iSubject,iStimulus) = (mean(subject_data_stim_prev_highConf_0.choiceRepetition)+mean(subject_data_stim_prev_highConf_1.choiceRepetition))/2;
        
        
        %% p_repeat for previous stim level, split for prev confidence
        
        % Figure out the most equal split
        for i = 1:3
            temp(i) = abs(sum(subject_data_stim.prevConfidence <= i) - sum(subject_data_stim.prevConfidence >= i+1));
        end

        [~,crit] = min(temp);
        
        % Split data into previous low and high confidence
        subject_data_stim_prev_lowConf_2      = subject_data_stim(subject_data_stim.prevConfidence <= crit, :);
        subject_data_stim_prev_highConf_2     = subject_data_stim(subject_data_stim.prevConfidence >= crit+1, :);
        
        % Calculate p_repeat for each prev stimulus level, for prev
        % incorrect
        subject_data_stim_prev_lowConf_0_2 = subject_data_stim_prev_lowConf_2( ...
            subject_data_stim_prev_lowConf_2.currResponseDir == 0, :);
        subject_data_stim_prev_lowConf_1_2 = subject_data_stim_prev_lowConf_2( ...
            subject_data_stim_prev_lowConf_2.currResponseDir == 1, :);
        
        p_repeat_prev_lowConf_2(iSubject,iStimulus) = (mean(subject_data_stim_prev_lowConf_0_2.choiceRepetition)+mean(subject_data_stim_prev_lowConf_1_2.choiceRepetition))/2;
        
        % Calculate p_repeat for each prev stimulus level, for prev correct
        subject_data_stim_prev_highConf_0_2 = subject_data_stim_prev_highConf_2( ...
            subject_data_stim_prev_highConf_2.currResponseDir == 0, :);
        subject_data_stim_prev_highConf_1_2 = subject_data_stim_prev_highConf_2( ...
            subject_data_stim_prev_highConf_2.currResponseDir == 1, :);

        p_repeat_prev_highConf_2(iSubject,iStimulus) = (mean(subject_data_stim_prev_highConf_0_2.choiceRepetition)+mean(subject_data_stim_prev_highConf_1_2.choiceRepetition))/2;
        
        
        %% p_repeat for previous stim level, split for prev confidence
       
        for conf = 1:4
            
            % Select data for one prev confidence level
            subject_data_stim_prev_conf{conf} = subject_data_stim(subject_data_stim.prevConfidence == conf, :);
        
            % Calculate p_repeat for each prev stimulus level, for prev
            % incorrect
            subject_data_stim_prev_conf_0{conf} = subject_data_stim_prev_conf{conf}( ...
                subject_data_stim_prev_conf{conf}.currResponseDir == 0, :);
            subject_data_stim_prev_conf_1{conf} = subject_data_stim_prev_conf{conf}( ...
                subject_data_stim_prev_conf{conf}.currResponseDir == 1, :);
        
            p_repeat_prev_conf{conf}(iSubject,iStimulus) = (mean(subject_data_stim_prev_conf_0{conf}.choiceRepetition)+mean(subject_data_stim_prev_conf_1{conf}.choiceRepetition))/2;
        
        end

    end
end


%% Group means and ses

for iStimulus = 1:length(stim_levels) 
    
    p_repeat_mean(1,iStimulus)              = mean(p_repeat(:,iStimulus));                               
        p_repeat_se(1,iStimulus)            = std(p_repeat(:,iStimulus))/sqrt(length(p_repeat(:,iStimulus)));
    p_repeat_lowRt_mean(1,iStimulus)        = mean(p_repeat_lowRt(:,iStimulus));
        p_repeat_lowRt_se(1,iStimulus)      = std(p_repeat_lowRt(:,iStimulus))/sqrt(length(p_repeat_lowRt(:,iStimulus)));
    p_repeat_highRt_mean(1,iStimulus)       = mean(p_repeat_highRt(:,iStimulus));
        p_repeat_highRt_se(1,iStimulus)     = std(p_repeat_highRt(:,iStimulus))/sqrt(length(p_repeat_highRt(:,iStimulus)));
    p_repeat_incorr_mean(1,iStimulus)       = nanmean(p_repeat_prev_incorr(:,iStimulus));
        p_repeat_incorr_se(1,iStimulus)     = nanstd(p_repeat_prev_incorr(:,iStimulus))/sqrt(length(p_repeat_prev_incorr(~isnan(p_repeat_prev_incorr(:,iStimulus)),iStimulus)));
    p_repeat_corr_mean(1,iStimulus)         = nanmean(p_repeat_prev_corr(:,iStimulus));
        p_repeat_corr_se(1,iStimulus)       = nanstd(p_repeat_prev_corr(:,iStimulus))/sqrt(length(p_repeat_prev_corr(~isnan(p_repeat_prev_corr(:,iStimulus)),iStimulus)));
    p_repeat_lowConf_mean(1,iStimulus)      = nanmean(p_repeat_prev_lowConf(:,iStimulus));
        p_repeat_lowConf_se(1,iStimulus)    = nanstd(p_repeat_prev_lowConf(:,iStimulus))/sqrt(length(p_repeat_prev_lowConf(~isnan(p_repeat_prev_lowConf(:,iStimulus)),iStimulus)));
    p_repeat_highConf_mean(1,iStimulus)     = nanmean(p_repeat_prev_highConf(:,iStimulus));
        p_repeat_highConf_se(1,iStimulus)   = nanstd(p_repeat_prev_highConf(:,iStimulus))/sqrt(length(p_repeat_prev_highConf(~isnan(p_repeat_prev_highConf(:,iStimulus)),iStimulus)));
    p_repeat_lowConf_mean_2(1,iStimulus)    = nanmean(p_repeat_prev_lowConf_2(:,iStimulus));
        p_repeat_lowConf_se_2(1,iStimulus)    = nanstd(p_repeat_prev_lowConf_2(:,iStimulus))/sqrt(length(p_repeat_prev_lowConf_2(~isnan(p_repeat_prev_lowConf_2(:,iStimulus)),iStimulus)));
    p_repeat_highConf_mean_2(1,iStimulus)     = nanmean(p_repeat_prev_highConf_2(:,iStimulus));
        p_repeat_highConf_se_2(1,iStimulus)   = nanstd(p_repeat_prev_highConf_2(:,iStimulus))/sqrt(length(p_repeat_prev_highConf_2(~isnan(p_repeat_prev_highConf_2(:,iStimulus)),iStimulus)));

    for conf = 1:4
        p_repeat_conf_mean{conf}(1,iStimulus) = nanmean(p_repeat_prev_conf{conf}(:,iStimulus));
        p_repeat_conf_se{conf}(1,iStimulus)   = nanstd(p_repeat_prev_conf{conf}(:,iStimulus))/sqrt(length(p_repeat_prev_conf{conf}(~isnan(p_repeat_prev_conf{conf}(:,iStimulus)),iStimulus)));

    end

end

%{
   % cut 6th stimulus level out of p_repeat_incorr_mean, because of too
    % little data going into it
    p_repeat_incorr_mean    = p_repeat_incorr_mean(:, 1:end-1);
    p_repeat_incorr_se      = p_repeat_incorr_se(:, 1:end-1);
  %}
  

%% SIMULATED DATA %%

for iTeration = 1:1000
    
    load('P:/3018029.09/data_sd_clean.mat')
    load('P:/3018029.09/simulatedResponses_1000_iterations.mat')
    
    data_sd.currResponseDir = simulatedResponses(:,iTeration);
    
    
    %% Add row to data for choice repetition

    choiceRepetition    = (data_sd.prevResponseDir == data_sd.currResponseDir); 
    data_sd             = [data_sd, table(choiceRepetition)]; 

    %% ========= P_repeats ========== %%

    for iSubject = 1:length(subjects)

        %% Select the subject's data

        subject_data    = data_sd(data_sd.subject == subjects(iSubject), :);

        stim_levels_all = table2array(unique(subject_data(:,'currRelativeCoherence')));
        stim_levels     = unique(abs(stim_levels_all)); % does the same thing, but perhaps more readable.

        %% p_repeat for previous stimulus level
        for iStimulus = 1:length(stim_levels)

            % Select data for PREVIOUS stimulus levels
            subject_data_stim = subject_data(subject_data.prevRelativeCoherenceAbs == stim_levels(iStimulus), :);

            % Calculate p_repeat for each current response separately
            subject_data_stim_0 = subject_data_stim(subject_data_stim.currResponseDir == 0, :);
            subject_data_stim_1 = subject_data_stim(subject_data_stim.currResponseDir == 1, :);

            p_repeat(iSubject,iStimulus) = (mean(subject_data_stim_0.choiceRepetition)+mean(subject_data_stim_1.choiceRepetition))/2;

            %% p_repeat for previous stim level, split for prev RT

            % Split for prev RT
            subject_data_stim_rtsorted = sortrows(subject_data_stim,'prevRt');
            length_rt = length(subject_data_stim_rtsorted.prevRt);
            %half_length_rt = length(subject_data_stim_rtsorted.prevRt)/2;
            half_length_rt = floor(length(subject_data_stim_rtsorted.prevRt)/2);
            %{
            if isinteger(half_length_rt)
                % Low prev RTs
                subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:half_length_rt,:);
                % High prev RTs
                subject_data_stim_prev_highRt = subject_data_stim_rtsorted((half_length_rt+1):length_rt,:);
            else
                % Low prev RTs
                subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:floor(half_length_rt),:);
                % High prev RTs
                subject_data_stim_prev_highRt = subject_data_stim_rtsorted(ceil(half_length_rt):length_rt,:);
            end
            %}

            % Low prev RTs
            subject_data_stim_prev_lowRt = subject_data_stim_rtsorted(1:half_length_rt,:);
            % High prev RTs
            subject_data_stim_prev_highRt = subject_data_stim_rtsorted((half_length_rt+1):length_rt,:);


            % Calculate p_repeat for each prev stimulus level, for prev low RTs
            subject_data_stim_prev_lowRt_0 = subject_data_stim_prev_lowRt( ...
                subject_data_stim_prev_lowRt.currResponseDir == 0, :);
            subject_data_stim_prev_lowRt_1 = subject_data_stim_prev_lowRt( ...
                subject_data_stim_prev_lowRt.currResponseDir == 1, :);

            p_repeat_lowRt(iSubject,iStimulus) = (mean(subject_data_stim_prev_lowRt_0.choiceRepetition)+mean(subject_data_stim_prev_lowRt_1.choiceRepetition))/2;

            % Calculate p_repeat for each prev stimulus level, for prev high RTs
            subject_data_stim_prev_highRt_0 = subject_data_stim_prev_highRt( ...
                subject_data_stim_prev_highRt.currResponseDir == 0, :);
            subject_data_stim_prev_highRt_1 = subject_data_stim_prev_highRt( ...
                subject_data_stim_prev_highRt.currResponseDir == 1, :);

            p_repeat_highRt(iSubject,iStimulus) = (mean(subject_data_stim_prev_highRt_0.choiceRepetition)+mean(subject_data_stim_prev_highRt_1.choiceRepetition))/2;


            %% p_repeat for previous stim level, split for prev correctness

            % Split data into previous incorrect and previous correct
            subject_data_stim_prev_incorr   = subject_data_stim(subject_data_stim.prevCorrect == 0, :);
            subject_data_stim_prev_corr     = subject_data_stim(subject_data_stim.prevCorrect == 1, :);

            % Calculate p_repeat for each prev stimulus level, for prev
            % incorrect
            subject_data_stim_prev_incorr_0 = subject_data_stim_prev_incorr( ...
                subject_data_stim_prev_incorr.currResponseDir == 0, :);
            subject_data_stim_prev_incorr_1 = subject_data_stim_prev_incorr( ...
                subject_data_stim_prev_incorr.currResponseDir == 1, :);

            p_repeat_prev_incorr(iSubject,iStimulus) = (mean(subject_data_stim_prev_incorr_0.choiceRepetition)+mean(subject_data_stim_prev_incorr_1.choiceRepetition))/2;

            % Calculate p_repeat for each prev stimulus level, for prev correct
            subject_data_stim_prev_corr_0 = subject_data_stim_prev_corr( ...
                subject_data_stim_prev_corr.currResponseDir == 0, :);
            subject_data_stim_prev_corr_1 = subject_data_stim_prev_corr( ...
                subject_data_stim_prev_corr.currResponseDir == 1, :);

            p_repeat_prev_corr(iSubject,iStimulus) = (mean(subject_data_stim_prev_corr_0.choiceRepetition)+mean(subject_data_stim_prev_corr_1.choiceRepetition))/2;

            %% p_repeat for previous stim level, split for prev confidence

            % Split data into previous low and high confidence
            subject_data_stim_prev_lowConf      = subject_data_stim(subject_data_stim.prevConfidence <= 2, :);
            subject_data_stim_prev_highConf     = subject_data_stim(subject_data_stim.prevConfidence >= 3, :);

            % Calculate p_repeat for each prev stimulus level, for prev
            % low conf
            subject_data_stim_prev_lowConf_0 = subject_data_stim_prev_lowConf( ...
                subject_data_stim_prev_lowConf.currResponseDir == 0, :);
            subject_data_stim_prev_lowConf_1 = subject_data_stim_prev_lowConf( ...
                subject_data_stim_prev_lowConf.currResponseDir == 1, :);

            p_repeat_prev_lowConf(iSubject,iStimulus) = (mean(subject_data_stim_prev_lowConf_0.choiceRepetition)+mean(subject_data_stim_prev_lowConf_1.choiceRepetition))/2;

            % Calculate p_repeat for each prev stimulus level, for prev 
            % high conf
            subject_data_stim_prev_highConf_0 = subject_data_stim_prev_highConf( ...
                subject_data_stim_prev_highConf.currResponseDir == 0, :);
            subject_data_stim_prev_highConf_1 = subject_data_stim_prev_highConf( ...
                subject_data_stim_prev_highConf.currResponseDir == 1, :);

            p_repeat_prev_highConf(iSubject,iStimulus) = (mean(subject_data_stim_prev_highConf_0.choiceRepetition)+mean(subject_data_stim_prev_highConf_1.choiceRepetition))/2;


            %% p_repeat for previous stim level, split for prev confidence - FLEX SPLIT

            % Figure out the most equal split
            for i = 1:3
                temp(i) = abs(sum(subject_data_stim.prevConfidence <= i) - sum(subject_data_stim.prevConfidence >= i+1));
            end

            [~,crit] = min(temp);

            % Split data into previous low and high confidence
            subject_data_stim_prev_lowConf_2      = subject_data_stim(subject_data_stim.prevConfidence <= crit, :);
            subject_data_stim_prev_highConf_2     = subject_data_stim(subject_data_stim.prevConfidence >= crit+1, :);

            % Calculate p_repeat for each prev stimulus level, for prev
            % incorrect
            subject_data_stim_prev_lowConf_0_2 = subject_data_stim_prev_lowConf_2( ...
                subject_data_stim_prev_lowConf_2.currResponseDir == 0, :);
            subject_data_stim_prev_lowConf_1_2 = subject_data_stim_prev_lowConf_2( ...
                subject_data_stim_prev_lowConf_2.currResponseDir == 1, :);

            p_repeat_prev_lowConf_2(iSubject,iStimulus) = (mean(subject_data_stim_prev_lowConf_0_2.choiceRepetition)+mean(subject_data_stim_prev_lowConf_1_2.choiceRepetition))/2;

            % Calculate p_repeat for each prev stimulus level, for prev correct
            subject_data_stim_prev_highConf_0_2 = subject_data_stim_prev_highConf_2( ...
                subject_data_stim_prev_highConf_2.currResponseDir == 0, :);
            subject_data_stim_prev_highConf_1_2 = subject_data_stim_prev_highConf_2( ...
                subject_data_stim_prev_highConf_2.currResponseDir == 1, :);

            p_repeat_prev_highConf_2(iSubject,iStimulus) = (mean(subject_data_stim_prev_highConf_0_2.choiceRepetition)+mean(subject_data_stim_prev_highConf_1_2.choiceRepetition))/2;


            %% p_repeat for previous stim level, split for prev confidence - 4 LEVELS

            for conf = 1:4

                % Select data for one prev confidence level
                subject_data_stim_prev_conf{conf} = subject_data_stim(subject_data_stim.prevConfidence == conf, :);

                % Calculate p_repeat for each prev stimulus level, for prev
                % incorrect
                subject_data_stim_prev_conf_0{conf} = subject_data_stim_prev_conf{conf}( ...
                    subject_data_stim_prev_conf{conf}.currResponseDir == 0, :);
                subject_data_stim_prev_conf_1{conf} = subject_data_stim_prev_conf{conf}( ...
                    subject_data_stim_prev_conf{conf}.currResponseDir == 1, :);

                p_repeat_prev_conf{conf}(iSubject,iStimulus) = (mean(subject_data_stim_prev_conf_0{conf}.choiceRepetition)+mean(subject_data_stim_prev_conf_1{conf}.choiceRepetition))/2;

            end

        end
    end
   
    %% Group means and ses

    for iStimulus = 1:length(stim_levels) 

        p_repeat_sim_mean(iTeration,iStimulus)              = mean(p_repeat(:,iStimulus));                               
            p_repeat_sim_se(iTeration,iStimulus)            = std(p_repeat(:,iStimulus))/sqrt(length(p_repeat(:,iStimulus)));
        p_repeat_lowRt_sim_mean(iTeration,iStimulus)        = mean(p_repeat_lowRt(:,iStimulus));
            p_repeat_lowRt_sim_se(iTeration,iStimulus)      = std(p_repeat_lowRt(:,iStimulus))/sqrt(length(p_repeat_lowRt(:,iStimulus)));
        p_repeat_highRt_sim_mean(iTeration,iStimulus)       = mean(p_repeat_highRt(:,iStimulus));
            p_repeat_highRt_sim_se(iTeration,iStimulus)     = std(p_repeat_highRt(:,iStimulus))/sqrt(length(p_repeat_highRt(:,iStimulus)));
        p_repeat_incorr_sim_mean(iTeration,iStimulus)       = nanmean(p_repeat_prev_incorr(:,iStimulus));
            p_repeat_incorr_sim_se(iTeration,iStimulus)     = nanstd(p_repeat_prev_incorr(:,iStimulus))/sqrt(length(p_repeat_prev_incorr(~isnan(p_repeat_prev_incorr(:,iStimulus)),iStimulus)));
        p_repeat_corr_sim_mean(iTeration,iStimulus)         = nanmean(p_repeat_prev_corr(:,iStimulus));
            p_repeat_corr_sim_se(iTeration,iStimulus)       = nanstd(p_repeat_prev_corr(:,iStimulus))/sqrt(length(p_repeat_prev_corr(~isnan(p_repeat_prev_corr(:,iStimulus)),iStimulus)));
        p_repeat_lowConf_sim_mean(iTeration,iStimulus)      = nanmean(p_repeat_prev_lowConf(:,iStimulus));
            p_repeat_lowConf_sim_se(iTeration,iStimulus)    = nanstd(p_repeat_prev_lowConf(:,iStimulus))/sqrt(length(p_repeat_prev_lowConf(~isnan(p_repeat_prev_lowConf(:,iStimulus)),iStimulus)));
        p_repeat_highConf_sim_mean(iTeration,iStimulus)     = nanmean(p_repeat_prev_highConf(:,iStimulus));
            p_repeat_highConf_sim_se(iTeration,iStimulus)   = nanstd(p_repeat_prev_highConf(:,iStimulus))/sqrt(length(p_repeat_prev_highConf(~isnan(p_repeat_prev_highConf(:,iStimulus)),iStimulus)));
        p_repeat_lowConf_sim_mean_2(iTeration,iStimulus)    = nanmean(p_repeat_prev_lowConf_2(:,iStimulus));
            p_repeat_lowConf_sim_se_2(iTeration,iStimulus)    = nanstd(p_repeat_prev_lowConf_2(:,iStimulus))/sqrt(length(p_repeat_prev_lowConf_2(~isnan(p_repeat_prev_lowConf_2(:,iStimulus)),iStimulus)));
        p_repeat_highConf_sim_mean_2(iTeration,iStimulus)     = nanmean(p_repeat_prev_highConf_2(:,iStimulus));
            p_repeat_highConf_sim_se_2(iTeration,iStimulus)   = nanstd(p_repeat_prev_highConf_2(:,iStimulus))/sqrt(length(p_repeat_prev_highConf_2(~isnan(p_repeat_prev_highConf_2(:,iStimulus)),iStimulus)));

        for conf = 1:4
            p_repeat_conf_sim_mean{conf}(iTeration,iStimulus) = nanmean(p_repeat_prev_conf{conf}(:,iStimulus));
            p_repeat_conf_sim_se{conf}(iTeration,iStimulus)   = nanstd(p_repeat_prev_conf{conf}(:,iStimulus))/sqrt(length(p_repeat_prev_conf{conf}(~isnan(p_repeat_prev_conf{conf}(:,iStimulus)),iStimulus)));

        end

    end
        
%{
   % cut 6th stimulus level out of p_repeat_incorr_mean, because of too
    % little data going into it
    p_repeat_incorr_mean    = p_repeat_incorr_mean(:, 1:end-1);
    p_repeat_incorr_se      = p_repeat_incorr_se(:, 1:end-1);
  %}
    
end

%% Overall means

p_repeat_sim_overall_mean              = mean(p_repeat_sim_mean);                               
    p_repeat_sim_overall_se            = std(p_repeat_sim_mean)/sqrt(length(p_repeat_sim_mean));
p_repeat_lowRt_sim_overall_mean        = mean(p_repeat_lowRt_sim_mean);
    p_repeat_lowRt_sim_overall_se      = std(p_repeat_lowRt_sim_mean)/sqrt(length(p_repeat_lowRt_sim_mean));
p_repeat_highRt_sim_overall_mean       = mean(p_repeat_highRt_sim_mean);
    p_repeat_highRt_sim_overall_se     = std(p_repeat_highRt_sim_mean)/sqrt(length(p_repeat_highRt_sim_mean));
p_repeat_incorr_sim_overall_mean       = nanmean(p_repeat_incorr_sim_mean);
    p_repeat_incorr_sim_overall_se     = nanstd(p_repeat_incorr_sim_mean)/sqrt(length(p_repeat_incorr_sim_mean(~isnan(p_repeat_incorr_sim_mean))));
p_repeat_corr_sim_overall_mean         = nanmean(p_repeat_corr_sim_mean);
    p_repeat_corr_sim_overall_se       = nanstd(p_repeat_corr_sim_mean)/sqrt(length(p_repeat_corr_sim_mean(~isnan(p_repeat_corr_sim_mean))));
p_repeat_lowConf_sim_overall_mean      = nanmean(p_repeat_lowConf_sim_mean);
    p_repeat_lowConf_sim_overall_se    = nanstd(p_repeat_lowConf_sim_mean)/sqrt(length(p_repeat_lowConf_sim_mean(~isnan(p_repeat_lowConf_sim_mean))));
p_repeat_highConf_sim_overall_mean     = nanmean(p_repeat_highConf_sim_mean); 
    p_repeat_highConf_sim_overall_se   = nanstd(p_repeat_highConf_sim_mean)/sqrt(length(p_repeat_highConf_sim_mean(~isnan(p_repeat_highConf_sim_mean))));
p_repeat_lowConf_sim_overall_mean_2    = nanmean(p_repeat_lowConf_sim_mean_2);  
    p_repeat_lowConf_sim_overall_se_2    = nanstd(p_repeat_lowConf_sim_mean_2)/sqrt(length(p_repeat_lowConf_sim_mean_2(~isnan(p_repeat_lowConf_sim_mean_2))));
p_repeat_highConf_sim_overall_mean_2     = nanmean(p_repeat_highConf_sim_mean_2);
    p_repeat_highConf_sim_overall_se_2   = nanstd(p_repeat_highConf_sim_mean_2)/sqrt(length(p_repeat_highConf_sim_mean_2(~isnan(p_repeat_highConf_sim_mean_2))));
%{
for conf = 1:4
    p_repeat_conf_sim_mean_mean{conf} = nanmean(p_repeat_conf_sim_mean{conf});
    p_repeat_conf_sim_mean_se{conf}   = nanstd(p_repeat_conf_sim_mean{conf})/sqrt(length(p_repeat_conf_sim_mean{conf}(~isnan(p_repeat_conf_sim_mean{conf})));

end
%}
    
% Confidence intervals

temp = sort(p_repeat_sim_mean);
    p_repeat_sim_overall_CI(1,:)            = temp(25,:);
    p_repeat_sim_overall_CI(2,:)            = temp(975,:);
temp = sort(p_repeat_lowRt_sim_mean);
    p_repeat_lowRt_sim_overall_CI(1,:)      = temp(25,:);
    p_repeat_lowRt_sim_overall_CI(2,:)      = temp(975,:);
temp = sort(p_repeat_highRt_sim_mean);
    p_repeat_highRt_sim_overall_CI(1,:)     = temp(25,:);
    p_repeat_highRt_sim_overall_CI(2,:)     = temp(975,:);
temp = sort(p_repeat_incorr_sim_mean);
    p_repeat_incorr_sim_overall_CI(1,:)     = temp(25,:);
    p_repeat_incorr_sim_overall_CI(2,:)     = temp(975,:);
temp = sort(p_repeat_corr_sim_mean);
    p_repeat_corr_sim_overall_CI(1,:)       = temp(25,:);
    p_repeat_corr_sim_overall_CI(2,:)       = temp(975,:);
temp = sort(p_repeat_lowConf_sim_mean);
    p_repeat_lowConf_sim_overall_CI(1,:)    = temp(25,:);
    p_repeat_lowConf_sim_overall_CI(2,:)    = temp(975,:);
temp = sort(p_repeat_highConf_sim_mean);
    p_repeat_highConf_sim_overall_CI(1,:)   = temp(25,:);
    p_repeat_highConf_sim_overall_CI(2,:)   = temp(975,:);
temp = sort(p_repeat_lowConf_sim_mean_2);
    p_repeat_lowConf_sim_overall_CI_2(1,:)  = temp(25,:);
    p_repeat_lowConf_sim_overall_CI_2(2,:)  = temp(975,:);
temp = sort(p_repeat_highConf_sim_mean_2);
    p_repeat_highConf_sim_overall_CI_2(1,:) = temp(25,:);
    p_repeat_highConf_sim_overall_CI_2(2,:) = temp(975,:);
    

%% Plots

if plots_p_repeat == true
    
    % p_repeat per previous stimulus level   
    figure; hold on;
        for iTeration = 1:1000
            plot(p_repeat_sim_mean(iTeration,:),'Color',[0.7 0.7 0.7])
        end
        errorbar(p_repeat_mean,p_repeat_se,'k'); 
        ylim([.42 .60]); 
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level.png'])

    % p_repeat per previous stimulus level   
    figure; hold on;
        plot(p_repeat_sim_overall_mean,'Color',[0.7 0.7 0.7])
            plot(p_repeat_sim_overall_CI(1,:),'--','Color',[0.7 0.7 0.7])
            plot(p_repeat_sim_overall_CI(2,:),'--','Color',[0.7 0.7 0.7])
        errorbar(p_repeat_mean,p_repeat_se,'k'); 
        ylim([.42 .60]); 
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level - sim mean.png'])
        
    % p_repeat per previous stimulus level, split for prev RT  
    figure; hold on;
        for iTeration = 1:1000
            plot(p_repeat_lowRt_sim_mean(iTeration,:),'Color',[0.7 0.7 1])
            plot(p_repeat_highRt_sim_mean(iTeration,:),'Color',[1 0.7 0.7])
        end
        x = plot(p_repeat_lowRt_mean,'b'); 
            errorbar(p_repeat_lowRt_mean,p_repeat_lowRt_se,'.b');
        y = plot(p_repeat_highRt_mean,'r');
            errorbar(p_repeat_highRt_mean,p_repeat_highRt_se,'.r');
        ylim([.42 .60]); 
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous fast RT', 'Previous slow RT');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev RT.png'])

    % p_repeat per previous stimulus level, split for prev RT  
    figure; hold on;
        plot(p_repeat_lowRt_sim_overall_mean,'Color',[0.7 0.7 1])
            plot(p_repeat_lowRt_sim_overall_CI(1,:),'--','Color',[0.7 0.7 1])
            plot(p_repeat_lowRt_sim_overall_CI(2,:),'--','Color',[0.7 0.7 1])
        plot(p_repeat_highRt_sim_overall_mean,'Color',[1 0.7 0.7])
            plot(p_repeat_highRt_sim_overall_CI(1,:),'--','Color',[1 0.7 0.7])
            plot(p_repeat_highRt_sim_overall_CI(2,:),'--','Color',[1 0.7 0.7])
        x = plot(p_repeat_lowRt_mean,'b'); 
            errorbar(p_repeat_lowRt_mean,p_repeat_lowRt_se,'.b');
        y = plot(p_repeat_highRt_mean,'r');
            errorbar(p_repeat_highRt_mean,p_repeat_highRt_se,'.r');
        ylim([.42 .60]); 
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous fast RT', 'Previous slow RT');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev RT - sim mean.png'])

    % p_repeat per previous stimulus level, split for prev correctness  
    figure; hold on;
        for iTeration = 1:1000
            plot(p_repeat_corr_sim_mean(iTeration,:),'Color',[0.7 0.7 1])
            plot(p_repeat_incorr_sim_mean(iTeration,:),'Color',[1 0.7 0.7])
        end
        x = plot(p_repeat_corr_mean,'b'); 
            errorbar(p_repeat_corr_mean,p_repeat_corr_se,'.b');
        y = plot(p_repeat_incorr_mean,'r'); 
            errorbar(p_repeat_incorr_mean,p_repeat_incorr_se,'.r');
        ylim([.42 .70]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous correct response', 'Previous incorrect response','Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev correctness.png'])

    % p_repeat per previous stimulus level, split for prev correctness  
    figure; hold on;
        plot(p_repeat_corr_sim_overall_mean,'Color',[0.7 0.7 1])
            plot(p_repeat_corr_sim_overall_CI(1,:),'--','Color',[0.7 0.7 1])
            plot(p_repeat_corr_sim_overall_CI(2,:),'--','Color',[0.7 0.7 1])
        plot(p_repeat_incorr_sim_overall_mean,'Color',[1 0.7 0.7])
            plot(p_repeat_incorr_sim_overall_CI(1,:),'--','Color',[1 0.7 0.7])
            plot(p_repeat_incorr_sim_overall_CI(2,:),'--','Color',[1 0.7 0.7])
        x = plot(p_repeat_corr_mean,'b'); 
            errorbar(p_repeat_corr_mean,p_repeat_corr_se,'.b');
        y = plot(p_repeat_incorr_mean,'r'); 
            errorbar(p_repeat_incorr_mean,p_repeat_incorr_se,'.r');
        ylim([.42 .70]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous correct response', 'Previous incorrect response','Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev correctness - sim mean.png'])
    
            
    % p_repeat per previous stimulus level, split for prev confidence  
    figure; hold on;
        for iTeration = 1:1000
            plot(p_repeat_lowConf_sim_mean(iTeration,:),'Color',[0.7 0.7 1])
            plot(p_repeat_highConf_sim_mean(iTeration,:),'Color',[1 0.7 0.7])
        end
        x = plot(p_repeat_lowConf_mean,'b'); 
            errorbar(p_repeat_lowConf_mean,p_repeat_lowConf_se,'.b');
        y = plot(p_repeat_highConf_mean,'r'); 
            errorbar(p_repeat_highConf_mean,p_repeat_highConf_se,'.r');
        ylim([.42 .60]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low confidence', 'Previous high confidence','Location','northeast');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev confidence.png'])

    % p_repeat per previous stimulus level, split for prev confidence  
    figure; hold on;
        plot(p_repeat_lowConf_sim_overall_mean,'Color',[0.7 0.7 1])
            plot(p_repeat_lowConf_sim_overall_CI(1,:),'--','Color',[0.7 0.7 1])
            plot(p_repeat_lowConf_sim_overall_CI(2,:),'--','Color',[0.7 0.7 1])
        plot(p_repeat_highConf_sim_overall_mean,'Color',[1 0.7 0.7])
            plot(p_repeat_highConf_sim_overall_CI(1,:),'--','Color',[1 0.7 0.7])
            plot(p_repeat_highConf_sim_overall_CI(2,:),'--','Color',[1 0.7 0.7])
        x = plot(p_repeat_lowConf_mean,'b'); 
            errorbar(p_repeat_lowConf_mean,p_repeat_lowConf_se,'.b');
        y = plot(p_repeat_highConf_mean,'r'); 
            errorbar(p_repeat_highConf_mean,p_repeat_highConf_se,'.r');
        ylim([.42 .60]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low confidence', 'Previous high confidence','Location','northeast');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev confidence - sim mean.png'])
    
        
    % p_repeat per previous stimulus level, split for prev confidence - FLEX SPLIT 
    figure; hold on;
        for iTeration = 1:1000
            plot(p_repeat_lowConf_sim_mean_2(iTeration,:),'Color',[0.7 0.7 1])
            plot(p_repeat_highConf_sim_mean_2(iTeration,:),'Color',[1 0.7 0.7])
        end
        x = plot(p_repeat_lowConf_mean_2,'b'); 
            errorbar(p_repeat_lowConf_mean_2,p_repeat_lowConf_se_2,'.b');
        y = plot(p_repeat_highConf_mean_2,'r'); 
            errorbar(p_repeat_highConf_mean_2,p_repeat_highConf_se_2,'.r');
        ylim([.42 .60]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low confidence', 'Previous high confidence','Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev confidence FLEX SPLIT.png'])

    % p_repeat per previous stimulus level, split for prev confidence -
    % FLEX SPLIT
    figure; hold on;
        plot(p_repeat_lowConf_sim_overall_mean_2,'Color',[0.7 0.7 1])
            plot(p_repeat_lowConf_sim_overall_CI_2(1,:),'--','Color',[0.7 0.7 1])
            plot(p_repeat_lowConf_sim_overall_CI_2(2,:),'--','Color',[0.7 0.7 1])
        plot(p_repeat_highConf_sim_overall_mean_2,'Color',[1 0.7 0.7])
            plot(p_repeat_highConf_sim_overall_CI_2(1,:),'--','Color',[1 0.7 0.7])
            plot(p_repeat_highConf_sim_overall_CI_2(2,:),'--','Color',[1 0.7 0.7])
        x = plot(p_repeat_lowConf_mean_2,'b'); 
            errorbar(p_repeat_lowConf_mean_2,p_repeat_lowConf_se_2,'.b');
        y = plot(p_repeat_highConf_mean_2,'r'); 
            errorbar(p_repeat_highConf_mean_2,p_repeat_highConf_se_2,'.r');
        ylim([.42 .60]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([x,y], 'Previous low confidence', 'Previous high confidence','Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Compared_p(repeat) per previous stimulus level split for prev confidence FLEX SPLIT - sim mean.png'])
       
    %{    
    % p_repeat per previous stimulus level, split for prev confidence 
    % 4 LEVELS
    figure; hold on;
        k = plot(p_repeat_conf_mean{1},'g'); 
            errorbar(p_repeat_conf_mean{1},p_repeat_conf_se{1},'.g');
        l = plot(p_repeat_conf_mean{2},'c'); 
            errorbar(p_repeat_conf_mean{2},p_repeat_conf_se{2},'.c');
        m = plot(p_repeat_conf_mean{3},'b'); 
            errorbar(p_repeat_conf_mean{3},p_repeat_conf_se{3},'.b');
        n = plot(p_repeat_conf_mean{4},'m'); 
            errorbar(p_repeat_conf_mean{4},p_repeat_conf_se{4},'.m');
        ylim([.4 .70]);
        xlim([0.25 6.75]); 
        set(gca,'XTick',1:7,'XTickLabel',{'smallest','','','','','largest'});
        xlabel('Previous relative stimulus coherence')
        ylabel('p(repeat)')
        legend([k,l,m,n], 'Prev confidence = 1', 'Prev confidence = 2', 'Prev confidence = 3', 'Prev confidence = 4', 'Location','northwest');
        title('Choice repetition per previous relative stimulus coherence');
        saveas(gcf,['P:/3018029.09/Graphs/p_repeat/' folder_str '/Group/Group_p(repeat) per previous stimulus level split for prev confidence 4 LEVELS.png'])
    %}
end