1 function [h, pval, ci, stats, df] = ttest2 (x, y, alpha, tail, vartype, DIM)
2 % TTEST2 (unpaired) t-test
3 % For two samples x and y from normal distributions with unknown
4 % means and unknown equal variances, perform a two-sample t-test of
5 % the null hypothesis of equal means. Under the null, the test
6 % statistic T follows a Student distribution with DF degrees of
9 % TTEST2 treads NaNs as "Missing values" and ignores these.
12 % H = ttest2([x;y],C,W)
13 % H = ttest2(x,y,alpha)
14 % H = ttest2(x,y,alpha,tail)
15 % H = ttest2(x,y,alpha,tail,vartype)
16 % H = ttest2(x,y,alpha,tail,vartype,DIM)
17 % [H,PVAL] = ttest2(...)
18 % [h,p,ci,stats] = ttest2(...)
20 % H=1 indicates a rejection of the Null-hypothesis at a significance
21 % level of alpha (default alpha = 0.05).
23 % With the optional argument string TAIL, the Alternative of interest
24 % can be selected. If TAIL is '!=' or '<>' or 'both', the null is tested
25 % against the two-sided Alternative `mean (X) ~= mean (Y)'. If TAIL
26 % is '>' or 'right', the one-sided Alternative `mean (X) > mean (Y)' is used.
27 % Similarly for '<' or 'left', the one-sided Alternative `mean (X) < mean
28 % (Y)' is used. The default is the two-sided case.
30 % vartype support only 'equal' (default value); the value 'unequal' is not supported.
32 % H returns whether the Null-Hypotheses must be rejected.
33 % The p-value of the test is returned in PVAL.
35 % TTEST2 works on the first non-singleton dimension or on DIM.
37 % If no output argument is given, the p-value of the test is
42 % [h,p,ci] = ttest2(...)
43 % [h,p,ci,stats] = ttest2(...)
46 % Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
48 % Copyright (C) 2010 by Alois Schloegl <alois.schloegl@gmail.com>
49 % This function is part of the NaN-toolbox
50 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
52 % This program is free software: you can redistribute it and/or modify
53 % it under the terms of the GNU General Public License as published by
54 % the Free Software Foundation, either version 3 of the License, or
55 % (at your option) any later version.
57 % This program is distributed in the hope that it will be useful,
58 % but WITHOUT ANY WARRANTY; without even the implied warranty of
59 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 % GNU General Public License for more details.
62 % You should have received a copy of the GNU General Public License
63 % along with this program. If not, see <http://www.gnu.org/licenses/>.
65 if ((nargin < 2) || (nargin > 6) || nargout > 4)
69 if (nargin < 3) || isempty(alpha)
72 elseif size(x,1)==size(y,1) && size(x,1)==size(alpha,1) && size(y,2)==1 && all(y==1 | y==2)
81 if (nargin < 4) || isempty(tail)
85 error ('ttest2: tail must be a string');
87 if (nargin < 5) || isempty(vartype)
90 if ~strcmp(vartype,'equal')
91 error ('test: vartype not supported')
94 DIM = find(size(x)>1,1);
96 if isempty(DIM), DIM=1; end;
104 error ('ttest2: dimension of X and Y do not fit');
107 [SX, NX] = sumskipnan(x, DIM, Wx);
108 [SY, NY] = sumskipnan(y, DIM, Wy);
109 stats.df = NX + NY - 2;
113 if any(size(x)==0) || any(size(y)==0)
116 v = sumsq(x-repmat(MX,size(x)./size(MX))) + sumsq(y-repmat(MY,size(y)./size(MY)));
118 stats.sd = sqrt(v/stats.df);
119 stats.tstat = (MX - MY) .* sqrt ((NX .* NY .* stats.df) ./ (v .* (NX + NY)));
120 cdf = tcdf (stats.tstat, stats.df);
122 if (strcmp (tail, '~=') || strcmp (tail, '!=') || strcmp (tail, '<>')) || strcmp(tail,'both'),
123 pval = 2 * min (cdf, 1 - cdf);
124 elseif strcmp (tail, '>') || strcmp(tail,'right'),
126 elseif strcmp (tail, '<') || strcmp(tail,'left'),
129 error ('ttest2: option %s not recognized', tail);
135 fprintf(1,' pval: %g\n', pval);