From e1d17f5954ec876c809549e4929debcf33cc4639 Mon Sep 17 00:00:00 2001 From: acp29 Date: Tue, 2 Apr 2024 14:33:00 +0100 Subject: [PATCH] added BOOTDATA as third output argument to `bootclust` --- inst/bootclust.m | 38 ++++++++++++++++++++------------------ inst/bootknife.m | 4 ++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/inst/bootclust.m b/inst/bootclust.m index be973007..f1054d36 100755 --- a/inst/bootclust.m +++ b/inst/bootclust.m @@ -92,10 +92,11 @@ % so that bootclust results are reproducible. % % 'bootclust (DATA, NBOOT, BOOTFUN, ALPHA, ..., LOO, SEED, NPROC)' also -% sets the number of parallel processes to use for function evaluations -% during bootstrap and jackknife computations on multicore machines. This -% feature requires the Parallel package (in Octave), or the Parallel -% Computing Toolbox (in Matlab). +% sets the number of parallel processes to use for jackknife computations +% and non-vectorized function evaluations during bootstrap and on multicore +% machines. This feature requires the Parallel package (in Octave), or the +% Parallel Computing Toolbox (in Matlab). This option is ignored during +% bootstrap function evaluations when BOOTFUN is vectorized. % % 'STATS = bootclust (...)' returns a structure with the following fields % (defined above): original, bias, std_error, CI_lower, CI_upper. @@ -103,6 +104,10 @@ % '[STATS, BOOTSTAT] = bootclust (...)' returns BOOTSTAT, a vector or matrix % of bootstrap statistics calculated over the bootstrap resamples. % +% '[STATS, BOOTSTAT, BOOTDATA] = bootclust (...)' returns BOOTDATA, a 1-by- +% NBOOT cell array of datasets generated by cluster or block bootstrap +% resampling. +% % REQUIREMENTS: % The function file boot.m (or better boot.mex) and bootcdf, which are % distributed with the statistics-resampling package. @@ -140,8 +145,8 @@ % You should have received a copy of the GNU General Public License % along with this program. If not, see http://www.gnu.org/licenses/ -function [stats, bootstat] = bootclust (x, nboot, bootfun, alpha, clustid, ... - loo, seed, ncpus) +function [stats, bootstat, X] = bootclust (x, nboot, bootfun, alpha, ... + clustid, loo, seed, ncpus) % Store local functions in a stucture for parallel processes localfunc = struct ('col2args', @col2args, ... @@ -159,7 +164,7 @@ if (nargin > 8) error ('bootclust: Too many input arguments') end - if (nargout > 2) + if (nargout > 3) error ('bootclust: Too many output arguments') end @@ -407,7 +412,7 @@ M = cell2mat (cellfun (@(i) repmat (x(:, i), 1, 2), ... num2cell (1 : nvar), 'UniformOutput', false)); else - M = repmat (x, 1, 2); + M = repmat (x, 1, 2); end if (any (szx > 1)) VECTORIZED = false; @@ -442,30 +447,27 @@ % Perform resampling of clusters bootsam = boot (nx, nboot, loo); - X = arrayfun (@(i) x(bootsam(i, :)), 1 : nx, 'UniformOutput', false); - X = [X{:}]'; + X = arrayfun (@(b) cell2mat (x(bootsam(:, b))), 1 : nboot, ... + 'UniformOutput', false); % Perform the function evaluations - if VECTORIZED - X = reshape (vertcat (X{:}), n, nboot * nvar); - bootstat = bootfun (X); + if ( (VECTORIZED) && (nvar == 1) ) + bootstat = bootfun (cell2mat (X)); else if (ncpus > 1) % Evaluate bootfun on each bootstrap resample in PARALLEL - X = arrayfun (@(i) vertcat (X(:,i)), 1 : nboot, 'UniformOutput', false); if (ISOCTAVE) % OCTAVE - bootstat = parcellfun (ncpus, @(X) bootfun (cell2mat (X)), X, ... + bootstat = parcellfun (ncpus, @(x) bootfun (x), X, ... 'UniformOutput', false); - else % MATLAB bootstat = cell (1, nboot); - parfor b = 1 : nboot; bootstat{b} = bootfun (cell2mat (X{:, b})); end + parfor b = 1 : nboot; bootstat{b} = bootfun (X{:, b}); end end else % Evaluate bootfun on each bootstrap resample in SERIAL - bootstat = cell2mat (arrayfun (@(b) bootfun (vertcat (X{:,b})), ... + bootstat = cell2mat (arrayfun (@(b) bootfun (X{:, b}), ... 1 : nboot, 'UniformOutput', false)); end end diff --git a/inst/bootknife.m b/inst/bootknife.m index ad1fc593..ef1f9d43 100755 --- a/inst/bootknife.m +++ b/inst/bootknife.m @@ -939,8 +939,8 @@ function print_output (stats, nboot, alpha, l, m, bootfun_str, strata) function retval = col2args (func, x, szx) - % Usage: retval = col2args (func, x, nvar) - % col2args evaluates func on the columns of x. When nvar > 1, each of the + % Usage: retval = col2args (func, x, szx) + % col2args evaluates func on the columns of x. When sum(szx) > 1, each of the % blocks of x are passed to func as a separate arguments. % Extract columns of the matrix into a cell array