3200
|
1 ## Copyright (C) 1996, 1997 Kurt Hornik |
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3200
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3454
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} manova (@var{y}, @var{g}) |
|
21 ## Perform a one-way multivariate analysis of variance (MANOVA). The |
3200
|
22 ## goal is to test whether the p-dimensional population means of data |
3454
|
23 ## taken from @var{k} different groups are all equal. All data are |
|
24 ## assumed drawn independently from p-dimensional normal distributions |
|
25 ## with the same covariance matrix. |
3200
|
26 ## |
3454
|
27 ## The data matrix is given by @var{y}. As usual, rows are observations |
|
28 ## and columns are variables. The vector @var{g} specifies the |
|
29 ## corresponding group labels (e.g., numbers from 1 to @var{k}). |
3200
|
30 ## |
|
31 ## The LR test statistic (Wilks' Lambda) and approximate p-values are |
|
32 ## computed and displayed. |
3454
|
33 ## @end deftypefn |
3200
|
34 |
|
35 ## Three test statistics (Wilks, Hotelling-Lawley, and Pillai-Bartlett) |
|
36 ## and corresponding approximate p-values are calculated and displayed. |
|
37 ## (Currently NOT because the f_cdf respectively betai code is too bad.) |
3426
|
38 |
3456
|
39 ## Author: TF <Thomas.Fuereder@ci.tuwien.ac.at> |
5428
|
40 ## Adapted-By: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
41 ## Description: One-way multivariate analysis of variance (MANOVA) |
3200
|
42 |
|
43 function manova (Y, g) |
|
44 |
|
45 if (nargin != 2) |
6046
|
46 print_usage (); |
3200
|
47 endif |
|
48 |
4030
|
49 if (isvector (Y)) |
3456
|
50 error ("manova: Y must not be a vector"); |
3200
|
51 endif |
|
52 |
|
53 [n, p] = size (Y); |
|
54 |
4030
|
55 if (!isvector (g) || (length (g) != n)) |
3456
|
56 error ("manova: g must be a vector of length rows (Y)"); |
3200
|
57 endif |
|
58 |
|
59 s = sort (g); |
|
60 i = find (s (2:n) > s(1:(n-1))); |
|
61 k = length (i) + 1; |
3426
|
62 |
3200
|
63 if (k == 1) |
3456
|
64 error ("manova: there should be at least 2 groups"); |
3200
|
65 else |
3273
|
66 group_label = s ([1, (reshape (i, 1, k - 1) + 1)]); |
3200
|
67 endif |
|
68 |
|
69 Y = Y - ones (n, 1) * mean (Y); |
|
70 SST = Y' * Y; |
|
71 |
|
72 s = zeros (1, p); |
|
73 SSB = zeros (p, p); |
|
74 for i = 1 : k; |
|
75 v = Y (find (g == group_label (i)), :); |
|
76 s = sum (v); |
|
77 SSB = SSB + s' * s / rows (v); |
|
78 endfor |
|
79 n_b = k - 1; |
3426
|
80 |
3200
|
81 SSW = SST - SSB; |
|
82 n_w = n - k; |
|
83 |
|
84 l = real (eig (SSB / SSW)); |
|
85 l (l < eps) = 0; |
|
86 |
|
87 ## Wilks' Lambda |
|
88 ## ============= |
|
89 |
|
90 Lambda = prod (1 ./ (1 + l)); |
3426
|
91 |
3200
|
92 delta = n_w + n_b - (p + n_b + 1) / 2 |
|
93 df_num = p * n_b |
|
94 W_pval_1 = 1 - chisquare_cdf (- delta * log (Lambda), df_num); |
3426
|
95 |
3200
|
96 if (p < 3) |
|
97 eta = p; |
|
98 else |
|
99 eta = sqrt ((p^2 * n_b^2 - 4) / (p^2 + n_b^2 - 5)) |
|
100 endif |
|
101 |
|
102 df_den = delta * eta - df_num / 2 + 1 |
3426
|
103 |
3200
|
104 WT = exp (- log (Lambda) / eta) - 1 |
|
105 W_pval_2 = 1 - f_cdf (WT * df_den / df_num, df_num, df_den); |
|
106 |
|
107 if (0) |
|
108 |
|
109 ## Hotelling-Lawley Test |
|
110 ## ===================== |
3426
|
111 |
3200
|
112 HL = sum (l); |
3426
|
113 |
3200
|
114 theta = min (p, n_b); |
3426
|
115 u = (abs (p - n_b) - 1) / 2; |
3200
|
116 v = (n_w - p - 1) / 2; |
|
117 |
|
118 df_num = theta * (2 * u + theta + 1); |
|
119 df_den = 2 * (theta * v + 1); |
|
120 |
|
121 HL_pval = 1 - f_cdf (HL * df_den / df_num, df_num, df_den); |
|
122 |
|
123 ## Pillai-Bartlett |
|
124 ## =============== |
3426
|
125 |
3200
|
126 PB = sum (l ./ (1 + l)); |
|
127 |
|
128 df_den = theta * (2 * v + theta + 1); |
|
129 PB_pval = 1 - f_cdf (PB * df_den / df_num, df_num, df_den); |
|
130 |
|
131 printf ("\n"); |
|
132 printf ("One-way MANOVA Table:\n"); |
3426
|
133 printf ("\n"); |
3200
|
134 printf ("Test Test Statistic Approximate p\n"); |
|
135 printf ("**************************************************\n"); |
|
136 printf ("Wilks %10.4f %10.9f \n", Lambda, W_pval_1); |
|
137 printf (" %10.9f \n", W_pval_2); |
|
138 printf ("Hotelling-Lawley %10.4f %10.9f \n", HL, HL_pval); |
|
139 printf ("Pillai-Bartlett %10.4f %10.9f \n", PB, PB_pval); |
|
140 printf ("\n"); |
|
141 |
|
142 endif |
|
143 |
|
144 printf ("\n"); |
|
145 printf ("MANOVA Results:\n"); |
|
146 printf ("\n"); |
3456
|
147 printf ("# of groups: %d\n", k); |
|
148 printf ("# of samples: %d\n", n); |
|
149 printf ("# of variables: %d\n", p); |
3426
|
150 printf ("\n"); |
3456
|
151 printf ("Wilks' Lambda: %5.4f\n", Lambda); |
|
152 printf ("Approximate p: %10.9f (chisquare approximation)\n", W_pval_1); |
3200
|
153 printf (" %10.9f (F approximation)\n", W_pval_2); |
|
154 printf ("\n"); |
3426
|
155 |
3200
|
156 endfunction |