-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmT_produceParamStats.m
172 lines (141 loc) · 5.61 KB
/
mT_produceParamStats.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
function paramMedian = mT_produceParamStats(DSet, dir, varargin)
% Produce statistics on the fitted paramters accross participants.
% INPUT
% dir: Where to save the results file (ready for latex). Set to 'none' if don't
% want to save
% varargin 1: Vector if want params for some models only, or leave empty.
% varargin 2: Text to use for the labeling the parameters. Structure with a
% field for every paramter want to relable. Field contains text.
% varargin 3: Text to add to the file name when saving
% varargin 4: numModels long cell array of model names to use instead of
% simply numebering the models. Needs to be as long as the number of models
% that have fit results in DSet, not just as long as the number of model
% for which paramter values have been requested.
% NOTE
% For use in latex, need to have the package siunitx
% HISTORY
% Reviewed 2020
models = mT_findAppliedModels(DSet);
if ~isempty(varargin) && ~isempty(varargin{1})
modelsToAnlyse = varargin{1};
else
modelsToAnlyse = 1 : length(models);
end
if (length(varargin)>=2) && ~isempty(varargin{2})
ParamLabels = varargin{2};
else
ParamLabels = struct();
end
if length(varargin)>=3 && ~isempty(varargin{3})
fileNameEnd = varargin{3};
else
fileNameEnd = '';
end
if length(varargin)>=4 && ~isempty(varargin{4})
modelNames = varargin{4};
if length(modelNames) ~= length(models)
error(['modelNames needs to be as long as the number of models ', ...
'that have fit results in DSet, not just as long as the ', ...
'number of model for which paramter values have been ', ...
'requested.'])
end
else
modelNames = [];
end
% Open a file to write results to, and add titles
if ~strcmp(dir, 'none')
saveFile = fopen([dir '/paramStats' fileNameEnd '.tex'], 'w' );
fprintf(saveFile, '%s\n', '\begin{table}[H]');
fprintf(saveFile, '%s\n', '\begin{center}');
fprintf(saveFile, '%s\n', '\renewcommand{\arraystretch}{1.29}');
fprintf(saveFile, '%s\n', '\begin{tabular}{l l l l l |}');
fprintf(saveFile, '%s\n', 'Model & Parameter & Median & 25th percentile & 75th percentile \\');
fprintf(saveFile, '%s\n', '\toprule');
end
for iModel = modelsToAnlyse
if ~isempty(modelNames)
thisModelName = modelNames{iModel};
else
thisModelName = num2str(iModel);
end
% Find the parameters in this model
params = fieldnames(DSet.P(1).Models(iModel).BestFit.Params);
% Find out how many subparams each parameter has
subParamTot = NaN(length(params), 1);
for iParam = 1 : length(subParamTot)
subParamTot(iParam) = length(...
DSet.P(1).Models(iModel).BestFit.Params.(params{iParam})(:));
end
paramNames = {};
paramMedian = [];
paramIqr = []; % Interquartile range
Pcent25 = [];
Pcent75 = [];
paramCount = 1;
for iParam = 1 : length(params)
if isfield(ParamLabels, params{iParam})
label = ParamLabels.(params{iParam});
else
label = params{iParam};
end
subParams = subParamTot(iParam);
for iSubParam = 1 : subParams
% Find data for the print out
if iParam == 1 && iSubParam == 1
if iModel == modelsToAnlyse(1)
midline = '';
else
midline = '\\midrule';
end
modelText = [midline ' \n \\multirow{%d}{*}{%s} &'];
modelArgs = {sum(subParamTot), thisModelName};
else
modelText = ' &';
modelArgs = {};
end
if iSubParam == 1
paramText = ' \\multirow{%d}{*}{%s} &';
paramArgs = {subParamTot(iParam), label};
else
paramText = ' &';
paramArgs = {};
end
paramVals = mT_stackData(DSet.P, ...
@(st) st.Models(iModel).BestFit.Params.(params{iParam} ...
)(iSubParam));
paramNames{paramCount, 1} = params{iParam};
paramMedian(paramCount, 1) = median(paramVals);
paramIqr(paramCount, 1) = iqr(paramVals);
Pcent25(paramCount, 1) = prctile(paramVals,25);
Pcent75(paramCount, 1) = prctile(paramVals,75);
% Print output
if ~strcmp(dir, 'none')
fprintf(saveFile, [modelText, paramText, ...
' \\num[round-precision=2,round-mode=figures]{%f}', ...
' & \\num[round-precision=2,round-mode=figures]{%f}', ...
' & \\num[round-precision=2,round-mode=figures]{%f}\\\\ \n'], ...
modelArgs{:}, paramArgs{:}, ...
paramMedian(paramCount), ...
Pcent25(paramCount), Pcent75(paramCount));
end
paramCount = paramCount +1;
if ~strcmp(dir, 'none')
if iSubParam == subParams && ~(iParam == length(params))
fprintf(saveFile, '%s \n', '\cline{2-5}');
end
end
end
end
% Display results
disp('**********************')
disp(['Model: ' num2str(iModel)])
resultTable = table(paramNames, paramMedian, paramIqr, Pcent25, Pcent75);
disp(resultTable)
end
if ~strcmp(dir, 'none')
fprintf(saveFile, '%s\n', '\bottomrule');
fprintf(saveFile, '%s\n', '\end{tabular}');
fprintf(saveFile, '%s\n', '\end{center}');
fprintf(saveFile, '%s\n', '\end{table}');
fclose(saveFile);
end