3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
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. |
|
7 ## |
|
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 |
|
11 ## General Public License for more details. |
|
12 ## |
|
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 |
|
17 ## usage: [pval, t, df] = t_test (x, m [, alt]) |
|
18 ## |
|
19 ## For a sample x from a normal distribution with unknown mean and |
|
20 ## variance, perform a t-test of the null hypothesis mean(x) == m. |
|
21 ## Under the null, the test statistic t follows a Student distribution |
|
22 ## with df = length (x) - 1 degrees of freedom. |
|
23 ## |
|
24 ## With the optional argument string alt, the alternative of interest |
|
25 ## can be selected. |
|
26 ## If alt is "!=" or "<>", the null is tested against the two-sided |
|
27 ## alternative mean(x) != m. |
|
28 ## If alt is ">", the one-sided alternative mean(x) > m is considered, |
|
29 ## similarly for "<". |
|
30 ## The default is the two-sided case. |
|
31 ## |
|
32 ## pval is the p-value of the test. |
|
33 ## |
|
34 ## If no output argument is given, the p-value of the test is displayed. |
|
35 |
|
36 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
37 ## Description: Student's one-sample t test |
|
38 |
|
39 function [pval, t, df] = t_test (x, m, alt) |
|
40 |
|
41 if ((nargin < 2) || (nargin > 3)) |
|
42 usage ("[pval, t, df] = t_test (x, m [, alt])"); |
|
43 endif |
|
44 |
|
45 if (! is_vector (x)) |
|
46 error ("t_test: x must be a vector."); |
|
47 endif |
|
48 if (! is_scalar (m)) |
|
49 error ("t_test: m must be a scalar."); |
|
50 endif |
|
51 |
|
52 n = length (x); |
|
53 df = n - 1; |
|
54 t = sqrt (n) * (sum (x) / n - m) / std (x); |
|
55 cdf = t_cdf (t, df); |
|
56 |
|
57 if (nargin == 2) |
|
58 alt = "!="; |
|
59 endif |
|
60 |
|
61 if (! isstr (alt)) |
|
62 error ("t_test: alt must be a string"); |
|
63 endif |
|
64 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
65 pval = 2 * min (cdf, 1 - cdf); |
|
66 elseif strcmp (alt, ">") |
|
67 pval = 1 - cdf; |
|
68 elseif strcmp (alt, "<") |
|
69 pval = cdf; |
|
70 else |
|
71 error (sprintf ("t_test: option %s not recognized", alt)); |
|
72 endif |
|
73 |
|
74 if (nargout == 0) |
|
75 printf (" pval: %g\n", pval); |
|
76 endif |
|
77 |
|
78 endfunction |