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}] =} welch_test (@var{x}, @var{y}, @var{alt}) |
|
19 ## For two samples @var{x} and @var{y} from normal distributions with |
|
20 ## unknown means and unknown and not necessarily equal variances, |
|
21 ## perform a Welch test of the null hypothesis of equal means. |
3200
|
22 ## Under the null, the test statistic t approximately follows a Student |
|
23 ## distribution with df degrees of freedom. |
|
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 |
|
29 ## one-sided alternative mean(x) > m is considered, similarly for |
|
30 ## @code{"<"}. The default is the two-sided case. |
3200
|
31 ## |
3454
|
32 ## The p-value of the test is returned in @var{pval}. |
3426
|
33 ## |
3200
|
34 ## If no output argument is given, the p-value of the test is displayed. |
3454
|
35 ## @end deftypefn |
3200
|
36 |
|
37 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
38 ## Description: Welch two-sample t test |
|
39 |
|
40 function [pval, t, df] = welch_test (x, y, alt) |
3426
|
41 |
3200
|
42 if ((nargin < 2) || (nargin > 3)) |
|
43 usage ("[pval, t, df] = welch_test (x, y [, alt])"); |
|
44 endif |
3426
|
45 |
3200
|
46 if (! (is_vector (x) && is_vector (y))) |
|
47 error ("welch_test: both x and y must be vectors"); |
|
48 endif |
|
49 |
|
50 n_x = length (x); |
|
51 n_y = length (y); |
|
52 mu_x = sum (x) / n_x; |
|
53 mu_y = sum (y) / n_y; |
|
54 v_x = sumsq (x - mu_x) / (n_x * (n_x - 1)); |
|
55 v_y = sumsq (x - mu_y) / (n_y * (n_y - 1)); |
|
56 c = v_x / (v_x + v_y); |
|
57 df = 1 / ( c^2 / (n_x - 1) + (1 - c)^2 / (n_y - 1)); |
|
58 t = (mu_x - mu_y) / sqrt (v_x + v_y); |
|
59 cdf = t_cdf (t, df); |
|
60 |
|
61 if (nargin == 2) |
|
62 alt = "!="; |
|
63 endif |
3426
|
64 |
3200
|
65 if (! isstr (alt)) |
|
66 error ("welch_test: alt must be a string"); |
|
67 endif |
|
68 if (strcmp (alt, "!=") || strcmp (alt, "<>")) |
|
69 pval = 2 * min (cdf, 1 - cdf); |
|
70 elseif (strcmp (alt, ">")) |
|
71 pval = 1 - cdf; |
|
72 elseif (strcmp (alt, "<")) |
|
73 pval = cdf; |
|
74 else |
|
75 error (sprintf ("welch_test: option %s not recognized", alt)); |
|
76 endif |
3426
|
77 |
3200
|
78 if (nargout == 0) |
|
79 printf (" pval: %g\n", pval); |
|
80 endif |
|
81 |
|
82 endfunction |