|
377 | 377 | % calculated exclude variability attributed to other predictors in
|
378 | 378 | % the model. To avoid small sample bias inflating effect sizes for
|
379 | 379 | % posthoc comparisons when use the 'bayesian' method, use the 'auto'
|
380 |
| -% setting for the prior. |
| 380 | +% setting for the prior. |
381 | 381 | %
|
382 | 382 | % '[...] = bootlm (Y, GROUP, ..., 'seed', SEED)' initialises the Mersenne
|
383 | 383 | % Twister random number generator using an integer SEED value so that
|
|
393 | 393 | % - 'CI_upper': The upper bound(s) of the confidence/credible interval(s)
|
394 | 394 | % - 'pval': The p-value(s) for the hypothesis that the estimate(s) == 0
|
395 | 395 | % - 'fpr': The minimum false positive risk (FPR) for each p-value
|
396 |
| -% - 'N': The number of independnet sampling units used to compute CIs |
397 |
| -% - 'prior': The prior used for Bayesian bootstrap |
| 396 | +% - 'N': The number of independent sampling units used to compute CIs |
| 397 | +% - 'prior': The prior used for Bayesian bootstrap. This will return a |
| 398 | +% scalar for regression coeefficients, or a P x 1 or P x 2 |
| 399 | +% matrix for estimated marginal means or posthoc tests |
| 400 | +% respectively, where P is the number of parameter estimates. |
398 | 401 | % - 'levels': A cell array with the levels of each predictor.
|
399 | 402 | % - 'contrasts': A cell array of contrasts associated with each predictor.
|
400 | 403 | %
|
|
680 | 683 | GROUP(excl,:) = [];
|
681 | 684 | end
|
682 | 685 | Y(excl) = [];
|
683 |
| - if ((~ isempty(DEP)) && (~ isscalar(DEP))) |
| 686 | + if ((~ isempty (DEP)) && (~ isscalar (DEP))) |
684 | 687 | DEP(excl) = [];
|
685 | 688 | end
|
686 | 689 | n = numel (Y); % Recalculate total number of observations
|
|
1111 | 1114 | switch (lower (PRIOR))
|
1112 | 1115 | case 'auto'
|
1113 | 1116 | PRIOR = 1 - 2 ./ N_dim;
|
1114 |
| - % Use parallel processing if it is available to accelerate |
1115 |
| - % bootstrap computation for each column of the hypothesis |
1116 |
| - % matrix. |
1117 |
| - if (PARALLEL) |
1118 |
| - if (ISOCTAVE) |
1119 |
| - [STATS, BOOTSTAT] = pararrayfun (inf, @(i) bootbayes ( ... |
1120 |
| - Y, X, DEP, NBOOT, fliplr (1 - ALPHA), ... |
1121 |
| - PRIOR(i), SEED, L(:, i), ISOCTAVE), (1:Np)', ... |
1122 |
| - 'UniformOutput', false); |
1123 |
| - else |
1124 |
| - STATS = cell (Np, 1); BOOTSTAT = cell (Np, 1); |
1125 |
| - parfor i = 1:Np |
1126 |
| - [STATS{i}, BOOTSTAT{i}] = bootbayes (Y, X, DEP, ... |
1127 |
| - NBOOT, fliplr (1 - ALPHA), ... |
1128 |
| - PRIOR(i), SEED, L(:, i), ISOCTAVE); |
| 1117 | + if (all (PRIOR == PRIOR(1))) |
| 1118 | + [STATS, BOOTSTAT] = bootbayes (Y, X, DEP, NBOOT, ... |
| 1119 | + fliplr (1 - ALPHA), PRIOR(1), SEED, L, ISOCTAVE); |
| 1120 | + STATS.prior = PRIOR; |
| 1121 | + else |
| 1122 | + % If the prior is not the same across all the samples then |
| 1123 | + % we need to perform a separate simulation for each sample. |
| 1124 | + % Use parallel processing if it is available to accelerate |
| 1125 | + % bootstrap computation for each column of the hypothesis |
| 1126 | + % matrix. |
| 1127 | + if (PARALLEL) |
| 1128 | + if (ISOCTAVE) |
| 1129 | + [STATS, BOOTSTAT] = pararrayfun (inf, @(i) bootbayes ( ... |
| 1130 | + Y, X, DEP, NBOOT, fliplr (1 - ALPHA), ... |
| 1131 | + PRIOR(i), SEED, L(:, i), ISOCTAVE), (1:Np)', ... |
| 1132 | + 'UniformOutput', false); |
| 1133 | + else |
| 1134 | + STATS = cell (Np, 1); BOOTSTAT = cell (Np, 1); |
| 1135 | + parfor i = 1:Np |
| 1136 | + [STATS{i}, BOOTSTAT{i}] = bootbayes (Y, X, DEP, ... |
| 1137 | + NBOOT, fliplr (1 - ALPHA), ... |
| 1138 | + PRIOR(i), SEED, L(:, i), ISOCTAVE); |
| 1139 | + end |
1129 | 1140 | end
|
| 1141 | + else |
| 1142 | + [STATS, BOOTSTAT] = arrayfun (@(i) bootbayes (Y, X, ... |
| 1143 | + DEP, NBOOT, fliplr (1 - ALPHA), PRIOR(i), SEED, ... |
| 1144 | + L(:, i), ISOCTAVE), (1:Np)', 'UniformOutput', false); |
1130 | 1145 | end
|
1131 |
| - else |
1132 |
| - [STATS, BOOTSTAT] = arrayfun (@(i) bootbayes (Y, X, ... |
1133 |
| - DEP, NBOOT, fliplr (1 - ALPHA), PRIOR(i), SEED, ... |
1134 |
| - L(:, i), ISOCTAVE), (1:Np)', 'UniformOutput', false); |
| 1146 | + STATS = flatten_struct (cell2mat (STATS)); |
| 1147 | + BOOTSTAT = cell2mat (BOOTSTAT); |
1135 | 1148 | end
|
1136 |
| - STATS = flatten_struct (cell2mat (STATS)); |
1137 |
| - BOOTSTAT = cell2mat (BOOTSTAT); |
1138 | 1149 | otherwise
|
1139 | 1150 | [STATS, BOOTSTAT] = bootbayes (Y, X, DEP, NBOOT, ...
|
1140 | 1151 | fliplr (1 - ALPHA), PRIOR, SEED, ...
|
1141 | 1152 | L, ISOCTAVE);
|
| 1153 | + STATS.prior = PRIOR * ones (Np, 1); |
1142 | 1154 | end
|
1143 | 1155 | % Create empty fields in STATS structure
|
1144 | 1156 | STATS.pval = [];
|
|
1166 | 1178 | ' control group for ''trt_vs_ctrl'''))
|
1167 | 1179 | end
|
1168 | 1180 | pairs = feval (POSTHOC{1}, L, POSTHOC{2:end});
|
1169 |
| - L = make_test_matrix (L, pairs); |
1170 | 1181 | POSTHOC = POSTHOC{1};
|
1171 | 1182 | case 'char'
|
1172 | 1183 | if (strcmp (POSTHOC, 'trt.vs.ctrl'))
|
|
1176 | 1187 | ' ''pairwise'' and ''trt_vs_ctrl'''))
|
1177 | 1188 | end
|
1178 | 1189 | pairs = feval (POSTHOC, L);
|
1179 |
| - L = make_test_matrix (L, pairs); |
1180 | 1190 | otherwise
|
1181 | 1191 | pairs = unique_stable (POSTHOC, 'rows');
|
1182 | 1192 | if (size (pairs, 2) > 2)
|
1183 | 1193 | error (cat (2, 'bootlm: pairs matrix defining posthoc', ...
|
1184 | 1194 | ' comparisons must have exactly two columns'))
|
1185 | 1195 | end
|
1186 |
| - L = make_test_matrix (L, pairs); |
1187 | 1196 | end
|
1188 |
| - Np = size (pairs, 1); % Update the number of parameters |
1189 | 1197 | switch (lower (METHOD))
|
1190 | 1198 | case 'wild'
|
| 1199 | + % Modifying the hypothesis matrix (L) to perform the desired tests |
| 1200 | + L = make_test_matrix (L, pairs); |
| 1201 | + Np = size (pairs, 1); % Update the number of parameters |
1191 | 1202 | [STATS, BOOTSTAT] = bootwild (Y, X, DEP, NBOOT, ALPHA, SEED, ...
|
1192 | 1203 | L, ISOCTAVE);
|
1193 | 1204 | % Control the type 1 error rate across multiple comparisons
|
|
1199 | 1210 | case {'bayes', 'bayesian'}
|
1200 | 1211 | switch (lower (PRIOR))
|
1201 | 1212 | case 'auto'
|
1202 |
| - wgt = bsxfun (@rdivide, N_dim(pairs')', ... |
1203 |
| - sum (N_dim(pairs')', 2)); |
1204 |
| - PRIOR = sum ((1 - wgt) .* (1 - 2 ./ N_dim(pairs')'), 2); |
1205 |
| - % Use parallel processing if it is available to accelerate |
1206 |
| - % bootstrap computation for each column of the hypothesis |
1207 |
| - % matrix. |
1208 |
| - if (PARALLEL) |
1209 |
| - if (ISOCTAVE) |
1210 |
| - [STATS, BOOTSTAT] = pararrayfun (inf, @(i) bootbayes ( ... |
1211 |
| - Y, X, DEP, NBOOT, fliplr (1 - ALPHA), ... |
1212 |
| - PRIOR(i), SEED, L(:, i), ISOCTAVE), (1:Np)', ... |
1213 |
| - 'UniformOutput', false); |
1214 |
| - else |
1215 |
| - STATS = cell (Np, 1); BOOTSTAT = cell (Np, 1); |
1216 |
| - parfor i = 1:Np |
1217 |
| - [STATS{i}, BOOTSTAT{i}] = bootbayes (Y, X, DEP, ... |
1218 |
| - NBOOT, fliplr (1 - ALPHA), ... |
1219 |
| - PRIOR(i), SEED, L(:, i), ISOCTAVE); |
| 1213 | + % To enable the 'auto' prior to be set independently on each |
| 1214 | + % sample, we use the hypothesis matrix (L) already generated |
| 1215 | + % to compute the posterior distributions for the estimated |
| 1216 | + % marginal means each with their own prior. Later, we will |
| 1217 | + % perform elementwise subtraction for each pair of posterior |
| 1218 | + % distributions to find the differences |
| 1219 | + PRIOR = 1 - 2 ./ N_dim; |
| 1220 | + if (all (PRIOR == PRIOR(1))) |
| 1221 | + [STATS, BOOTSTAT] = bootbayes (Y, X, DEP, NBOOT, ... |
| 1222 | + fliplr (1 - ALPHA), PRIOR(1), SEED, L, ISOCTAVE); |
| 1223 | + else |
| 1224 | + % If the prior is not the same across all the samples then |
| 1225 | + % we need to perform a separate simulation for each sample. |
| 1226 | + % Use parallel processing if it is available to accelerate |
| 1227 | + % bootstrap computation for each column of the hypothesis |
| 1228 | + % matrix. |
| 1229 | + if (PARALLEL) |
| 1230 | + if (ISOCTAVE) |
| 1231 | + [STATS, BOOTSTAT] = pararrayfun (inf, @(i) bootbayes ( ... |
| 1232 | + Y, X, DEP, NBOOT, fliplr (1 - ALPHA), ... |
| 1233 | + PRIOR(i), SEED, L(:, i), ISOCTAVE), (1:Np)', ... |
| 1234 | + 'UniformOutput', false); |
| 1235 | + else |
| 1236 | + STATS = cell (Np, 1); BOOTSTAT = cell (Np, 1); |
| 1237 | + parfor i = 1:Np |
| 1238 | + [STATS{i}, BOOTSTAT{i}] = bootbayes (Y, X, DEP, ... |
| 1239 | + NBOOT, fliplr (1 - ALPHA), ... |
| 1240 | + PRIOR(i), SEED, L(:, i), ISOCTAVE); |
| 1241 | + end |
1220 | 1242 | end
|
| 1243 | + else |
| 1244 | + [STATS, BOOTSTAT] = arrayfun (@(i) bootbayes (Y, X, ... |
| 1245 | + DEP, NBOOT, fliplr (1 - ALPHA), PRIOR(i), SEED, ... |
| 1246 | + L(:, i), ISOCTAVE), (1:Np)', 'UniformOutput', false); |
1221 | 1247 | end
|
1222 |
| - else |
1223 |
| - [STATS, BOOTSTAT] = arrayfun (@(i) bootbayes (Y, X, ... |
1224 |
| - DEP, NBOOT, fliplr (1 - ALPHA), PRIOR(i), SEED, ... |
1225 |
| - L(:, i), ISOCTAVE), (1:Np)', 'UniformOutput', false); |
| 1248 | + STATS = flatten_struct (cell2mat (STATS)); |
| 1249 | + BOOTSTAT = cell2mat (BOOTSTAT); |
1226 | 1250 | end
|
1227 |
| - STATS = flatten_struct (cell2mat (STATS)); |
1228 |
| - BOOTSTAT = cell2mat (BOOTSTAT); |
1229 | 1251 | otherwise
|
| 1252 | + % Compute the posterior distributions for the estimated |
| 1253 | + % marginal means each with the PRIOR. Later, we will perform |
| 1254 | + % elementwise subtraction for each pair of posterior |
| 1255 | + % distributions to find the differences |
1230 | 1256 | [STATS, BOOTSTAT] = bootbayes (Y, X, DEP, NBOOT, ...
|
1231 |
| - fliplr (1 - ALPHA), PRIOR, SEED, L, ISOCTAVE); |
| 1257 | + fliplr (1 - ALPHA), PRIOR, SEED, ... |
| 1258 | + L, ISOCTAVE); |
1232 | 1259 | end
|
1233 | 1260 | % Create empty fields in STATS structure
|
1234 | 1261 | STATS.pval = [];
|
1235 | 1262 | STATS.fpr = [];
|
| 1263 | + % Calculate estimates and credible intervals from the estimated |
| 1264 | + % marginal means and their associated posterior distributions |
| 1265 | + Np = size (pairs, 1); % Update the number of parameters |
| 1266 | + STATS.original = bsxfun (@minus, STATS.original(pairs(:, 1), :), ... |
| 1267 | + STATS.original(pairs(:, 2), :)); |
| 1268 | + BOOTSTAT = bsxfun (@minus, BOOTSTAT(pairs(:, 1),:), ... |
| 1269 | + BOOTSTAT(pairs(:, 2),:)); |
| 1270 | + CI = credint (BOOTSTAT, fliplr (1 - ALPHA)); |
| 1271 | + STATS.CI_lower = CI(:,1); STATS.CI_upper = CI(:,2); |
| 1272 | + if (isscalar (PRIOR)) |
| 1273 | + STATS.prior = PRIOR * ones (Np, 2); |
| 1274 | + else |
| 1275 | + STATS.prior = cat (2, PRIOR(pairs(:, 1)), PRIOR(pairs(:, 2))); |
| 1276 | + end |
1236 | 1277 | otherwise
|
1237 | 1278 | error (cat (2, 'bootlm: unrecignised bootstrap method.', ...
|
1238 | 1279 | ' Use ''wild'' or ''bayesian''.'))
|
|
1347 | 1388 | % If applicable, print parameter estimates (a.k.a contrasts) for fixed
|
1348 | 1389 | % effects. Parameter estimates correspond to the contrasts we set.
|
1349 | 1390 | if (isempty (DIM))
|
| 1391 | + switch (lower (METHOD)) |
| 1392 | + case 'wild' |
| 1393 | + p_str = 'p-val'; |
| 1394 | + case {'bayes', 'bayesian'} |
| 1395 | + p_str = ' '; |
| 1396 | + end |
1350 | 1397 | fprintf ('\nMODEL COEFFICIENTS\n\n');
|
| 1398 | + |
1351 | 1399 | fprintf (cat (2, 'name coeff', ...
|
1352 |
| - ' CI_lower CI_upper p-val\n')); |
| 1400 | + ' CI_lower CI_upper %s\n'), p_str); |
1353 | 1401 | fprintf (cat (2, '--------------------------------------------', ...
|
1354 | 1402 | '------------------------------------\n'));
|
1355 | 1403 | else
|
|
1361 | 1409 | fprintf (cat (2, '---------------------------------------', ...
|
1362 | 1410 | '-----------------------------------------\n'));
|
1363 | 1411 | otherwise
|
| 1412 | + switch (lower (METHOD)) |
| 1413 | + case 'wild' |
| 1414 | + p_str = 'p-adj'; |
| 1415 | + case {'bayes', 'bayesian'} |
| 1416 | + p_str = ' '; |
| 1417 | + end |
1364 | 1418 | fprintf ('\nMODEL POSTHOC COMPARISONS\n\n');
|
1365 | 1419 | fprintf (cat (2, 'name ', ...
|
1366 |
| - 'mean CI_lower CI_upper p-adj\n')); |
| 1420 | + 'mean CI_lower CI_upper %s\n'), p_str); |
1367 | 1421 | fprintf (cat (2, '---------------------------------------', ...
|
1368 | 1422 | '-----------------------------------------\n'));
|
1369 | 1423 | end
|
|
0 commit comments