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