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
27 #include "dictionary.h"
28 #include "group_proc.h"
41 /* This module calculates the Levene statistic for variables.
43 Just for reference, the Levene Statistic is a defines as follows:
45 W = \frac{ (n-k)\sum_{i=1}^k n_i(Z_{iL} - Z_{LL})^2}
46 { (k-1)\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - Z_{iL})^2}
49 k is the number of groups
50 n is the total number of samples
51 n_i is the number of samples in the ith group
52 Z_{ij} is | Y_{ij} - Y_{iL} | where Y_{iL} is the mean of the ith group
53 Z_{iL} is the mean of Z_{ij} over the ith group
54 Z_{LL} is the grand mean of Z_{ij}
56 Imagine calculating that with pencil and paper!
64 /* Per group statistics */
65 struct t_test_proc **group_stats;
67 /* The independent variable */
68 struct variable *v_indep;
70 /* Number of dependent variables */
73 /* The dependent variables */
74 struct variable **v_dep;
76 /* How to treat missing values */
77 enum lev_missing missing;
79 /* Function to test for missing values */
80 is_missing_func is_missing;
85 static void levene_precalc (const struct levene_info *l);
86 static int levene_calc (const struct ccase *, void *);
87 static void levene_postcalc (void *);
91 static void levene2_precalc (void *);
92 static int levene2_calc (const struct ccase *, void *);
93 static void levene2_postcalc (void *);
97 levene(const struct casefile *cf,
98 struct variable *v_indep, int n_dep, struct variable **v_dep,
99 enum lev_missing missing, is_missing_func value_is_missing)
101 struct casereader *r;
103 struct levene_info l;
109 l.is_missing = value_is_missing;
114 for(r = casefile_get_reader (cf);
115 casereader_read (r, &c) ;
120 casereader_destroy (r);
124 for(r = casefile_get_reader (cf);
125 casereader_read (r, &c) ;
130 casereader_destroy (r);
131 levene2_postcalc(&l);
135 /* Internal variables used in calculating the Levene statistic */
137 /* Per variable statistics */
140 /* Total of all lz */
146 /* The total number of cases */
149 /* Number of groups */
153 /* An array of lz_stats for each variable */
154 static struct lz_stats *lz;
158 levene_precalc (const struct levene_info *l)
162 lz = xmalloc (sizeof (struct lz_stats ) * l->n_dep ) ;
164 for(i = 0; i < l->n_dep ; ++i )
166 struct variable *var = l->v_dep[i];
167 struct group_proc *gp = group_proc_get (var);
168 struct group_statistics *gs;
169 struct hsh_iterator hi;
171 lz[i].grand_total = 0;
173 lz[i].n_groups = gp->n_groups ;
176 for ( gs = hsh_first(gp->group_hash, &hi);
178 gs = hsh_next(gp->group_hash, &hi))
188 levene_calc (const struct ccase *c, void *_l)
192 struct levene_info *l = (struct levene_info *) _l;
193 const union value *gv = case_data (c, l->v_indep->fv);
194 struct group_statistics key;
195 double weight = dict_get_case_weight(default_dict,c,&warn);
197 /* Skip the entire case if /MISSING=LISTWISE is set */
198 if ( l->missing == LEV_LISTWISE )
200 for (i = 0; i < l->n_dep; ++i)
202 struct variable *v = l->v_dep[i];
203 const union value *val = case_data (c, v->fv);
205 if (l->is_missing(val,v) )
215 for (i = 0; i < l->n_dep; ++i)
217 struct variable *var = l->v_dep[i];
218 struct group_proc *gp = group_proc_get (var);
220 const union value *v = case_data (c, var->fv);
221 struct group_statistics *gs;
223 gs = hsh_find(gp->group_hash,(void *) &key );
228 if ( ! l->is_missing(v,var))
230 levene_z= fabs(v->f - gs->mean);
231 lz[i].grand_total += levene_z * weight;
232 lz[i].total_n += weight;
234 gs->lz_total += levene_z * weight;
243 levene_postcalc (void *_l)
247 struct levene_info *l = (struct levene_info *) _l;
249 for (v = 0; v < l->n_dep; ++v)
252 lz[v].grand_mean = lz[v].grand_total / lz[v].total_n ;
259 /* The denominator for the expression for the Levene */
260 static double *lz_denominator;
263 levene2_precalc (void *_l)
267 struct levene_info *l = (struct levene_info *) _l;
269 lz_denominator = (double *) xmalloc(sizeof(double) * l->n_dep);
271 /* This stuff could go in the first post calc . . . */
272 for (v = 0; v < l->n_dep; ++v)
274 struct hsh_iterator hi;
275 struct group_statistics *g;
277 struct variable *var = l->v_dep[v] ;
278 struct hsh_table *hash = group_proc_get (var)->group_hash;
281 for(g = (struct group_statistics *) hsh_first(hash,&hi);
283 g = (struct group_statistics *) hsh_next(hash,&hi) )
285 g->lz_mean = g->lz_total / g->n ;
287 lz_denominator[v] = 0;
292 levene2_calc (const struct ccase *c, void *_l)
297 struct levene_info *l = (struct levene_info *) _l;
299 double weight = dict_get_case_weight(default_dict,c,&warn);
301 const union value *gv = case_data (c, l->v_indep->fv);
302 struct group_statistics key;
304 /* Skip the entire case if /MISSING=LISTWISE is set */
305 if ( l->missing == LEV_LISTWISE )
307 for (i = 0; i < l->n_dep; ++i)
309 struct variable *v = l->v_dep[i];
310 const union value *val = case_data (c, v->fv);
312 if (l->is_missing(val,v) )
321 for (i = 0; i < l->n_dep; ++i)
324 struct variable *var = l->v_dep[i] ;
325 const union value *v = case_data (c, var->fv);
326 struct group_statistics *gs;
328 gs = hsh_find(group_proc_get (var)->group_hash,(void *) &key );
333 if ( ! l->is_missing(v,var) )
335 levene_z = fabs(v->f - gs->mean);
336 lz_denominator[i] += weight * pow2(levene_z - gs->lz_mean);
345 levene2_postcalc (void *_l)
349 struct levene_info *l = (struct levene_info *) _l;
351 for (v = 0; v < l->n_dep; ++v)
353 double lz_numerator = 0;
354 struct hsh_iterator hi;
355 struct group_statistics *g;
357 struct variable *var = l->v_dep[v] ;
358 struct group_proc *gp = group_proc_get (var);
359 struct hsh_table *hash = gp->group_hash;
361 for(g = (struct group_statistics *) hsh_first(hash,&hi);
363 g = (struct group_statistics *) hsh_next(hash,&hi) )
365 lz_numerator += g->n * pow2(g->lz_mean - lz[v].grand_mean );
367 lz_numerator *= ( gp->ugs.n - gp->n_groups );
369 lz_denominator[v] *= (gp->n_groups - 1);
371 gp->levene = lz_numerator / lz_denominator[v] ;
375 /* Now clear up after ourselves */
376 free(lz_denominator);