3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3200
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3200
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3200
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3454
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {[@var{pval}, @var{t}, @var{df}] =} t_test (@var{x}, @var{m}, @var{alt}) |
|
19 ## For a sample @var{x} from a normal distribution with unknown mean and |
|
20 ## variance, perform a t-test of the null hypothesis @code{mean |
|
21 ## (@var{x}) == @var{m}}. Under the null, the test statistic @var{t} |
|
22 ## follows a Student distribution with @code{@var{df} = length (@var{x}) |
|
23 ## - 1} degrees of freedom. |
3200
|
24 ## |
3454
|
25 ## With the optional argument string @var{alt}, the alternative of |
|
26 ## interest can be selected. If @var{alt} is @code{"!="} or |
|
27 ## @code{"<>"}, the null is tested against the two-sided alternative |
|
28 ## @code{mean (@var{x}) != @var{m}}. If @var{alt} is @code{">"}, the |
3457
|
29 ## one-sided alternative @code{mean (@var{x}) > @var{m}} is considered. |
|
30 ## Similarly for @var{"<"}, the one-sided alternative @code{mean |
|
31 ## (@var{x}) < @var{m}} is considered, The default is the two-sided |
|
32 ## case. |
3200
|
33 ## |
3454
|
34 ## The p-value of the test is returned in @var{pval}. |
3426
|
35 ## |
3200
|
36 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
37 ## @end deftypefn |
3426
|
38 |
3456
|
39 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
40 ## Description: Student's one-sample t test |
3200
|
41 |
|
42 function [pval, t, df] = t_test (x, m, alt) |
3426
|
43 |
3200
|
44 if ((nargin < 2) || (nargin > 3)) |
3456
|
45 usage ("[pval, t, df] = t_test (x, m, alt)"); |
3200
|
46 endif |
3426
|
47 |
3200
|
48 if (! is_vector (x)) |
3458
|
49 error ("t_test: x must be a vector"); |
3200
|
50 endif |
|
51 if (! is_scalar (m)) |
3458
|
52 error ("t_test: m must be a scalar"); |
3200
|
53 endif |
3426
|
54 |
3200
|
55 n = length (x); |
|
56 df = n - 1; |
|
57 t = sqrt (n) * (sum (x) / n - m) / std (x); |
|
58 cdf = t_cdf (t, df); |
3426
|
59 |
3200
|
60 if (nargin == 2) |
|
61 alt = "!="; |
|
62 endif |
|
63 |
|
64 if (! isstr (alt)) |
3456
|
65 error ("t_test: alt must be a string"); |
3200
|
66 endif |
|
67 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
68 pval = 2 * min (cdf, 1 - cdf); |
|
69 elseif strcmp (alt, ">") |
|
70 pval = 1 - cdf; |
|
71 elseif strcmp (alt, "<") |
|
72 pval = cdf; |
|
73 else |
3456
|
74 error ("t_test: option %s not recognized", alt); |
3200
|
75 endif |
|
76 |
|
77 if (nargout == 0) |
3456
|
78 printf (" pval: %g\n", pval); |
3200
|
79 endif |
3426
|
80 |
3200
|
81 endfunction |