clear all

% means [S, T, H]
mu = [15 15 15];

% Standard deviations [S, T, H]
s = [ 3 ; 3 ; 3 ];

% Input Corrs
R = {};

for i = 1:100 %for i simulations
    inputCorr = i*.01; %Only need to use if looping through input corrs.
    
  %correlation matrix [S, T, H] %positive intercorrelations
    R{i} = [ 1.0, 0.5, 0.5
             0.5, 1.0, 0.5
             0.5, 0.5, 1.0 ];
          
%     UNIFORM ZERO %comment in to run simulation with full independence
%     correlation matrix [S, T, H] 
%     R{i} = [ 1.0, 0.0, 0.0
%              0.0, 1.0, 0.0
%              0.0, 0.0, 1.0 ];
% 
%     DIVERGING rS,H; rS,T %unused
%     correlation matrix [S, T, H] 
%     R{i} = [ 1.0, 0, inputCorr
%              0, 1.0, -inputCorr
%              inputCorr, -inputCorr, 1.0 ];
% 
%     VARYING POSITIVE INPUT CORRS %unused
%     correlation matrix [S, T, H] 
%     R{i} = [ 1.0, inputCorr, inputCorr
%              inputCorr, 1.0, inputCorr
%              inputCorr, inputCorr, 1.0 ];


end %for i

%% Simulate datapoints

% Variance matrix (outer product):
V = s*s';
r = {};

for i = 1:100 %for i simulations
% Co-variance matrix:
covariance = V.*R{i};

% Generate Samples
n = 1000; %n per simulation
r{i} = mvnrnd( mu, covariance, n ); %full cell of all observations
r{i} = round(r{i}); %round to whole numbers

% View descriptives
maxVal{i} = max(r{i});
minVal{i} = min(r{i});
medians{i} = median(r{i});
means{i} = mean(r{i});
stdevs{i} = std(r{i});
corrs{i} = corrcoef(r{i});

end %for multivariate

%% Compute Difference Score and Regression Terms

%define cells for storage over loops
S_T = {};
betas = {};%[B1 B2]
betaDiff = {}; %B1 - B2
diffscoreCorr = {}; %correlation between S-T and criterion H
STmean = {}; %Mean S - T for difference score analysis
diffScoreSE = {}; %dummy code: diff score self-enhancement observed or not
humbergSE = {}; %categorized Humberg self-enhancement observed or not
posSelfView = {}; %%categorized Humberg PSV observed or not

for i = 1:100 %for i simulations
    
    %extract S, T, H and S-T into vectors
    S{i} = r{i}(:,1);
    T{i} = r{i}(:,2);
    H{i} = r{i}(:,3);
    S_T{i} = r{i}(:,1) - r{i}(:,2);
    
    %To use regress function, inputs must be a column of 1's followed by the actual
    %input. Criterion does not need column of 1's!

    S_pred{i} = [ones(n,1) S{i}]; %add vector of 1's for regress function
    T_pred{i} = [ones(n,1) T{i}]; %add vector of 1's for regress function
    Y{i} = H{i}; %establish criterion variable
    
    model{i} = regress(Y{i},[S_pred{i} T_pred{i}]); %regression model predicting H from S and T
    betas{i} = [model{i}(2,1) model{i}(4,1)]; %extract beta weights for S and T
    betaDiff{i} = betas{i}(1) - betas{i}(2); %compute difference between Betas
    diffscoreCorr{i} = corr(S_T{i},H{i}); %correlate criterion H with diff score self-enhancement
    
    %Categorize positive PSV (+1) or not (0)
    posSelfView{i} = (betas{i}(1) > 0);
    
    %Categorize into S-T self-enhancement (+1) or not (-1)
    STmean{i} = mean(S_T{i});
    
    if STmean{i} > 0; %if positive
    diffScoreSE{i} = 1;
    elseif STmean{i} < 0; %if negative
    diffScoreSE{i} = -1;
    else %if equal
    diffScoreSE{i} = 0; 
    end
    
%   Categorize into Humberg self-enhancement (+1), self-effacement (-1),
%   or neither (0)

    if (betas{i}(1) > 0) && (betas{i}(2) < 0); %if B1 pos and B2 neg
    humbergSE{i} = 1;
    elseif (betas{i}(1) < 0) && (betas{i}(2) > 0); %if B1 neg and B2 pos
    humbergSE{i} = -1;
    else %all others
    humbergSE{i} = 0; 
    end
    
end

    %Correlation between Betas
    betaMatrix = betas'; %transpose to better visualize
    betaMatrix = cell2mat(betaMatrix); %put B1 and B2 into same matrix
    betaCorr = corr(betaMatrix); %correlate B1 and B2 OVER simulations

%compute correlations between various measures reported in manuscript
% r( r(S-T,H) , (BetaS - BetaT))
criterionCorr = corr(cell2mat(betaDiff)',cell2mat(diffscoreCorr)');
categoryCritCorr = corr(cell2mat(diffScoreSE)',(cell2mat(humbergSE)'));
independenceCorr = corr(cell2mat(posSelfView)',(cell2mat(humbergSE)'));



