Skip to content

Commit

Permalink
further improved matlab compatibility of bootstrp
Browse files Browse the repository at this point in the history
  • Loading branch information
acp29 committed Apr 27, 2024
1 parent 8f1011c commit a58b2d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
45 changes: 26 additions & 19 deletions inst/bootstrp.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
% (column vector, matrix or cell array), which is supplied to BOOTFUN. This
% function is the only function in the statistics-resampling package to also
% accept cell arrays for the data arguments. The simulation method used by
% default is bootstrap resampling with first order balance [2-3].
% default is bootstrap resampling with first order balance [2-3]; see help
% for the 'boot' function for more information.
%
% 'BOOTSTAT = bootstrp (NBOOT, BOOTFUN, D1,...,DN)' is as above except
% that the third and subsequent input arguments are multiple data objects,
Expand Down Expand Up @@ -77,7 +78,8 @@
% provided, BOOTSAM is a matrix. If multiple data arguments are provided
% and MATCH is false, BOOTSAM is returned in a 1-by-N cell array of
% matrices, where each cell corresponds to the respective data argument
% D1 to DN.
% D1 to DN. To get the output samples BOOTSAM without applying a function,
% set BOOTFUN to empty (i.e. []).
%
% Bibliography:
% [1] Efron, and Tibshirani (1993) An Introduction to the
Expand Down Expand Up @@ -320,24 +322,26 @@
error ('bootstrp: BOOTFUN must be a function name or function handle')
end
end
try
t0 = bootfun (x{:});
catch
error (cat (2, 'bootstrp: There was an error evaluating ''bootfun''', ...
' and the data provided'))
end
% Check whether evaluation of bootfun on the data is vectorized
vectorized = false;
if (eq (size (t0, 2), 1))
if (all (bsxfun (@eq, 1, cellfun (@(x) size (x, 2), x))))
xt = arrayfun (@(v) repmat (x{v}, 1, 2), 1 : nvar, 'UniformOutput', false);
try
chk = bootfun (xt{:});
if ( eq (size (chk), cat (2, size (t0, 1), 2)) )
vectorized = true;
if (~ isempty (bootfun))
try
t0 = bootfun (x{:});
catch
error (cat (2, 'bootstrp: There was an error evaluating ''bootfun''', ...
' and the data provided'))
end
% Check whether evaluation of bootfun on the data is vectorized
vectorized = false;
if (eq (size (t0, 2), 1))
if (all (bsxfun (@eq, 1, cellfun (@(x) size (x, 2), x))))
xt = arrayfun (@(v) repmat (x{v}, 1, 2), 1 : nvar, 'UniformOutput', false);
try
chk = bootfun (xt{:});
if ( eq (size (chk), cat (2, size (t0, 1), 2)) )
vectorized = true;
end
catch
% Do nothing
end
catch
% Do nothing
end
end
end
Expand Down Expand Up @@ -583,6 +587,9 @@
%! Z = cat (1, X, Y);
%!
%! bootstrp (50, @mean, X);
%! bootstat = bootstrp (50, @mean, X);
%! [bootstat, bootsam] = bootstrp (50, @mean, X);
%! [bootstat, bootsam] = bootstrp (50, [], X);
%! bootstrp (50, @(x) mean (cell2mat (x)), num2cell (X, 2));
%! bootstrp (50, @(x, y) mean (x) - mean (y), X, Y);
%! bootstrp (50, @(x, y) mean (x - y), X, Y);
Expand Down
3 changes: 3 additions & 0 deletions test/test_script.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
474 329 555 282 423 323 256 431 437 240]';
Z = cat (1, X, Y);
bootstrp (50, @mean, X);
bootstat = bootstrp (50, @mean, X);
[bootstat, bootsam] = bootstrp (50, @mean, X);
[bootstat, bootsam] = bootstrp (50, [], X);
bootstrp (50, @(x) mean (cell2mat (x)), num2cell (X, 2));
bootstrp (50, @(x, y) mean (x) - mean (y), X, Y);
bootstrp (50, @(x, y) mean (x - y), X, Y);
Expand Down

0 comments on commit a58b2d0

Please sign in to comment.