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
39 /* This module calculates the Levene statistic for variables.
41 Just for reference, the Levene Statistic is a defines as follows:
43 W = \frac{ (n-k)\sum_{i=1}^k n_i(Z_{iL} - Z_{LL})^2}
44 { (k-1)\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - Z_{iL})^2}
47 k is the number of groups
48 n is the total number of samples
49 n_i is the number of samples in the ith group
50 Z_{ij} is | Y_{ij} - Y_{iL} | where Y_{iL} is the mean of the ith group
51 Z_{iL} is the mean of Z_{ij} over the ith group
52 Z_{LL} is the grand mean of Z_{ij}
54 Imagine calculating that with pencil and paper!
62 /* Per group statistics */
63 struct t_test_proc **group_stats;
65 /* The independent variable */
66 struct variable *v_indep;
68 /* Number of dependent variables */
71 /* The dependent variables */
72 struct variable **v_dep;
74 /* How to treat missing values */
75 enum lev_missing missing;
77 /* Function to test for missing values */
78 is_missing_func is_missing;
83 static void levene_precalc (const struct levene_info *l);
84 static int levene_calc (const struct ccase *, void *);
85 static void levene_postcalc (void *);
89 static void levene2_precalc (void *);
90 static int levene2_calc (const struct ccase *, void *);
91 static void levene2_postcalc (void *);
95 levene(const struct casefile *cf,
96 struct variable *v_indep, int n_dep, struct variable **v_dep,
97 enum lev_missing missing, is_missing_func value_is_missing)
101 struct levene_info l;
107 l.is_missing = value_is_missing;
112 for(r = casefile_get_reader (cf);
113 casereader_read (r, &c) ;
118 casereader_destroy (r);
122 for(r = casefile_get_reader (cf);
123 casereader_read (r, &c) ;
128 casereader_destroy (r);
129 levene2_postcalc(&l);
133 /* Internal variables used in calculating the Levene statistic */
135 /* Per variable statistics */
138 /* Total of all lz */
144 /* The total number of cases */
147 /* Number of groups */
151 /* An array of lz_stats for each variable */
152 static struct lz_stats *lz;
156 levene_precalc (const struct levene_info *l)
160 lz = xmalloc (sizeof (struct lz_stats ) * l->n_dep ) ;
162 for(i=0; i < l->n_dep ; ++i )
164 struct variable *v = l->v_dep[i];
166 lz[i].grand_total = 0;
168 lz[i].n_groups = v->p.grp_data.n_groups ;
174 levene_calc (const struct ccase *c, void *_l)
178 struct levene_info *l = (struct levene_info *) _l;
179 const union value *gv = case_data (c, l->v_indep->fv);
180 struct group_statistics key;
181 double weight = dict_get_case_weight(default_dict,c,&warn);
183 /* Skip the entire case if /MISSING=LISTWISE is set */
184 if ( l->missing == LEV_LISTWISE )
186 for (i = 0; i < l->n_dep; ++i)
188 struct variable *v = l->v_dep[i];
189 const union value *val = case_data (c, v->fv);
191 if (l->is_missing(val,v) )
201 for (i = 0; i < l->n_dep; ++i)
203 struct variable *var = l->v_dep[i];
205 const union value *v = case_data (c, var->fv);
206 struct group_statistics *gs;
208 gs = hsh_find(var->p.grp_data.group_hash,(void *) &key );
213 if ( ! l->is_missing(v,var))
215 levene_z= fabs(v->f - gs->mean);
216 lz[i].grand_total += levene_z * weight;
217 lz[i].total_n += weight;
219 gs->lz_total += levene_z * weight;
227 levene_postcalc (void *_l)
231 struct levene_info *l = (struct levene_info *) _l;
233 for (v = 0; v < l->n_dep; ++v)
236 lz[v].grand_mean = lz[v].grand_total / lz[v].total_n ;
242 /* The denominator for the expression for the Levene */
243 static double *lz_denominator;
246 levene2_precalc (void *_l)
250 struct levene_info *l = (struct levene_info *) _l;
252 lz_denominator = (double *) xmalloc(sizeof(double) * l->n_dep);
254 /* This stuff could go in the first post calc . . . */
255 for (v = 0; v < l->n_dep; ++v)
257 struct hsh_iterator hi;
258 struct group_statistics *g;
260 struct variable *var = l->v_dep[v] ;
261 struct hsh_table *hash = var->p.grp_data.group_hash;
264 for(g = (struct group_statistics *) hsh_first(hash,&hi);
266 g = (struct group_statistics *) hsh_next(hash,&hi) )
268 g->lz_mean = g->lz_total/g->n ;
270 lz_denominator[v] = 0;
275 levene2_calc (const struct ccase *c, void *_l)
280 struct levene_info *l = (struct levene_info *) _l;
282 double weight = dict_get_case_weight(default_dict,c,&warn);
284 const union value *gv = case_data (c, l->v_indep->fv);
285 struct group_statistics key;
287 /* Skip the entire case if /MISSING=LISTWISE is set */
288 if ( l->missing == LEV_LISTWISE )
290 for (i = 0; i < l->n_dep; ++i)
292 struct variable *v = l->v_dep[i];
293 const union value *val = case_data (c, v->fv);
295 if (l->is_missing(val,v) )
304 for (i = 0; i < l->n_dep; ++i)
307 struct variable *var = l->v_dep[i] ;
308 const union value *v = case_data (c, var->fv);
309 struct group_statistics *gs;
311 gs = hsh_find(var->p.grp_data.group_hash,(void *) &key );
316 if ( ! l->is_missing(v,var) )
318 levene_z = fabs(v->f - gs->mean);
319 lz_denominator[i] += weight * pow2(levene_z - gs->lz_mean);
328 levene2_postcalc (void *_l)
332 struct levene_info *l = (struct levene_info *) _l;
334 for (v = 0; v < l->n_dep; ++v)
336 double lz_numerator = 0;
337 struct hsh_iterator hi;
338 struct group_statistics *g;
340 struct variable *var = l->v_dep[v] ;
341 struct hsh_table *hash = var->p.grp_data.group_hash;
343 for(g = (struct group_statistics *) hsh_first(hash,&hi);
345 g = (struct group_statistics *) hsh_next(hash,&hi) )
347 lz_numerator += g->n * pow2(g->lz_mean - lz[v].grand_mean );
349 lz_numerator *= ( l->v_dep[v]->p.grp_data.ugs.n -
350 l->v_dep[v]->p.grp_data.n_groups );
352 lz_denominator[v] *= (l->v_dep[v]->p.grp_data.n_groups - 1);
354 l->v_dep[v]->p.grp_data.levene = lz_numerator/lz_denominator[v] ;
358 /* Now clear up after ourselves */
359 free(lz_denominator);