1 /* This file is part of GNU PSPP
2 Computes Levene test statistic.
4 Copyright (C) 2004 Free Software Foundation, Inc.
5 Written by John Darrington <john@darrington.wattle.id.au>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 /* This module calculates the Levene statistic for variables.
37 Just for reference, the Levene Statistic is a defines as follows:
39 W = \frac{ (n-k)\sum_{i=1}^k n_i(Z_{iL} - Z_{LL})^2}
40 { (k-1)\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - Z_{iL})^2}
43 k is the number of groups
44 n is the total number of samples
45 n_i is the number of samples in the ith group
46 Z_{ij} is | Y_{ij} - Y_{iL} | where Y_{iL} is the mean of the ith group
47 Z_{iL} is the mean of Z_{ij} over the ith group
48 Z_{LL} is the grand mean of Z_{ij}
50 Imagine calculating that with pencil and paper!
54 static void levene_precalc (void *);
55 static int levene_calc (struct ccase *, void *);
56 static void levene_postcalc (void *);
60 static void levene2_precalc (void *);
61 static int levene2_calc (struct ccase *, void *);
62 static void levene2_postcalc (void *);
68 /* The number of groups */
71 /* Per group statistics */
72 struct t_test_proc **group_stats;
74 /* The independent variable */
75 struct variable *v_indep;
77 /* Number of dependent variables */
80 /* The dependent variables */
81 struct variable **v_dep;
88 levene(struct variable *v_indep, int n_dep, struct variable **v_dep)
96 procedure(levene_precalc, levene_calc, levene_postcalc, &l);
97 procedure(levene2_precalc,levene2_calc,levene2_postcalc,&l);
101 static struct hsh_table **hash;
104 compare_group_id(const void *a_, const void *b_, void *aux)
106 const struct group_statistics *a = (struct group_statistics *) a_;
107 const struct group_statistics *b = (struct group_statistics *) b_;
109 int width = (int) aux;
111 return compare_values(&a->id, &b->id, width);
116 hash_group_id(const void *g_, void *aux)
118 const struct group_statistics *g = (struct group_statistics *) g_;
120 int width = (int) aux;
123 return hsh_hash_double (g->id.f);
125 return hsh_hash_bytes (g->id.s, width);
129 /* Internal variables used in calculating the Levene statistic */
131 /* Per variable statistics */
134 /* Total of all lz */
140 /* The total number of cases */
143 /* Number of groups */
147 /* An array of lz_stats for each variable */
148 static struct lz_stats *lz;
153 levene_precalc (void *_l)
156 struct levene_info *l = (struct levene_info *) _l;
158 lz = xmalloc (sizeof (struct lz_stats ) * l->n_dep ) ;
160 hash = xmalloc (sizeof ( struct hsh_table *) * l->n_dep );
162 for(i=0; i < l->n_dep ; ++i )
164 struct variable *v = l->v_dep[i];
166 int number_of_groups = v->p.t_t.n_groups ;
168 hash[i] = hsh_create (l->n_dep * number_of_groups,
169 compare_group_id, hash_group_id,
170 0,(void *) l->v_indep->width);
172 lz[i].grand_total = 0;
174 lz[i].n_groups = number_of_groups;
176 for (g = 0 ; g < v->p.t_t.n_groups ; ++g )
178 struct group_statistics *gs = &v->p.t_t.gs[g];
180 hsh_insert(hash[i],gs);
187 levene_calc (struct ccase *c, void *_l)
190 struct levene_info *l = (struct levene_info *) _l;
191 union value *gv = &c->data[l->v_indep->fv];
192 struct group_statistics key;
193 double weight = dict_get_case_weight(default_dict,c);
197 for (var = 0; var < l->n_dep; ++var)
200 union value *v = &c->data[l->v_dep[var]->fv];
201 struct group_statistics *gs;
202 gs = hsh_find(hash[var],&key);
203 assert(0 == compare_values(&gs->id, &key.id, l->v_indep->width));
205 /* FIXME: handle SYSMIS properly */
207 levene_z= fabs(v->f - gs->mean);
208 lz[var].grand_total += levene_z * weight;
209 lz[var].total_n += weight;
211 gs->lz_total += levene_z * weight;
219 levene_postcalc (void *_l)
223 struct levene_info *l = (struct levene_info *) _l;
225 for (v = 0; v < l->n_dep; ++v)
227 lz[v].grand_mean = lz[v].grand_total / lz[v].total_n ;
234 /* The denominator for the expression for the Levene */
235 static double *lz_denominator;
238 levene2_precalc (void *_l)
242 struct levene_info *l = (struct levene_info *) _l;
244 lz_denominator = (double *) xmalloc(sizeof(double) * l->n_dep);
246 /* This stuff could go in the first post calc . . . */
247 for (v = 0; v < l->n_dep; ++v)
249 struct hsh_iterator hi;
250 struct group_statistics *g;
251 for(g = (struct group_statistics *) hsh_first(hash[v],&hi);
253 g = (struct group_statistics *) hsh_next(hash[v],&hi) )
255 g->lz_mean = g->lz_total/g->n ;
257 lz_denominator[v] = 0;
262 levene2_calc (struct ccase *c, void *_l)
266 struct levene_info *l = (struct levene_info *) _l;
268 double weight = dict_get_case_weight(default_dict,c);
270 union value *gv = &c->data[l->v_indep->fv];
271 struct group_statistics key;
275 for (var = 0; var < l->n_dep; ++var)
278 union value *v = &c->data[l->v_dep[var]->fv];
279 struct group_statistics *gs;
280 gs = hsh_find(hash[var],&key);
282 assert(0 == compare_values(&gs->id, &key.id, l->v_indep->width));
284 /* FIXME: handle SYSMIS properly */
286 levene_z = fabs(v->f - gs->mean);
288 lz_denominator[var] += weight * sqr(levene_z - gs->lz_mean);
296 levene2_postcalc (void *_l)
300 struct levene_info *l = (struct levene_info *) _l;
302 for (v = 0; v < l->n_dep; ++v)
304 double lz_numerator = 0;
305 struct hsh_iterator hi;
306 struct group_statistics *g;
307 for(g = (struct group_statistics *) hsh_first(hash[v],&hi);
309 g = (struct group_statistics *) hsh_next(hash[v],&hi) )
312 lz_numerator += g->n * sqr(g->lz_mean - lz[v].grand_mean );
316 lz_numerator *= ( l->v_dep[v]->p.t_t.ugs.n -
317 l->v_dep[v]->p.t_t.n_groups );
319 lz_denominator[v] /= (l->v_dep[v]->p.t_t.n_groups - 1);
321 l->v_dep[v]->p.t_t.levene = lz_numerator/lz_denominator[v] ;
324 /* Now clear up after ourselves */
325 free(lz_denominator);
326 for (v = 0; v < l->n_dep; ++v)
328 hsh_destroy(hash[v]);