Mercurial > forge
changeset 12440:d715ab138bfc octave-forge
Support for name-value pairs added.
author | asnelt |
---|---|
date | Sun, 20 Apr 2014 13:49:11 +0000 |
parents | daa313c0884a |
children | 0b27c6d893c5 |
files | main/statistics/inst/ttest.m main/statistics/inst/ttest2.m main/statistics/inst/ztest.m |
diffstat | 3 files changed, 114 insertions(+), 173 deletions(-) [+] |
line wrap: on
line diff
--- a/main/statistics/inst/ttest.m Sun Apr 20 13:44:17 2014 +0000 +++ b/main/statistics/inst/ttest.m Sun Apr 20 13:49:11 2014 +0000 @@ -28,34 +28,32 @@ ## ttest (@var{x}, @var{y}) ## ## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest (..., @var{alpha}) +## ttest (@var{x}, @var{m}, @var{Name}, @var{Value}) ## ## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest (..., @var{alpha}, @var{tail}) -## -## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest (..., @var{alpha}, @var{tail}, @var{dim}) +## ttest (@var{x}, @var{y}, @var{Name}, @var{Value}) ## ## Perform a T-test of the null hypothesis @code{mean (@var{x}) == ## @var{m}} for a sample @var{x} from a normal distribution with unknown ## mean and unknown std deviation. Under the null, the test statistic -## @var{t} has a Student's t distribution. +## @var{t} has a Student's t distribution. The default value of +## @var{m} is 0. ## ## If the second argument @var{y} is a vector, a paired-t test of the ## hypothesis mean(x) = mean(y) is performed. ## -## The argument @var{alpha} can be used to specify the significance level -## of the test (the default value is 0.05). The string -## argument @var{tail}, can be used to select the desired alternative -## hypotheses. If @var{alt} is @qcode{"both"} (default) the null is -## tested against the two-sided alternative @code{mean (@var{x}) != @var{m}}. -## If @var{alt} is @qcode{"right"} the one-sided -## alternative @code{mean (@var{x}) > @var{m}} is considered. -## Similarly for @qcode{"left"}, the one-sided alternative @code{mean -## (@var{x}) < @var{m}} is considered. When argument @var{x} is a matrix -## the @var{dim} argument can be used to selection the dimension over -## which to perform the test. (The default is the first non-singleton -## dimension.) +## Name-Value pair arguments can be used to set various options. +## @var{'alpha'} can be used to specify the significance level +## of the test (the default value is 0.05). @var{'tail'}, can be used +## to select the desired alternative hypotheses. If the value is +## @qcode{'both'} (default) the null is tested against the two-sided +## alternative @code{mean (@var{x}) != @var{m}}. +## If it is @qcode{'right'} the one-sided alternative @code{mean (@var{x}) +## > @var{m}} is considered. Similarly for @qcode{'left'}, the one-sided +## alternative @code{mean (@var{x}) < @var{m}} is considered. +## When argument @var{x} is a matrix, @var{'dim'} can be used to selection +## the dimension over which to perform the test. (The default is the +## first non-singleton dimension.) ## ## If @var{h} is 0 the null hypothesis is accepted, if it is 1 the null ## hypothesis is rejected. The p-value of the test is returned in @var{pval}. @@ -67,43 +65,45 @@ ## @end deftypefn ## Author: Tony Richardson <richardson.tony@gmail.com> -## Description: Hypothesis test for mean of a normal sample with unknown variance +## Description: Test for mean of a normal sample with known variance -function [h, p, ci, stats] = ttest(x, my, alpha, tail, dim) +function [h, p, ci, stats] = ttest(x, my, varargin) + % Set default arguments my_default = 0; - alpha_default = 0.05; - tail_default = 'both'; + alpha = 0.05; + tail = 'both'; % Find the first non-singleton dimension of x - dim_default = min(find(size(x)~=1)); - if isempty(dim_default), dim_default = 1; end + dim = min(find(size(x)~=1)); + if isempty(dim), dim = 1; end - % Set the default argument values if input arguments are not present - switch (nargin) - case 1 - my = my_default; - alpha = alpha_default; - tail = tail_default; - dim = dim_default; - case 2 - alpha = alpha_default; - tail = tail_default; - dim = dim_default; - case 3 - tail = tail_default; - dim = dim_default; - case 4 - dim = dim_default; - case 5 - % Do nothing here. - % This is a valid case - otherwise - err_msg = 'Invalid call to ttest. Correct usage is:'; - err_msg = [err_msg '\n\n ttest(x, m) or ttest(x, y)\n\n']; - error(err_msg,[]); + if (nargin == 1) + my = my_default; + end + + i = 1; + while ( i <= length(varargin) ) + switch lower(varargin{i}) + case 'alpha' + i = i + 1; + alpha = varargin{i}; + case 'tail' + i = i + 1; + tail = varargin{i}; + case 'dim' + i = i + 1; + dim = varargin{i}; + otherwise + error('Invalid Name argument.',[]); + end + i = i + 1; end + if ~isa(tail, 'char') + error('tail argument to vartest2 must be a string\n',[]); + end + if any(and(~isscalar(my),size(x)~=size(my))) error('Arrays in paired test must be the same size.'); end @@ -112,20 +112,7 @@ if isempty(my) my = my_default; end - if isempty(alpha) - alpha = alpha_default; - end - if isempty(tail) - tail = tail_default; - end - if isempty(dim) - dim = dim_default; - end - - if ~isa(tail, 'char') - error('Fifth argument to ttest must be a string\n',[]); - end - + % This adjustment allows everything else to remain the % same for both the one-sample t test and paired tests. x = x - my;
--- a/main/statistics/inst/ttest2.m Sun Apr 20 13:44:17 2014 +0000 +++ b/main/statistics/inst/ttest2.m Sun Apr 20 13:49:11 2014 +0000 @@ -22,16 +22,7 @@ ## ttest2 (@var{x}, @var{y}) ## ## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest2 (@var{x}, @var{y}, @var{alpha}) -## -## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest2 (@var{x}, @var{y}, @var{alpha}, @var{tail}) -## -## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest2 (@var{x}, @var{y}, @var{alpha}, @var{tail}, @var{vartype}) -## -## {[@var{h}, @var{pval}, @var{ci}, @var{stats} ] =} -## ttest2 (@var{x}, @var{y}, @var{alpha}, @var{tail}, @var{vartype}, @var{dim}) +## ttest2 (@var{x}, @var{y}, @var{Name}, @var{Value}) ## ## Perform a T-test of the null hypothesis @code{mean (@var{x}) == ## @var{m}} for a sample @var{x} from a normal distribution with unknown @@ -66,59 +57,41 @@ ## @end deftypefn ## Author: Tony Richardson <richardson.tony@gmail.com> -## Description: Two sample Hypothesis test for mean of a normal sample with unknown variance +## Description: Test for mean of a normal sample with known variance -function [h, p, ci, stats] = ttest2(x, y, alpha, tail, vartype, dim) +function [h, p, ci, stats] = ttest2(x, y, varargin) - alpha_default = 0.05; - tail_default = 'both'; - vartype_default = 'equal'; + alpha = 0.05; + tail = 'both'; + vartype = 'equal'; % Find the first non-singleton dimension of x - dim_default = min(find(size(x)~=1)); - if isempty(dim_default), dim_default = 1; end + dim = min(find(size(x)~=1)); + if isempty(dim), dim = 1; end - % Set the default argument values if input arguments are not present - switch (nargin) - case 2 - alpha = alpha_default; - tail = tail_default; - vartype = vartype_default; - dim = dim_default; - case 3 - tail = tail_default; - vartype = vartype_default; - dim = dim_default; - case 4 - vartype = vartype_default; - dim = dim_default; - case 5 - dim = dim_default; - case 6 - % Do nothing here. - % This is a valid case - otherwise - err_msg = 'Invalid call to ttest2. Correct usage is:'; - err_msg = [err_msg '\n\n ttest2(x, y)\n\n']; - error(err_msg,[]); - end - - % Set default values if arguments are present but empty - if isempty(alpha) - alpha = alpha_default; - end - if isempty(tail) - tail = tail_default; - end - if isempty(vartype) - vartype = vartype_default; - end - if isempty(dim) - dim = dim_default; + i = 1; + while ( i <= length(varargin) ) + switch lower(varargin{i}) + case 'alpha' + i = i + 1; + alpha = varargin{i}; + case 'tail' + i = i + 1; + tail = varargin{i}; + case 'vartype' + i = i + 1; + vartype = varargin{i}; + case 'dim' + i = i + 1; + dim = varargin{i}; + otherwise + error('Invalid Name argument.',[]); + end + i = i + 1; end if ~isa(tail, 'char') - error('Fourth argument to ttest2 must be a string\n',[]); + error('Tail argument to ttest2 must be a string\n',[]); end m = size(x, dim); @@ -148,7 +121,7 @@ stats.tstat = x_bar./x_bar_std; - % Based on the "tail" argument determine the P-value, the critical values, + % Based on the "tail" argument determine the P-value, the critical values, % and the confidence interval. switch lower(tail) case 'both'
--- a/main/statistics/inst/ztest.m Sun Apr 20 13:44:17 2014 +0000 +++ b/main/statistics/inst/ztest.m Sun Apr 20 13:49:11 2014 +0000 @@ -22,31 +22,25 @@ ## ztest (@var{x}, @var{m}, @var{s}) ## ## {[@var{h}, @var{pval}, @var{ci}, @var{z}, @var{zcrit} ] =} -## ztest (@var{x}, @var{m}, @var{s}, @var{alpha}) -## -## {[@var{h}, @var{pval}, @var{ci}, @var{z}, @var{zcrit} ] =} -## ztest (@var{x}, @var{m}, @var{s}, @var{alpha}, @var{tail}) -## -## {[@var{h}, @var{pval}, @var{ci}, @var{z}, @var{zcrit} ] =} -## ztest (@var{x}, @var{m}, @var{s}, @var{alpha}, @var{tail}, @var{dim}) +## ztest (@var{x}, @var{m}, @var{s}, @var{Name}, @var{Value}) ## ## Perform a Z-test of the null hypothesis @code{mean (@var{x}) == ## @var{m}} for a sample @var{x} from a normal distribution with unknown ## mean and known std deviation @var{s}. Under the null, the test statistic ## @var{z} follows a standard normal distribution. ## -## The argument @var{alpha} can be used to specify the significance level -## of the test (the default value is 0.05). The string -## argument @var{tail}, can be used to select the desired alternative -## hypotheses. If @var{alt} is @qcode{"both"} (default) the null is -## tested against the two-sided alternative @code{mean (@var{x}) != @var{m}}. -## If @var{alt} is @qcode{"right"} the one-sided -## alternative @code{mean (@var{x}) > @var{m}} is considered. -## Similarly for @qcode{"left"}, the one-sided alternative @code{mean -## (@var{x}) < @var{m}} is considered. When argument @var{x} is a matrix -## the @var{dim} argument can be used to selection the dimension over -## which to perform the test. (The default is the first non-singleton -## dimension.) +## Name-Value pair arguments can be used to set various options. +## @var{'alpha'} can be used to specify the significance level +## of the test (the default value is 0.05). @var{'tail'}, can be used +## to select the desired alternative hypotheses. If the value is +## @qcode{'both'} (default) the null is tested against the two-sided +## alternative @code{mean (@var{x}) != @var{m}}. +## If it is @qcode{'right'} the one-sided alternative @code{mean (@var{x}) +## > @var{m}} is considered. Similarly for @qcode{'left'}, the one-sided +## alternative @code{mean (@var{x}) < @var{m}} is considered. +## When argument @var{x} is a matrix, @var{'dim'} can be used to selection +## the dimension over which to perform the test. (The default is the +## first non-singleton dimension.) ## ## If @var{h} is 0 the null hypothesis is accepted, if it is 1 the null ## hypothesis is rejected. The p-value of the test is returned in @var{pval}. @@ -56,50 +50,37 @@ ## @end deftypefn ## Author: Tony Richardson <richardson.tony@gmail.com> -## Description: Hypothesis test for mean of a normal sample with known variance +## Description: Test for mean of a normal sample with known variance -function [h, p, ci, zval, zcrit] = ztest(x, m, sigma, alpha, tail, dim) +function [h, p, ci, zval, zcrit] = ztest(x, m, sigma, varargin) - alpha_default = 0.05; - tail_default = 'both'; + alpha = 0.05; + tail = 'both'; % Find the first non-singleton dimension of x - dim_default = min(find(size(x)~=1)); - if isempty(dim_default), dim_default = 1; end - - % Set the default argument values if input arguments are not present - switch (nargin) - case 3 - alpha = alpha_default; - tail = tail_default; - dim = dim_default; - case 4 - tail = tail_default; - dim = dim_default; - case 5 - dim = dim_default; - case 6 - % Do nothing here. - % This is a valid case - otherwise - err_msg = 'Invalid call to ztest. Correct usage is:'; - err_msg = [err_msg '\n\n ztest(x, m, sigma)\n\n']; - error(err_msg,[]); - end - - % Set default values if arguments are present but empty - if isempty(alpha) - alpha = alpha_default; - end - if isempty(tail) - tail = tail_default; - end - if isempty(dim) - dim = dim_default; + dim = min(find(size(x)~=1)); + if isempty(dim), dim = 1; end + + i = 1; + while ( i <= length(varargin) ) + switch lower(varargin{i}) + case 'alpha' + i = i + 1; + alpha = varargin{i}; + case 'tail' + i = i + 1; + tail = varargin{i}; + case 'dim' + i = i + 1; + dim = varargin{i}; + otherwise + error('Invalid Name argument.',[]); + end + i = i + 1; end if ~isa(tail, 'char') - error('Fifth argument to ztest must be a string\n',[]); + error('tail argument to ztest must be a string\n',[]); end % Calculate the test statistic value (zval)