Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
acp29 committed Apr 21, 2024
1 parent f0f90ab commit be81122
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
3 changes: 2 additions & 1 deletion inst/randtest.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

function [pval, stat, fpr, STATS] = randtest (x, y, nreps, func, seed)


% Check if we are running Octave or Matlab
info = ver;
ISOCTAVE = any (ismember ({info.Name}, 'Octave'));
Expand All @@ -104,7 +105,7 @@

% Check the number of function arguments
if (nargin < 2)
error ('randtest: X and Y must be provided');
error ('randtest: X and Y must be provided')
end
if (nargin > 6)
error ('randtest: Too many input arguments')
Expand Down
42 changes: 20 additions & 22 deletions inst/randtest1.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,43 @@
% to ascertain whether data sample in the column vector A comes from a
% population with mean equal to the value M. The value returned is a 2-
% tailed p-value against the null hypothesis computed using the absolute
% values of the test statistics. This function works by subtracting the
% value M from A
% values of the mean. This function generates resamples by independently
% and randomly flipping the signs of values in (A - M).
%
% 'PVAL = randtest1 (A, M, NREPS)' specifies the number of resamples to
% take in the randomization test. By default, NREPS is 5000. If the number
% of possible permutations is smaller than NREPS, the test becomes exact.
% For example, if the number of sampling units in the sample is 12, then
% the number of possible permutations is 2^12 = 4096, so NREPS will be
% truncated at 4096 and sampling will systematically evaluate all possible
% permutations.
% For example, if the number of sampling units (i.e. rows) in the sample
% is 12, then the number of possible permutations is 2^12 = 4096, so NREPS
% will be truncated at 4096 and sampling will systematically evaluate all
% possible permutations.
%
% 'PVAL = randtest1 (A, M, NREPS, FUNC)' also specifies a custom
% function calculated on the original samples, and the permuted or
% randomized resamples. Note that FUNC must compute a location parameter
% and should either be a:
% 'PVAL = randtest1 (A, M, NREPS, FUNC)' specifies a custom function
% calculated on the original samples, and the permuted or randomized
% resamples. Note that FUNC must compute a location parameter and
% should either be a:
% o function handle or anonymous function,
% o string of function name, or
% o a cell array where the first cell is one of the above function
% definitions and the remaining cells are (additional) input arguments
% to that function (other than the data arguments).
% See the built-in demos for example usage using the mean.
%
% 'PVAL = randtest1 (A, M, NREPS, FUNC, SEED)' initialises the
% Mersenne Twister random number generator using an integer SEED value so
% that the results of 'randtest1' results are reproducible when the test
% is approximate (i.e. when using randomization if not all permutations
% can be evaluated systematically).
% 'PVAL = randtest1 (A, M, NREPS, FUNC, SEED)' initialises the Mersenne
% Twister random number generator using an integer SEED value so that
% the results of 'randtest1' are reproducible when the test is approximate
% (i.e. when using randomization if not all permutations can be
% evaluated systematically).
%
% 'PVAL = randtest1 ([A, GA], M, ...)' also specifies the sampling
% units (i.e. clusters) using consecutive positive integers in GA for A.
% Defining the sampling units has applications for clustered resampling,
% for example in the cases of nested experimental designs. Note that when
% sampling units contain different numbers of values, function evaluations
% after sampling cannot be vectorized. If the parallel computing toolbox
% (Matlab) or package (Octave) is installed and loaded, then the function
% evaluations will be automatically accelerated by parallel processing
% on platforms with multiple processors.
% (Matlab) or parallel package (Octave) is installed and loaded, then the
% function evaluations will be automatically accelerated by parallel
% processing on platforms with multiple processors.
%
% '[PVAL, STAT] = randtest1 (...)' also returns the test statistic.
%
Expand Down Expand Up @@ -89,7 +89,7 @@

% Check the number of function arguments
if (nargin < 2)
error ('randtest1: A and m must be provided');
error ('randtest1: A and m must be provided')
end
if (nargin > 5)
error ('randtest1: Too many input arguments')
Expand Down Expand Up @@ -203,6 +203,4 @@
%! pval3 = randtest1 (A, M, 5000);
%! pval4 = randtest1 (A, M, [], [], 1);
%! pval5 = randtest1 (A, M, [], 'mean', 1);
%! pval6 = randtest1 (A, M, [], {@mean,'omitnan'}, 1);
%! pval7 = randtest1 (A, M, [], {'mean','omitnan'}, 1);
%! pval8 = randtest1 (A, M, [], @smoothmedian, 1);
%! pval6 = randtest1 (A, M, [], @smoothmedian, 1);
22 changes: 4 additions & 18 deletions inst/randtest2.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
% across A and B corresponds to a pair of matched observations.
% Within each pair, the allocation of data to samples A or B is
% permuted or randomized.
% See the built-in demo 2 for example usage on how to use 'randtest2'
% with paired == true to perform a one-sample randomization/permutation
% test.
%
% 'PVAL = randtest2 (A, B, PAIRED, NREPS)' specifies the number of resamples
% without replacement to take in the randomization test. By default, NREPS
Expand Down Expand Up @@ -77,9 +74,9 @@
% and B only where the clusters have the same identifier. Note that when
% sampling units contain different numbers of values, function evaluations
% after sampling cannot be vectorized. If the parallel computing toolbox
% (Matlab) or package (Octave) is installed and loaded, then the function
% evaluations will be automatically accelerated by parallel processing
% on platforms with multiple processors.
% (Matlab) or Parallel package (Octave) is installed and loaded, then the
% function evaluations will be automatically accelerated by parallel
% processing on platforms with multiple processors.
%
% '[PVAL, STAT] = randtest2 (...)' also returns the test statistic.
%
Expand Down Expand Up @@ -147,7 +144,7 @@

% Check the number of function arguments
if (nargin < 2)
error ('randtest2: A and B must be provided');
error ('randtest2: A and B must be provided')
end
if (nargin > 6)
error ('randtest2: Too many input arguments')
Expand Down Expand Up @@ -526,17 +523,6 @@
%! % paired or matching samples (assuming i.i.d and exchangeability). (Note
%! % that the log transformation is necessary to make the p-value two-tailed)
%! pval = randtest2 (A, B, true, 5000, @(A, B) log (var (A) ./ var (B)))
%!
%! % One-sample randomization test comparing the difference in the mean between
%! % a sample (in this case B) and a population value (in this case 120, which
%! % we have called m). As we can see from this example, a one-sample
%! % randomization/permutation test is a special case of a paired two-sample
%! % test designed to flip the sign of the sample values. Note that the function
%! % should be in mean (as demonstrated below), not the default Wasserstein
%! % metric function.
%! m = 120; % population value of the mean
%! C = B - m; % subtract population value from the sample
%! pval = randtest2 (C, -C, true, 5000, @(A, B) mean (A))

%!demo
%!
Expand Down
6 changes: 2 additions & 4 deletions test/test_script.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
bootci (1999, {@mean, y}, 'alpha', 0.1, 'type', 'stud', 'nbootstd', 100);
bootci (1999, {@mean, y}, 'alpha', 0.1, 'type', 'cal');
bootci (1999, {@mean, y}, 'alpha', 0.1, 'type', 'cal', 'nbootcal', 199);
g = reshape (repmat ([1:5], 4, 1), 20, []);
g = reshape (repmat ((1:5), 4, 1), 20, []);
bootci (1999, {@mean, y}, 'alpha', 0.1, 'strata', []);
bootci (1999, {@mean, y}, 'alpha', 0.1, 'type', 'norm', 'strata', g);
bootci (1999, {@mean, y}, 'alpha', 0.1, 'type', 'per', 'strata', g);
Expand Down Expand Up @@ -718,9 +718,7 @@
pval3 = randtest1 (A, M, 5000);
pval4 = randtest1 (A, M, [], [], 1);
pval5 = randtest1 (A, M, [], 'mean', 1);
pval6 = randtest1 (A, M, [], {@mean,'omitnan'}, 1);
pval7 = randtest1 (A, M, [], {'mean','omitnan'}, 1);
pval8 = randtest1 (A, M, [], @smoothmedian, 1);
pval6 = randtest1 (A, M, [], @smoothmedian, 1);

% randtest2:test:1
X = randn (3,1);
Expand Down

0 comments on commit be81122

Please sign in to comment.