1 function [h, pval, ci, stats] = ttest (x, m, alpha, tail, vartype, DIM)
2 % TTEST (paired) t-test
3 % For a sample X from a normal distribution with unknown mean and
4 % variance, perform a t-test of the null hypothesis `mean (X) == M'.
5 % Under the null, the test statistic T follows a Student
6 % distribution with `DF = length (X) - 1' degrees of freedom.
8 % TTEST treads NaNs as "Missing values" and ignores these.
11 % tests Null-hypothesis that mean of x is m.
13 % size of x and size of y must match, it is tested whether the
14 % difference x-y is significantly different to m=0;
15 % H = ttest(x,y,alpha)
16 % H = ttest(x,y,alpha,tail)
17 % H = ttest(x,y,alpha,tail,DIM)
18 % [H,PVAL] = ttest(...)
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 % H returns whether the Null-Hypotheses must be rejected.
31 % The p-value of the test is returned in PVAL.
33 % TTEST works on the first non-singleton dimension or on DIM.
35 % If no output argument is given, the p-value of the test is
40 % [h,p,ci] = ttest(...)
41 % [h,p,ci,stats] = ttest(...)
44 % Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
46 % Copyright (C) 2010 by Alois Schloegl <alois.schloegl@gmail.com>
47 % This function is part of the NaN-toolbox
48 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
50 % This program is free software: you can redistribute it and/or modify
51 % it under the terms of the GNU General Public License as published by
52 % the Free Software Foundation, either version 3 of the License, or
53 % (at your option) any later version.
55 % This program is distributed in the hope that it will be useful,
56 % but WITHOUT ANY WARRANTY; without even the implied warranty of
57 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 % GNU General Public License for more details.
60 % You should have received a copy of the GNU General Public License
61 % along with this program. If not, see <http://www.gnu.org/licenses/>.
63 if ((nargin < 2) || (nargin > 5) || nargout > 4)
70 if (nargin < 3) || isempty(alpha)
74 if (nargin < 4) || isempty(tail)
78 error ('ttest: tail must be a string');
80 if (nargin < 5) || isempty(vartype)
83 if ~strcmp(vartype,'equal')
84 error ('test: vartype not supported')
87 DIM = find(size(x)>1,1);
89 if isempty(DIM), DIM=1; end;
98 elseif size(x,DIM) == size(m,DIM)
102 error ('ttest: dimension of X and Y do not fit');
105 [S, N] = sumskipnan(x, DIM);
108 stats.tstat = sqrt (N) .* (S./N - m) ./ stats.sd;
109 cdf = tcdf (stats.tstat, stats.df);
111 if (strcmp (tail, '~=') || strcmp (tail, '!=') || strcmp (tail, '<>')) || strcmp(tail,'both'),
112 pval = 2 * min (cdf, 1 - cdf);
113 elseif strcmp (tail, '>') || strcmp(tail,'right'),
115 elseif strcmp (tail, '<') || strcmp(tail,'left'),
118 error ('ttest: option %s not recognized', tail);
123 fprintf(1,' pval: %g\n', pval);