]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/ttest.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / ttest.m
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.
7 %
8 %     TTEST treads NaNs as "Missing values" and ignores these. 
9 %
10 % H = ttest(x,m)
11 %       tests Null-hypothesis that mean of x is m.              
12 % H = ttest(x,y)
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(...)
19 %
20 %     H=1 indicates a rejection of the Null-hypothesis at a significance 
21 %     level of alpha (default alpha = 0.05).     
22
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.
29
30 %     H returns whether the Null-Hypotheses must be rejected. 
31 %     The p-value of the test is returned in PVAL. 
32
33 %     TTEST works on the first non-singleton dimension or on DIM. 
34 %
35 %     If no output argument is given, the p-value of the test is
36 %     displayed.
37 %
38
39 %%% not supported yet 
40 % [h,p,ci] = ttest(...)
41 % [h,p,ci,stats] = ttest(...)
42
43 %       $Id$
44 %       Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2005, 2006, 2007
45 %               Kurt Hornik
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/
49
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.
54 %
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.
59 %
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/>.
62
63   if ((nargin < 2) || (nargin > 5) || nargout > 4)
64         print_usage ;
65   end
66
67   if (nargin == 2)
68     alt  = '~=';
69   end
70   if (nargin < 3) || isempty(alpha)
71     alpha = .05;
72   end
73
74   if (nargin < 4) || isempty(tail)
75     tail = '~=';
76   end
77   if (~ ischar (tail))
78     error ('ttest: tail must be a string');
79   end
80   if (nargin < 5) || isempty(vartype)
81     vartype = 'equal';
82   end
83   if ~strcmp(vartype,'equal')
84     error ('test: vartype not supported')
85   end   
86   if nargin<6,
87        DIM = find(size(x)>1,1);
88   end;
89   if isempty(DIM), DIM=1; end;
90
91
92   szx = size(x); 
93   szm = size(m);        
94   szx(DIM) = 1;   
95   szm(DIM) = 1;
96   if size(m,DIM)==1
97         ;
98   elseif size(x,DIM) == size(m,DIM)
99         x = x-m;
100         m = zeros(szm);
101   else
102     error ('ttest: dimension of X and Y do not fit');
103   end     
104   
105   [S, N] = sumskipnan(x, DIM);
106   stats.df = N - 1;
107   stats.sd = std (x);
108   stats.tstat = sqrt (N) .* (S./N - m) ./ stats.sd;
109   cdf = tcdf (stats.tstat, stats.df);
110
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'),
114     pval = 1 - cdf;
115   elseif strcmp (tail, '<') || strcmp(tail,'left'),
116     pval = cdf;
117   else
118     error ('ttest: option %s not recognized', tail);
119   end
120
121   h = pval < alpha;     
122   if (nargout == 0)
123     fprintf(1,'  pval: %g\n', pval);
124   end
125