-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_parameter_recovery_analysis.m
executable file
·238 lines (193 loc) · 9.64 KB
/
run_parameter_recovery_analysis.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
function recov_output=run_parameter_recovery_analysis
%Load in the paramter recovery file
load('trust_param_recov_output.mat') %param_recov_struct
%Per model per parameter
%x: n_subs X 100 clones
%y: n_subs X 100 psudo samples
%Grab model names
model_names = fieldnames(param_recov_struct);
%Do we want to save the plotting data for R?
save_for_plotting = 1;
%Initialize final output struct
recov_output = struct();
%Point to where the single subject data is
original_data_path = 'E:/Box Sync/skinner/projects_analyses/Project Trust/data/model-derived/scan/';
ct = 1;
for model = model_names' %Model loop
%For saving later
T = table();
%Compile the original data
parameter_table=compile_vba_parameters([original_data_path model{:}]);
%Create the x axis variable
%Loop through all vaiables in paramter table?
original_params = struct();
params = fieldnames(parameter_table);
params(strcmp(params(:,1), 'ID'),:) = []; %Remove ID & Properties
params(strcmp(params(:,1), 'Properties'),:) = [];
for param = params'
original_params.(param{:}) = repmat(parameter_table.(param{:}),1,100);
end
%Create the y axis variable
simulated_params = struct();
simulated_params=return_compiled_simulated_params(param_recov_struct,model,'theta',simulated_params);
simulated_params=return_compiled_simulated_params(param_recov_struct,model,'phi',simulated_params);
%We have to rename the phi parameters from 3-6 because they are not
%fixed for both the simulated and originial parameter sets.
if ~strcmpi(model{:},'f_trust_svm1')
simulated_params = restructure_kappa_parameters(simulated_params, [original_data_path model{:}], model{:});
original_params = restructure_kappa_parameters(original_params, [original_data_path model{:}], model{:});
else
simulated_params=rename_parameters(simulated_params, model{:});
original_params=rename_parameters(original_params, model{:});
end
%Loop thorugh all params to make correlations and plots
%Could have a lookup table to rename these guys
vba_params = fieldnames(original_params);
%TODO Check if original and simulated params do not have the same fieldnames
%and kick out
%Run the correlations and svae the output per parameter per model
for vba_param = vba_params'
if iscell(simulated_params.(vba_param{:}))
simulated_params.(vba_param{:})=cell2mat(simulated_params.(vba_param{:}));
end
[rows,cols] = size(original_params.(vba_param{:}));
n_cols = rows*cols;
[recov_output.(model{:}).(vba_param{:}).r,recov_output.(model{:}).(vba_param{:}).p] = ...
corrcoef(reshape(original_params.(vba_param{:}),n_cols,1),reshape(simulated_params.(vba_param{:}),n_cols,1),'rows','complete');
figure(ct)
scatter(reshape(original_params.(vba_param{:}),n_cols,1),reshape(simulated_params.(vba_param{:}),n_cols,1))
title(sprintf('MODEL %s : PARAMETER: %s',model{:},vba_param{:}))
ct = ct+1;
if save_for_plotting
R_dir = 'for_R_plotting';
mkdir(R_dir)
T.(['original_' vba_param{:}]) = reshape(original_params.(vba_param{:}),n_cols,1);
T.(['simulated_' vba_param{:}]) = reshape(simulated_params.(vba_param{:}),n_cols,1);
end
end
if ~isempty(T) && save_for_plotting
writetable(T, [R_dir filesep [model{:} 'parameters_for_plotting.csv']])
end
end
function simulated_params=return_compiled_simulated_params(param_recov_struct,model,param_of_interest,simulated_params)
switch param_of_interest
case 'theta'
p_str = 'theta_param_';
n_vars = length(param_recov_struct.(model{:}).muTheta{1,1});
mu_str = {'muTheta'};
case 'phi'
p_str = 'phi_param_';
n_vars = length(param_recov_struct.(model{:}).muPhi{1,1});
mu_str = {'muPhi'};
otherwise
error('Options are with theta or phi')
end
for i = 1:n_vars
tmp_str = {[p_str num2str(i)]};
[rows,cols] = size(param_recov_struct.(model{:}).(mu_str{:}));
n_cols = rows*cols;
tmp=reshape(param_recov_struct.(model{:}).(mu_str{:}).',1,n_cols);
param_lenth = cellfun(@length, tmp);
max_params = max(cellfun(@length, tmp)); %This assumes that the higest number of parameters is correct!
%Find where there could be missing parameters for subjects and fill them with nans
missing_param_idx=find(param_lenth<max_params);
%Add in nans is need be
if ~isempty(missing_param_idx)
for idx = missing_param_idx
filler = max_params-length(tmp{idx});
tmp{idx} = [tmp{idx}; nan(filler,1)];
end
end
simulated_params.(tmp_str{:}) = permute(reshape(cellfun(@(v)v(i),tmp),cols,rows),[2,1]);
end
function s = restructure_kappa_parameters(s,path_to_subject_data,model_name)
%Becasue the trustee kappa parameter is not fixed in the observation
%function during multisession we have to rename/ restructe our phi
%matrices
%Grab all the subject files
subj_files = glob([path_to_subject_data filesep '*.mat']);
for i = 1:length(subj_files)
load(subj_files{i},'b') %only load in b
%Hardcoded, but this seems to be the only consistant variable
trustee_order = {b.identity{1} b.identity{50} b.identity{97} b.identity{173}};
%Reconstruction
trustees = {'good';'bad';'neutral';'computer'};
for j = 1:length(trustees)
trustee_idx = find(strcmp(trustee_order, trustees{j}))+2;
if ~isempty(trustee_idx)
s.(['kappa_' trustees{j}])(i,:) = s.(['phi_param_' num2str(trustee_idx)])(i,:);
else
%If the data is missing replace the row with nans
s.(['kappa_' trustees{j}])(i,:) = nan(1,length(s.phi_param_1));
end
end
end
s=rename_parameters(s,model_name);
function s = rename_parameters(s,model_name)
if ~strcmpi(model_name,'f_trust_svm1')
%Renaming vars
s.('learning_rate') = s.theta_param_1;
s.('beta') = s.phi_param_1;
s.('kappa_subject') = s.phi_param_2;
%Remove old fields
fields_to_remove = matlab.lang.makeUniqueStrings(repmat({'phi_param'},1,7));
fields_to_remove{1}='theta_param_1';
s=rmfield(s,fields_to_remove);
else
%Renaming vars
s.('learning_rate') = s.theta_param_1;
s.('social_value') = s.theta_param_1;
s.('beta') = s.phi_param_1;
%Remove
s=rmfield(s,{'theta_param_1','theta_param_2','phi_param_1'});
end
%%%OLD code del later
% % % n_phi = length(param_recov_struct.(model{:}).muPhi{1,1});
% % % for i = 1:n_phi
% % % tmp_str = {['phi_param_' num2str(i)]};
% % % [rows,cols] = size(param_recov_struct.(model{:}).muPhi);
% % % n_cols = rows*cols;
% % % tmp=reshape(param_recov_struct.(model{:}).muPhi.',1,n_cols);
% % %
% % % param_lenth = cellfun(@length, tmp);
% % % max_params = max(cellfun(@length, tmp)); %This assumes that the higest number of parameters is correct!
% % %
% % % %Find where there could be missing parameters for subjects and fill them with nans
% % % missing_param_idx=find(param_lenth<max_params);
% % %
% % % %Add in nans is need be
% % % if ~isempty(missing_param_idx)
% % % for idx = missing_param_idx
% % % filler = max_params-length(tmp{idx});
% % % tmp{idx} = [tmp{idx}; nan(filler,1)];
% % % end
% % % end
% % % simulated_params.(tmp_str{:}) = permute(reshape(cellfun(@(v)v(i),tmp),cols,rows),[2,1]);
% % % end
% % %
% % %
% % %
% % % for i = 1:n_theta
% % % % % tmp_str = {['theta_param_' num2str(i)]};
% % % % % simulated_params.(tmp_str{:}) = [param_recov_struct.(model{:}).muTheta];
% % %
% % % tmp_str = {['theta_param_' num2str(i)]};
% % % [rows,cols] = size(param_recov_struct.(model{:}).muTheta);
% % % n_cols = rows*cols;
% % % tmp=reshape(param_recov_struct.(model{:}).muTheta.',1,n_cols);
% % %
% % % param_lenth = cellfun(@length, tmp);
% % % max_params = max(cellfun(@length, tmp)); %This assumes that the higest number of parameters is correct!
% % %
% % % %Find where there could be missing parameters for subjects and fill them with nans
% % % missing_param_idx=find(param_lenth<max_params);
% % %
% % % %Add in nans is need be
% % % if ~isempty(missing_param_idx)
% % % for idx = missing_param_idx
% % % filler = max_params-length(tmp{idx});
% % % tmp{idx} = [tmp{idx}; nan(filler,1)];
% % % end
% % % end
% % % simulated_params.(tmp_str{:}) = permute(reshape(cellfun(@(v)v(i),tmp),cols,rows),[2,1]);
% % % end